Hibernate4第一部分--构建环境
构建环境
1、导入jar包
最简单的方法:把hibernate-release-4.0.0.Beta4.zip包里面lib/required下的jar包全部添加到工程的library里面,另外还需添加slf4j的实现包slf4j-log4j12-1.5.8.jar和log4j的实现包log4j-1.2.16.jar,还有别忘了把JDBC的驱动jar包也加入到library里面。
2、Object怎么做
1:就是前面学过的vo的写法(规则同样是那四点)
2:要求必须有一个public为空参的构造方法,现在写vo一般不写构造方法,默认就
有一个,但是写构造方法的时候要注意写上一个public为空参的构造方法
3:要求提供一个标识属性(identifier)
4:使用非final的类(因为要使用代理来延迟实体的装载)
5:设若构建一个对象:cn.javass.h4.hello.UserModel,有四个属性:
uuid,userId,name,age
package com.lim.hello.model;
public class UserModel {
public UserModel() {
}
private String uuid;
private String userId;
private String name;
private int age;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3、在数据库中建表
设若构建一个表为tbl_user,字段:uuid,userId,name,age
create table tbl_user (uuid varchar2(20) primary key,userId varchar2(20) not null,name varchar2(20),age number(3));
4、配置xxx.cfg.xml
1:缺省名称为 hibernate.cfg.xml
2:存放在当前classes的根目录下,开发的时候在src根下就可以了
3:主要有如下四部分配置 :
(1)与DB的连接
(2)可选配置
(3)资源文件注册
(4)二级缓存
4:配置的时候可以到Hibernate发行包里面找个hibernate.cfg.xml的例子,比如可以用
"\project\hibernate-
documentation\quickstart\tutorials\basic\src\test\resources"下面的
hibernate.cfg.xml作例子.
5:示例如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">hbnt</property>
<property name="connection.password">hbnt</property>
<property name="connection.pool_size">2</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/lim/hello/model/UserModel.hbm.xml" />
</session-factory>
</hibernate-configuration>
5、配置xxx.hbm.xml
1:与被描述的类同名 ,如:UserModel.hbm.xml
2:存放位置与所描述类存放在同一文件夹下
3:主要有如下四部分配置 :
(1)类和表的映射
(2)主键的映射
(3)类的属性和DB中字段的映射
(4)关系的映射
4:配置的时候可以到hibernate发行包里面找个例子,比如可以用
"\project\hibernate-core\src\test\java\org\hibernate\test\cid"下面的
Customer.hbm.xml作例子
5:示例如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lim.hello.model.UserModel" table="tbl_user">
<id name="uuid" length="20">
<generator class="assigned" /> <!-- 主键生成策略 -->
</id>
<property name="userId" not-null="true" length="20" />
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
6、客户端测试文件
package com.lim.hello.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.lim.hello.model.UserModel;
public class HelloUser {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = null;
Transaction t = null;
try {
// 准备数据
UserModel um = new UserModel();
um.setUuid("1");
um.setUserId("id1");
um.setName("name1");
um.setAge(1);
s = sf.openSession();
t = s.beginTransaction();
s.save(um);
t.commit();
} catch (Exception err) {
t.rollback();
err.printStackTrace();
} finally {
s.close();
}
}
}
测试:直接在Elipse里面运行Client文件即可,运行结束,你将会看到在console输出:
"Hibernate: insert into tbl_user (userId, name, age, uuid) values(?, ?, ?, ?)",打开数据库的数据表,你会看到一条值已经加入了。
说明:
1 :SessionFactory sf = new Configuration().configure().buildSessionFactory();
这句话的意思是读取hibernate.cfg.xml,创建Session工厂,是线程安全的。
默认是"hibernate.cfg.xml",不用写出来,如果文件名不是"hibernate.cfg.xml",那么需要显示指定,如下:
SessionFactory sf = new Configuration().configure( "javass.cfg.xml ").buildSessionFactory();
2 :Session是应用程序主要使用的Hibernate接口,约相当于JDBC的
Connection+Statement/PreparedStatement的功能,是线程不安全的。
3 :这里使用的事务Transaction是Hibernate的Transaction,需要有,不能去掉。