Heim > Datenbank > MySQL-Tutorial > IT忍者神龟之Hibernat持久化对象-数据表映射配置回顾

IT忍者神龟之Hibernat持久化对象-数据表映射配置回顾

WBOY
Freigeben: 2016-06-07 16:09:58
Original
1557 Leute haben es durchsucht

1.持久化对象POJO编写规则: 1) 有空参public构造器; 2) 提供标识属性,映射数据表主键; 3) 属性提供setter和getter方法; 4) 属性使用基本数据类型的包装类型。基本类型在数据库中不能区分null和0; 5) 不使用final修饰。如果使用final则无法生成代

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 <code class="xml keyword">xml <code class="xml color1"> version<code class="xml plain">=<code class="xml string">"1.0" <code class="xml color1"> encoding<code class="xml plain">=<code class="xml string">"UTF-8"<code class="xml plain">?> <code class="xml plain">DOCTYPE <code class="xml plain"> hibernate-mapping PUBLIC <code class="xml spaces"> <code class="xml plain">"-//Hibernate/Hibernate Mapping DTD 3.0//EN" <code class="xml spaces"> <code class="xml plain">"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <code class="xml comments"><!-- package="POJO所在包" --> <code class="xml plain">hibernate-mapping <code class="xml color1"> package<code class="xml plain">=<code class="xml string">"cn.cvu.domain"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO类" table="数据库中的表单" catalog="数据库,对应项目的hibernate核心配置文件中<property name=&#39;hibernate.connection.url&#39;>jdbc:mysql:///数据库</property> " --> <code class="xml spaces"> <code class="xml plain">class <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"User" <code class="xml color1"> table<code class="xml plain">=<code class="xml string">"table_user" <code class="xml color1"> catalog<code class="xml plain">=<code class="xml string">"db_hibernate"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO对应的属性" column="表单的主键 列" type="POJO属性的数据类型" --> <code class="xml spaces"> <code class="xml plain">id <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"id" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"id" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"int"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- class="主键生成策略" --> <code class="xml spaces"> <code class="xml plain">generator <code class="xml color1"> class<code class="xml plain">=<code class="xml string">"native"<code class="xml plain">><code class="xml keyword">generator<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 常用策略:increment、identity、sequence、native、uuid、assigned --> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO中对应属性" colunm="表单中的 列" type="POJO数据类型" --> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"name" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"name" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"age" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"age" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"int" <code class="xml plain"> ><code class="xml keyword">property<code class="xml plain">> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"city" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"city" <code class="xml color1"> sql-type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 常用属性:length:列值长度、not-null:非空true/false、unique:唯一true/false --> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">class<code class="xml plain">> <code class="xml plain"><code class="xml keyword">hibernate-mapping<code class="xml plain">>

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 <code class="java keyword"> void <code class="java plain"> testInsertAssigned() { <code class="java spaces"> <code class="java plain">Session session = UtilGetSesstion.openSession(); <code class="java spaces"> <code class="java plain">Transaction transaction = session.beginTransaction(); <code class="java spaces"> <code class="java spaces"> <code class="java plain">User user = <code class="java keyword">new <code class="java plain"> User(); <code class="java spaces"> <code class="java plain">user.setId(<code class="java value">20130124<code class="java plain">); <code class="java comments">//手动设置主键 <code class="java spaces"> <code class="java plain">user.setName(<code class="java string">"EminemXXX"<code class="java plain">); <code class="java spaces"> <code class="java plain">user.setAge(<code class="java value">42<code class="java plain">); <code class="java spaces"> <code class="java plain">user.setCity(<code class="java string">"NewYorkXXX"<code class="java plain">); <code class="java spaces"> <code class="java plain">session.save(user); <code class="java spaces"> <code class="java spaces"> <code class="java plain">transaction.commit(); <code class="java spaces"> <code class="java plain">session.close(); <code class="java spaces"> <code class="java plain">}

7)主键生成策略:复合主键:

(1)编写POJO类 Person.java:

1 2 3 4 5 6 7 8 9 10 11 package <code class="java plain"> cn.cvu.domain; <code class="java keyword">import <code class="java plain"> java.io.Serializable; <code class="java comments">//务必事先序列化接口 <code class="java keyword">public <code class="java keyword"> class <code class="java plain"> Person <code class="java keyword">implements <code class="java plain"> Serializable { <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String firstName; <code class="java comments">//对应表单的复合主键 <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String secondName; <code class="java comments">//对应表单的复合主键 <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String address; <code class="java spaces"> <code class="java spaces"> <code class="java comments">//get/set <code class="java spaces"> <code class="java comments">//toString <code class="java plain">}

(2)配置POJO.hbm.xml:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <code class="xml keyword">xml <code class="xml color1"> version<code class="xml plain">=<code class="xml string">"1.0" <code class="xml color1"> encoding<code class="xml plain">=<code class="xml string">"UTF-8"<code class="xml plain">?> <code class="xml plain">DOCTYPE <code class="xml plain"> hibernate-mapping PUBLIC <code class="xml spaces"> <code class="xml plain">"-//Hibernate/Hibernate Mapping DTD 3.0//EN" <code class="xml spaces"> <code class="xml plain">"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <code class="xml plain">hibernate-mapping <code class="xml color1"> package<code class="xml plain">=<code class="xml string">"cn.cvu.domain"<code class="xml plain">> <code class="xml spaces"> <code class="xml plain">class <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"Person" <code class="xml color1"> table<code class="xml plain">=<code class="xml string">"table_person" <code class="xml color1"> catalog<code class="xml plain">=<code class="xml string">"db_hibernate"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 复合id --> <code class="xml spaces"> <code class="xml plain">composite-id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 主键列 name="POJO属性" column="表单列名" --> <code class="xml spaces"> <code class="xml plain">key-property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"firstName" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"nameFirst"<code class="xml plain">><code class="xml keyword">key-property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain">key-property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"secondName" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"nameFirst"<code class="xml plain">><code class="xml keyword">key-property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">composite-id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 普通列 name="POJO属性" column="表单列名" type="数据类型" --> <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"address" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"addr" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">class<code class="xml plain">> <code class="xml plain"><code class="xml keyword">hibernate-mapping<code class="xml plain">>

(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 <code class="java keyword"> void <code class="java plain"> testInsert() { <code class="java spaces"> <code class="java plain">Configuration configuration = <code class="java keyword">new <code class="java plain"> Configuration().configure(); <code class="java spaces"> <code class="java plain">SessionFactory sessionFactory = configuration.buildSessionFactory(); <code class="java spaces"> <code class="java plain">Session session = sessionFactory.openSession(); <code class="java spaces"> <code class="java plain">Transaction transaction = session.beginTransaction(); <code class="java spaces"> <code class="java spaces"> <code class="java plain">Person person = <code class="java keyword">new <code class="java plain"> Person(); <code class="java spaces"> <code class="java plain">person.setFirstName(<code class="java string">"C"<code class="java plain">); <code class="java spaces"> <code class="java plain">person.setSecondName(<code class="java string">"Vigiles"<code class="java plain">); <code class="java spaces"> <code class="java plain">person.setAddress(<code class="java string">"Beijng"<code class="java plain">); <code class="java spaces"> <code class="java plain">session.save(person); <code class="java spaces"> <code class="java spaces"> <code class="java plain">transaction.commit(); <code class="java spaces"> <code class="java plain">session.close(); <code class="java spaces"> <code class="java plain">sessionFactory.close(); <code class="java spaces"> <code class="java plain">}

(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语言 --> <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"hibernate.dialect"<code class="xml plain">> <code class="xml plain">  org.hibernate.dialect.MySQL5InnoDBDialect<code class="xml keyword">property<code class="xml plain">>

-end
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage