新建數據庫User:
1.CREATE TABLE user (
2. name VARCHAR(100) NOT NULL,
3. phone VARCHAR(50) NOT NULL,
4. age INT,
5. PRIMARY KEY(name, phone)
6.);
現將name和phone定義成復合主鍵。分別使用2種方法如下:
方法1.復合主鍵字段直接包含在PO類中。
User.java(需要實現Serializable接口,並定義equals()和hashCode()方法)
1.package com.hb3.pack_03.model;
2.
3.import java.io.Serializable;
4.import org.apache.commons.lang.builder.EqualsBuilder;
5.import org.apache.commons.lang.builder.HashCodeBuilder;
6. 7.public class User implements Serializable {
8. 9. private static final long serialVersionUID = -8377583111386512407L;
10. 11. private String name;
12. private String phone;
13. private Integer age;
14.
15. public User() {
16. }
17. 18. public Integer getAge() {
19. return age;
20. }
21. 22. public void setAge(Integer age) {
23. this.age = age;
24. }
25. 26. public String getName() {
27. return name;
28. }
29. 30. public void setName(String name) {
31. this.name = name;
32. }
33. 34. public String getPhone() {
35. return phone;
36. }
37. 38. public void setPhone(String phone) {
39. this.phone = phone;
40. }
41.
42. public boolean equals(Object obj) {
43. if(obj == this) {
44. return true;
45. }
46.
47. if(!(obj instanceof User)) {
48. return false;
49. }
50.
51. User user = (User) obj;
52. return new EqualsBuilder()
53. .append(this.name, user.getName())
54. .append(this.phone, user.getPhone())
55. .isEquals();
56. }
57.
58. public int hashCode() {
59. return new HashCodeBuilder()
60. .append(this.name)
61. .append(this.phone)
62. .toHashCode();
63. }
64.}
在定義equals和hashCode方法時使用了apache的common-lang包。
User.hbm.xml文件:
1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping>
8. 9. <class name="com.hb3.pack_03.model.User" table="user"> 10.
11. <composite-id> 12.
13. <key-property name="name"
14. column="name"
15. type="java.lang.String"/> 16. <key-property name="phone"
17. column="phone"
18. type="java.lang.String"/> 19. </composite-id> 20. 21. <property name="age" column="age" type="java.lang.Integer"/> 22.
23. </class>
24.
25.</hibernate-mapping>
hibernate.cfg.xml中修改:
......
<mapping resource="com/hb3/pack_03/model/User.hbm.xml" />
......
測試代碼如下:
1.package com.hb3.pack_03;
2.
3.import org.hibernate.Session;
4.import org.hibernate.SessionFactory;
5.import org.hibernate.Transaction;
6.import org.hibernate.cfg.Configuration;
7. 8.import com.hb3.pack_03.model.User;
9. 10.public class BusinessService {
11. 12. public static void main(String[] args) {
13.
14. Configuration config = new Configuration().configure();
15. SessionFactory sessionFactory = config.buildSessionFactory();
16. Session session = sessionFactory.openSession();
17. 18. User user = new User();
19. user.setName("shenbin");
20. user.setPhone("0970123456");
21. user.setAge(28);
22. Transaction tx = session.beginTransaction();
23. session.save(user);
24. tx.commit();
25.
26. user = (User) session.load(User.class, user);
27.
28. System.out.println(user.getAge() + "\t" + user.getName() + "\t" + user.getPhone());
29. 30. session.close();
31. sessionFactory.close();
32. }
33.}
請注意:在實際測試過程中,如果數據庫已經有數據存在,則不需要上面的黑題部分向數據庫追加數據。
另外還請留意的是,光有session.save(user);而沒有tx.commit();的話,數據不會被持久到數據庫中的。
方法2.復合主鍵字段獨立到另一個類中。
UserPK.java(需要實現Serializable接口,並定義equals()和hashCode()方法)
1.package com.hb3.pack_04.model;
2.
3.import java.io.Serializable;
4. 5.import org.apache.commons.lang.builder.EqualsBuilder;
6.import org.apache.commons.lang.builder.HashCodeBuilder;
7. 8.public class UserPK implements Serializable {
9. 10. private static final long serialVersionUID = -2457999265373664790L;
11. 12. private String name;
13. private String phone;
14. 15. public String getName() {
16. return name;
17. }
18. public void setName(String name) {
19. this.name = name;
20. }
21. public String getPhone() {
22. return phone;
23. }
24. public void setPhone(String phone) {
25. this.phone = phone;
26. }
27. public boolean equals(Object obj) {
28. if(obj == this) {
29. return true;
30. }
31.
32. if(!(obj instanceof User)) {
33. return false;
34. }
35.
36. UserPK pk = (UserPK) obj;
37. return new EqualsBuilder()
38. .append(this.name, pk.getName())
39. .append(this.phone, pk.getPhone())
40. .isEquals();
41. }
42.
43. public int hashCode() {
44. return new HashCodeBuilder()
45. .append(this.name)
46. .append(this.phone)
47. .toHashCode();
48. }
49.}
User.java
1.package com.hb3.pack_04.model;
2.
3.import java.io.Serializable;
4. 5.public class User implements Serializable {
6. 7. private static final long serialVersionUID = -8630481462628539996L;
8. 9. private UserPK userPK;
10. private Integer age;
11.
12. public User() {
13. }
14. 15. public UserPK getUserPK() {
16. return userPK;
17. }
18. public void setUserPK(UserPK userPK) {
19. this.userPK = userPK;
20. }
21. public Integer getAge() {
22. return age;
23. }
24. public void setAge(Integer age) {
25. this.age = age;
26. }
27.}
User.hbm.xml文件:
1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping>
8. 9. <class name="com.hb3.pack_04.model.User" table="user"> 10.
11. <composite-id name="userPK" 12. class="com.hb3.pack_04.model.UserPK" 13. unsaved-value="any"> 14. <key-property name="name"
15. column="name"
16. type="java.lang.String"/> 17. <key-property name="phone"
18. column="phone"
19. type="java.lang.String"/> 20. </composite-id> 21.
22. <property name="age" column="age" type="java.lang.Integer"/> 23.
24. </class>
25. 26.</hibernate-mapping>
hibernate.cfg.xml中修改:
......
<mapping resource="com/hb3/pack_04/model/User.hbm.xml" />
......
測試代碼如下:
1.package com.hb3.pack_04;
2.
3.import org.hibernate.Session;
4.import org.hibernate.SessionFactory;
5.import org.hibernate.cfg.Configuration;
6. 7.import com.hb3.pack_04.model.User;
8.import com.hb3.pack_04.model.UserPK;
9. 10.public class BusinessService {
11. 12. public static void main(String[] args) {
13.
14. Configuration config = new Configuration().configure();
15. SessionFactory sessionFactory = config.buildSessionFactory();
16. Session session = sessionFactory.openSession();
17. 18. UserPK pk = new UserPK();
19. pk.setName("shenbin");
20. pk.setPhone("0970123456");
21.
22. User user = (User) session.load(User.class, pk);
23.
24. System.out.println(user.getAge() +
25. "\t" + user.getUserPK().getName() +
26. "\t" + user.getUserPK().getPhone());
27. 28. session.close();
29. sessionFactory.close();
30. }
31.}