1.持久化对象POJO编写规则:
1) 有空參public构造器;
2) 提供标识属性。映射数据表主键;
3) 属性提供setter和getter方法。
4) 属性使用基本数据类型的包装类型。基本类型在数据库中不能区分null和0;
5) 不使用final修饰。
假设使用final则无法生成代理对象。当使用了final。load查询将如同get查询。
2.POJO的OID:
OID是持久化类与数据表主键相应的属性,用来唯一区分持久化对象。
自然主键:採用数据库中有意义的列的值作为主键(有意义)
代理主键:採用自己主动生成的流水号、UUID作为主键(无意义。推荐)
3.数据类型:
基本类型无法区分null和0,开发中POJO属性都使用包装类型。
4.持久化类配置POJO.hbm.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml
version = "1.0"
encoding = "UTF-8" ?> <! DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--
package="POJO所在包" --> < hibernate-mapping
package = "cn.cvu.domain" > <!--
name="POJO类" table="数据库中的表单" catalog="数据库。相应项目的hibernate核心配置文件里<property name='hibernate.connection.url'>jdbc:mysql:///数据库</property> " --> < class
name = "User"
table = "table_user"
catalog = "db_hibernate" > <!--
name="POJO相应的属性" column="表单的主键 列" type="POJO属性的数据类型" --> < id
name = "id"
column = "id"
type = "int" > <!--
class="主键生成策略" --> < generator
class = "native" ></ generator > <!--
经常使用策略:increment、identity、sequence、native、uuid、assigned --> </ id > <!--
name="POJO中相应属性" colunm="表单中的 列" type="POJO数据类型" --> <!—hibernate定义的数据类型string
--> < property
name = "name"
column = "name"
type = "string" ></ property >
<!—java定义的为int
--> < property
name = "age"
column = "age"
type = "int"
></ property >
<!—sql定义的数据类型使用sql-type声明
--> < property
name = "city"
column = "city"
sql-type = "string" ></ property >
<!--
经常使用属性:length:列值长度、not-null:非空true/false、unique:唯一true/false --> </ class > </ hibernate-mapping > |
1)主键生成策略increment:顶层递增:
由hibernate自己主动完毕,原理:先查询最大值。再插入此值加一。OID必须为long、int或short类型。
长处:跨数据库。
缺点:多线程并发訪问问题。
2)主键生成策略identity:底层递增:
由数据库自己主动完毕,要求数据库必须支持自增主键。mysql支持。oracle不支持。OID必须为long、int或short类型。
长处:无并发訪问问题。
3)主键生成策略sequence:序列:
由数据库自己主动完毕递增。要求数据库必须支持序列。mysql不支持。oracle支持。OID必须为long、int或short类型。
Oracle:
1创建序列:create sequence myseq;
2调用序列:insert into customer values (myseq.nextval); #序列加一
4)主键生成策略native:本地:
採用数据库支持的自增策略。 mysql:identity,oracle:sequence。
OID必须为long、int或short类型。
长处:跨数据库平台。
5)主键生成策略uuid:标识符:
由数据库自己主动创建。 uuid是32位唯一字符串。表单主键使用varchar类型,POJO相应属性是String类型。
6)主键生成策略assigned:手动指定:
在调用hibernate时手动指定主键的值,用于自然主键(有意义的)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
void
testInsertAssigned() { Session
session = UtilGetSesstion.openSession(); Transaction
transaction = session.beginTransaction(); User
user = new
User(); user.setId( 20130124 );
//手动设置主键 user.setName( "EminemXXX" ); user.setAge( 42 ); user.setCity( "NewYorkXXX" ); session.save(user); transaction.commit(); session.close(); } |
7)主键生成策略:复合主键:
(1)编写POJO类 Person.java:
1
2
3
4
5
6
7
8
9
10
11
|
package
cn.cvu.domain; import
java.io.Serializable; //务必事先序列化接口 public
class
Person implements
Serializable { private
String firstName; //相应表单的复合主键
private
String secondName; //相应表单的复合主键
private
String address; //get/set //toString } |
(2)配置POJO.hbm.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml
version = "1.0"
encoding = "UTF-8" ?> <! DOCTYPE
hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> < hibernate-mapping
package = "cn.cvu.domain" > < class
name = "Person"
table = "table_person"
catalog = "db_hibernate" > <!--
复合id --> < composite-id > <!--
主键列 name="POJO属性" column="表单列名" --> < key-property
name = "firstName"
column = "nameFirst" ></ key-property > < key-property
name = "secondName"
column = "nameFirst" ></ key-property > </ composite-id > <!--
普通列 name="POJO属性" column="表单列名" type="数据类型" --> < property
name = "address"
column = "addr"
type = "string" ></ property > </ class > </ hibernate-mapping > |
(3)配置hibernate.cfg.xml,载入Person.hbm.xml文件:
(4)操作类的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
void
testInsert() { Configuration
configuration = new
Configuration().configure(); SessionFactory
sessionFactory = configuration.buildSessionFactory(); Session
session = sessionFactory.openSession(); Transaction
transaction = session.beginTransaction(); Person
person = new
Person(); person.setFirstName( "C" );
person.setSecondName( "Vigiles" );
person.setAddress( "Beijng" );
session.save(person);
transaction.commit();
session.close(); sessionFactory.close(); } |
(5)结果:
INFO: HHH000262: Table not found: tb_person
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: create table db_hibernate.tb_person (namef varchar(255) not null, names varchar(255) not null, age integer, city varchar(255), primary key
(namef, names)) type=InnoDB
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 7
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
1
2
3
|
<!--
方言-指定数据库为5.1版本号之后的SQL语言 --> < property
name = "hibernate.dialect" > org.hibernate.dialect.MySQL5InnoDBDialect</ property > |
-end