从 hbm.xml 到 annotations
下面让我们先看一个通常用 hbm.xml 映射文件的例子. 有3个类 .hibernateutil.java 也就是 hibernate文档中推荐的工具类,person.java , test.java 测试用的类.都在test.hibernate 包中. 每个类的代码如下:
hibernateutil:
01 package test.hibernate;
02
03 import org.hibernate.hibernateexception;
04 import org.hibernate.session;
05 import org.hibernate.sessionfactory;
06 import org.hibernate.cfg.configuration;
07
08 public class hibernateutil {
09 public static final sessionfactory sessionfactory;
10
11 static {
12 try {
13 sessionfactory = new configuration()
14 .configure()
15 .buildsessionfactory();
16 } catch (hibernateexception e) {
17 // todo auto-generated catch block
18
19 e.printstacktrace();
20 throw new exceptionininitializererror(e);
21 }
22 }
23
24 public static final threadlocal<session> session = new threadlocal<session>();
25
26 public static session currentsession() throws hibernateexception {
27 session s = session.get();
28
29 if(s == null) {
30 s = sessionfactory.opensession();
31 session.set(s);
32 }
33
34 return s;
35 }
36
37 public static void closesession() throws hibernateexception {
38 session s = session.get();
39 if(s != null) {
40 s.close();
41 }
42 session.set(null);
43 }
44 }
person:
01 package test.hibernate;
02
03 import java.util.linkedlist;
04 import java.util.list;
05
06 /**
07 *
08 */
09
10 @suppresswarnings("serial")
11 public class person implements java.io.serializable {
12
13 // fields
14
15 private integer id;
16
17 private string name;
18
19 private string sex;
20
21 private integer age;
22
23 private list list = new linkedlist();
24
25 // collection accessors
26
27 public list getlist() {
28 return list;
29 }
30
31 public void setlist(list list) {
32 this.list = list;
33 }
34
35 /** default constructor */
36 public person() {
37 }
38
39 /** constructor with id */
40 public person(integer id) {
41 this.id = id;
42 }
43
44 // property accessors
45
46 public integer getid() {
47 return this.id;
48 }
49
50 public void setid(integer id) {
51 this.id = id;
52 }
53
54 public string getname() {
55 return this.name;
56 }
57
58 public void setname(string name) {
59 this.name = name;
60 }
61
62 public string getsex() {
63 return this.sex;
64 }
65
66 public void setsex(string sex) {
67 this.sex = sex;
68 }
69
70 public integer getage() {
71 return this.age;
72 }
73
74 public void setage(integer age) {
75 this.age = age;
76 }
77
78 }
test:
01 /*
02 * created on
03 * @author
04 */
05 package test.hibernate;
06
07 import java.sql.sqlexception;
08
09 import org.hibernate.flushmode;
10 import org.hibernate.hibernateexception;
11 import org.hibernate.session;
12 import org.hibernate.transaction;
13
14 public class test {
15
16 public static void main(string [] args) {
17 session s = hibernateutil.currentsession();
18
19 transaction tx = s.begintransaction();
20
21 // person p = (person) s.load(person.class, 1);
22 // system.out.println(p.getname());
23 person p = new person();
24
25 p.setage(19);
26 p.setname("icerain");
27 p.setsex("male");
28 s.save(p);
29 s.flush();
30 /*
31 person p2 = (person) s.get(person.class, new integer(1));
32 system.out.println(p2.getname());
33 p2.setname("ice..");
34 s.saveorupdate(p2);
35 s.flush();
36 person p3 = (person) s.get(person.class, new integer(2));
37 system.out.println(p3.getname());
38 s.delete(p3);
39 */
40
41 tx.commit();
42 try {
43 system.out.println(p.getname());
44 } catch (exception e) {
45 // todo auto-generated catch block
46 e.printstacktrace();
47 }
48
49 hibernateutil.closesession();
50 }
51 }
hibernate.cfg.xml 配置文件如下,利用mysql 数据库.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.driver</property>
<property name="hibernate.connection.password">你的数据库密码</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/数据库名</property>
<property name="hibernate.connection.username">用户名</property>
<property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property>
<property name="show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.jdbctransactionfactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="test/hibernate/annotation/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
其中 配置了<property name="hibernate.hbm2ddl.auto">update</property>属性 自动导入数据库ddl.生产的ddl sql语句如下
create table person (id integer not null auto_increment, name varchar(255), sex varchar(255), age integer, person integer, primary key (id))
alter table person add index fkc4e39b5511c4a5c2 (person), add constraint fkc4e39b5511c4a5c2 foreign key (person) references person (id)
而person.hbm.xml 文件如下:
<?xml version="1.0"?>
<hibernate-mapping>
<class name="test.hibernate.person" table="person">
<id name="id" type="integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="string">
<column name="name" />
</property>
<property name="sex" type="string">
<column name="sex" />
</property>
<property name="age" type="integer">
<column name="age" />
</property>
<bag name="list" cascade="all">
<key column="person"></key>
<one-to-many class="test.hibernate.person"/>
</bag>
</class>
</hibernate-mapping>
下面让我们看看利用 hibernate annotations 如何做,只要三个类 不再需要 hbm.xml配置文件:
还要把用到的两个jar文件 放入的类路径中. 具体如何做,请参考 hibernate annotations 中文文档
http://hibernate.6644.net
hibernateutil.java 也就是 hibernate文档中推荐的工具类,person.java 一个持久化的类, test.java 测试用的类.都在test.hibernate.annotation 包中. 每个类的代码如下:
hibernateutil
01 package test.hibernate.annotation;
02
03 import org.hibernate.hibernateexception;
04 import org.hibernate.session;
05 import org.hibernate.sessionfactory;
06 import org.hibernate.cfg.annotationconfiguration;
07 import org.hibernate.cfg.configuration;
08
09 public class hibernateutil {
10 public static final sessionfactory sessionfactory;
11
12 static {
13 try {
14 sessionfactory = new annotationconfiguration() //注意: 建立 sessionfactory于前面的不同
15 .addpackage("test.hibernate.annotation")
16 .addannotatedclass(person.class)
17
18 .configure()
19 .buildsessionfactory();
20 //new configuration().configure().buildsessionfactory();
21 } catch (hibernateexception e) {
22 // todo auto-generated catch block
23
24 e.printstacktrace();
25 throw new exceptionininitializererror(e);
26 }
27 }
28
29 public static final threadlocal<session> session = new threadlocal<session>();
30
31 public static session currentsession() throws hibernateexception {
32 session s = session.get();
33
34 if(s == null) {
35 s = sessionfactory.opensession();
36 session.set(s);
37 }
38
39 return s;
40 }
41
42 public static void closesession() throws hibernateexception {
43 session s = session.get();
44 if(s != null) {
45 s.close();
46 }
47 session.set(null);
48 }
49 }
person:
01 package test.hibernate.annotation;
02
03 import java.util.linkedlist;
04 import java.util.list;
05
06 import javax.persistence.accesstype;
07 import javax.persistence.basic;
08 import javax.persistence.entity;
09 import javax.persistence.generatortype;
10 import javax.persistence.id;
11 import javax.persistence.onetomany;
12 import javax.persistence.table;
13 import javax.persistence.transient;
14
15 /**
16 *
17 */
18
19 @suppresswarnings("serial")
20 @entity(access = accesstype.property) //定义该类为实体类
21 @table //映射表
22 public class person implements java.io.serializable {
23
24 // fields
25
26 private integer id;
27
28 private string name;
29
30 private string sex;
31
32 private integer age;
33
34 private list list = new linkedlist();
35
36 // constructors
37 /** default constructor */
38 public person() {
39 }
40
41 /** constructor with id */
42 public person(integer id) {
43 this.id = id;
44 }
45
46 // property accessors
47 @id
48 public integer getid() {
49 return this.id;
50 }
51
52 public void setid(integer id) {
53 this.id = id;
54 }
55
56 @basic
57 public string getname() {
58 return this.name;
59 }
60
61 public void setname(string name) {
62 this.name = name;
63 }
64
65 @basic
66 public string getsex() {
67 return this.sex;
68 }
69
70 public void setsex(string sex) {
71 this.sex = sex;
72 }
73
74 @basic
75 public integer getage() {
76 return this.age;
77 }
78
79 public void setage(integer age) {
80 this.age = age;
81 }
82 @transient //由于本例不打算演示集合映射 所有声明该属性为 transient
83 public list getlist() {
84 return list;
85 }
86
87 public void setlist(list list) {
88 this.list = list;
89 }
90
91 }
注意该实体类中的属性都使用了默认值.
test.java 代码同上
不需要了 hbm.xml 映射文件, 是不是简单了一些 .给人认为简化了一些不是主要目的.主要是可以了解一下 ejb3 的持久化机制 ,提高一下开发效率才是重要的.
好了 .本例就完了 . 感觉怎么样了 .欢迎你来批批.
ps:
生成的数据库表 和 程序执行后的 数据库情况如下
mysql> describe person;
+--------+--------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | no | pri | null | auto_increment |
| name | varchar(255) | yes | | null | |
| sex | varchar(255) | yes | | null | |
| age | int(11) | yes | | null | |
| person | int(11) | yes | mul | null | |
+--------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> select * from person;
+----+---------+------+------+--------+
| id | name | sex | age | person |
+----+---------+------+------+--------+
| 1 | icerain | male | 19 | null |
+----+---------+------+------+--------+
1 row in set (0.03 sec)
闽公网安备 35060202000074号