Hibernate學(xué)習(xí)筆記( 三)-- 復(fù)合主鍵
Posted on 2008-09-01 16:43 ∪∩BUG 閱讀(920) 評論(1) 編輯 收藏 所屬分類: Hibernate學(xué)習(xí)筆記關(guān)于如何配置請參看:Hibernate學(xué)習(xí)筆記(一)--用MyEclipse 6.5+MySQL 5.0的環(huán)境跑起來
準(zhǔn)備:建表
用MySQL在名為STMS數(shù)據(jù)庫中建表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
//實(shí)現(xiàn)復(fù)合主鍵應(yīng)滿足的要求:1.本類必須實(shí)現(xiàn)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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

通過Hibernate反向工程建立personx表與Personx類的映射
首先調(diào)出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 @ 復(fù)合主鍵類
10
*/
11
//實(shí)現(xiàn)復(fù)合主鍵應(yīng)滿足的要求:
12
//1.本類必須實(shí)現(xiàn)Serializable 接口
13
//2.復(fù)寫equals(比較對象)和hashCode(取得Hash編碼)方法(可用commons-lang-2.4.jar來復(fù)寫,
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
// 修改復(fù)寫equals的方法
53
public boolean equals(Object obj) {
54
if (this == obj) {
55
return true;
56
}
57
58
// 如果不是Personx的實(shí)例
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
//修改復(fù)寫hashCode的方法
68
public int hashCode() {
69
return new HashCodeBuilder().append(this.name).append(
70
this.phone).toHashCode();
71
}
72
73
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

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 是一個(gè)接口,必須實(shí)例化
17
// 在構(gòu)造方法中實(shí)例實(shí)化Session對象
18
public PersonxOperate() {
19
// 找到Hibernae配置文件,從全局文件中取出SessionFactory,從sessionFactory中取出一個(gè)session
20
this.session = new Configuration().configure().buildSessionFactory()
21
.openSession();
22
}
23
24
// 所有的操作都是通過Session進(jìn)行
25
// (1)增加操作
26
public void insert(Personx p) {
27
// 將數(shù)據(jù)存放到數(shù)據(jù)庫中
28
this.session.save(p);
29
30
// 事務(wù)提交
31
this.session.beginTransaction().commit();
32
}
33
}
34

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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在表中是主鍵當(dāng)插入值為空時(shí)將無法插入
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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

2

3

4

7

8

9

10

14

15

16

17

18

19

20

21

22

23

24

25

26

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>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

例子結(jié)構(gòu):