Posted on 2008-09-01 16:43
∪∩BUG 阅读(915)
评论(1) 编辑 收藏 所属分类:
Hibernate学习笔记
关于如何配置请参看:Hibernate学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
准备:建表
用MySQL在名为STMS数据库中建表personx
src/org.lxh.hibernate2.Personx.java
1
package org.lxh.hibernate2;
2
3
/** *//**
4
* @author ∪∩BUG E-mail: tidelgl@163.com
5
* @version Aug 31, 2008 9:56:51 AM @ POJO类
6
*/
7
8
//实现复合主键应满足的要求:1.本类必须实现Serializable 接口
9
public class Personx implements java.io.Serializable
{
10
11
private PersonxId id;
12
private int age;
13
14
public Personx()
{
15
}
16
17
public PersonxId getId()
{
18
return this.id;
19
}
20
21
public void setId(PersonxId id)
{
22
this.id = id;
23
}
24
25
public int getAge()
{
26
return age;
27
}
28
29
public void setAge(int age)
{
30
this.age = age;
31
}
32
}
通过Hibernate反向工程建立personx表与Personx类的映射
首先调出DB Browser视图(Windows—view show—other—MyEclipse datebase—DB Browser)—展开MySQL_localhost至表person—右键表personx—Hibernate Reverse Engineering
src/org.lxh.hibernate2.PersonxId.java
1
package org.lxh.hibernate2;
2
3
import org.apache.commons.lang.builder.EqualsBuilder;
4
import org.apache.commons.lang.builder.HashCodeBuilder;
5
import org.lxh.hibernate1.Personx;
6
7
/** *//**
8
* @author ∪∩BUG E-mail: tidelgl@163.com
9
* @version Aug 31, 2008 9:56:51 AM @ 复合主键类
10
*/
11
//实现复合主键应满足的要求:
12
//1.本类必须实现Serializable 接口
13
//2.复写equals(比较对象)和hashCode(取得Hash编码)方法(可用commons-lang-2.4.jar来复写,
14
//下载:http://apache.mirror.phpchina.com/commons/lang/binaries/commons-lang-2.4-bin.zip)
15
public class PersonxId implements java.io.Serializable
{
16
17
// Fields
18
19
private String name;
20
private String phone;
21
22
// Constructors
23
24
/** *//** default constructor */
25
public PersonxId()
{
26
}
27
28
/** *//** full constructor */
29
public PersonxId(String name, String phone)
{
30
this.name = name;
31
this.phone = phone;
32
}
33
34
// Property accessors
35
36
public String getName()
{
37
return this.name;
38
}
39
40
public void setName(String name)
{
41
this.name = name;
42
}
43
44
public String getPhone()
{
45
return this.phone;
46
}
47
48
public void setPhone(String phone)
{
49
this.phone = phone;
50
}
51
52
// 修改复写equals的方法
53
public boolean equals(Object obj)
{
54
if (this == obj)
{
55
return true;
56
}
57
58
// 如果不是Personx的实例
59
if (!(obj instanceof Personx))
{
60
return false;
61
}
62
PersonxId p = (PersonxId) obj;
63
return new EqualsBuilder().append(this.name, p.getName()).append(
64
this.phone, p.getPhone()).isEquals();
65
}
66
67
//修改复写hashCode的方法
68
public int hashCode()
{
69
return new HashCodeBuilder().append(this.name).append(
70
this.phone).toHashCode();
71
}
72
73
}
src/org.lxh.hibernate2.PersonxOperate.java
1
package org.lxh.hibernate2;
2
3
4
import org.hibernate.Session;
5
import org.hibernate.cfg.Configuration;
6
7
/** *//**
8
* @author ∪∩BUG E-mail: tidelgl@163.com
9
* @version Aug 31, 2008 9:56:51 AM
10
* @ 具体操作Hibernate的类
11
*/
12
public class PersonxOperate
{
13
// 在Hibernate中所有的操作都是通过Session来完成
14
private Session session;
15
16
// Session 是一个接口,必须实例化
17
// 在构造方法中实例实化Session对象
18
public PersonxOperate()
{
19
// 找到Hibernae配置文件,从全局文件中取出SessionFactory,从sessionFactory中取出一个session
20
this.session = new Configuration().configure().buildSessionFactory()
21
.openSession();
22
}
23
24
// 所有的操作都是通过Session进行
25
// (1)增加操作
26
public void insert(Personx p)
{
27
// 将数据存放到数据库中
28
this.session.save(p);
29
30
// 事务提交
31
this.session.beginTransaction().commit();
32
}
33
}
34
src/org.lxh.hibernate2.Test.java
1
package org.lxh.hibernate2;
2
3
4
5
/** *//**
6
* @author ∪∩BUG E-mail: tidelgl@163.com
7
* @version Aug 31, 2008 9:54:16 AM
8
* @测试类
9
*/
10
public class Test
{
11
12
/** *//**
13
* @param args
14
*/
15
public static void main(String[] args)
{
16
Personx p = new Personx();
17
PersonxId pk = new PersonxId();
18
19
//name,phone在表中是主键当插入值为空时将无法插入
20
// p.setAge(4);
21
// p.setName("Hibernate");
22
// p.setPhone("123456");
23
24
p.setAge(2);
25
pk.setName("MySQL");
26
pk.setPhone("87654");
27
p.setId(pk);
28
PersonxOperate po = new PersonxOperate();
29
po.insert(p);
30
}
31
32
}
33
src/org.lxh.hibernate2.Personx.hbm.xml
1
xml version="1.0" encoding="utf-8"?>
2
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
7
<hibernate-mapping>
8
<class name="org.lxh.hibernate2.Personx" table="personx"
9
catalog="stms">
10
14
<composite-id name="id" class="org.lxh.hibernate2.PersonxId">
15
<key-property name="name" type="java.lang.String">
16
<column name="name" length="100" />
17
key-property>
18
<key-property name="phone" type="java.lang.String">
19
<column name="phone" length="50" />
20
key-property>
21
composite-id>
22
<property name="age" type="java.lang.Integer">
23
<column name="age" />
24
property>
25
class>
26
hibernate-mapping>
27
src/hibernate.cfg.xml
1
xml version='1.0' encoding='UTF-8'?>
2
DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6
7
<hibernate-configuration>
8
9
<session-factory>
10
<property name="connection.username">rootproperty>
11
<property name="connection.url">
12
jdbc:mysql://localhost:3306/STMS
13
property>
14
<property name="dialect">
15
org.hibernate.dialect.MySQLDialect
16
property>
17
<property name="myeclipse.connection.profile">
18
MySql_localhost
19
property>
20
<property name="connection.password">rootproperty>
21
<property name="connection.driver_class">
22
com.mysql.jdbc.Driver
23
property>
24
<property name="show_sql">trueproperty>
25
26
27
<mapping resource="org/lxh/hibernate2/Personx.hbm.xml" />
28
29
session-factory>
30
31
hibernate-configuration>
例子结构:
