程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(十九)|關系映射:一對一

hibernate3學習筆記(十九)|關系映射:一對一

編輯:關於JAVA

A.唯一外鍵關聯

DDL:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default '',
4. room_id INT(11)
5.);
6.
7.CREATE TABLE room (
8. id INT(11) NOT NULL auto_increment PRIMARY KEY,
9. address VARCHAR(100) NOT NULL default ''
10.);

User.java

1.package com.hb3.pack_02.model;
2.
3.public class User {
4. 5. private Integer id;
6. private String name;
7. private Room room;
8.
9. public User() {
10. }
11. 12. public Integer getId() {
13. return id;
14. }
15. public void setId(Integer id) {
16. this.id = id;
17. }
18. public String getName() {
19. return name;
20. }
21. public void setName(String name) {
22. this.name = name;
23. }
24. public Room getRoom() {
25. return room;
26. }
27. public void setRoom(Room room) {
28. this.room = room;
29. }
30.}

Room.java

1.package com.hb3.pack_02.model;
2.
3.public class Room {
4. 5. private Integer id;
6. private String address;
7. private User user;
8.
9. public Room() {
10. }
11.
12. public Integer getId() {
13. return id;
14. }
15. public void setId(Integer id) {
16. this.id = id;
17. }
18. public String getAddress() {
19. return address;
20. }
21. public void setAddress(String address) {
22. this.address = address;
23. }
24. public User getUser() {
25. return user;
26. }
27. public void setUser(User user) {
28. this.user = user;
29. }
30.}

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_02.model.User" table="user">
10. <id name="id" column="id" type="java.lang.Integer">
11. <generator class="native"/>
12. </id>
13.
14. <property name="name" column="name" type="java.lang.String"/>
15.
16. <many-to-one name="room"
17. column="room_id"
18. class="com.hb3.pack_02.model.Room" 19. cascade="all" 20. outer-join="true" 21. unique="true"/>
22. </class>
23. 24.</hibernate-mapping>

Room.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_02.model.Room" table="room">
10. <id name="id" column="id">
11. <generator class="native"/>
12. </id>
13. 14. <property name="address"
15. column="address"
16. type="java.lang.String"/>
17.
18. <one-to-one name="user" 19. class="com.hb3.pack_02.model.User" 20. property-ref="room"/> 21. </class>
22. 23.</hibernate-mapping>

測試代碼:

1.package com.hb3.pack_02;
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_02.model.Room;
9.import com.hb3.pack_02.model.User;
10. 11.public class BusinessService {
12. 13. public static void main(String[] args) {
14.
15. Configuration config = new Configuration().configure();
16. SessionFactory sessionFactory = config.buildSessionFactory();
17. Session session = sessionFactory.openSession();
18. 19. User user1 = new User();
20. user1.setName("shenbin");
21. Room room1 = new Room();
22. room1.setAddress("NTU-M8-419");
23. user1.setRoom(room1);
24.
25. User user2 = new User();
26. user2.setName("chanyan");
27. Room room2 = new Room();
28. room2.setAddress("NTU-M8-418");
29. user2.setRoom(room2);
30.
31. Transaction tx = session.beginTransaction();
32. session.save(user1);
33. session.save(user2);
34. tx.commit();
35.
36. Room room = (Room) session.load(Room.class, new Integer(1));
37. System.out.println(room.getUser().getName());
38.
39. session.close();
40. sessionFactory.close();
41. }
42.}

A.主鍵關聯

DDL:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default ''
4.);
5.
6.CREATE TABLE room (
7. id INT(11) NOT NULL auto_increment PRIMARY KEY,
8. address VARCHAR(100) NOT NULL default ''
9.);

Room和User類同上。

Room.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_18.model.Room" table="room"> 10.
11. <id name="id" column="id">
12. <generator class="foreign">
13. <param name="property">user</param> 14. </generator> 15. </id>
16. 17. <property name="address"
18. column="address"
19. type="java.lang.String"/>
20.
21. <one-to-one name="user" 22. class="com.hb3.pack_18.model.User" 23. constrained="true"/> 24. </class>
25. 26.</hibernate-mapping>

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_18.model.User" table="user">
10.
11. <id name="id" column="id" type="java.lang.Integer">
12. <generator class="native"/>
13. </id>
14. 15. <property name="name" column="name" type="java.lang.String"/>
16.
17. <one-to-one name="room"
18. class="com.hb3.pack_18.model.Room" 19. cascade="all"/> 20. </class>
21. 22.</hibernate-mapping>

測試代碼:

1.package com.hb3.pack_18;
2.
3.import java.io.IOException;
4.import java.sql.SQLException;
5. 6.import org.hibernate.Session;
7.import org.hibernate.SessionFactory;
8.import org.hibernate.Transaction;
9.import org.hibernate.cfg.Configuration;
10. 11.import com.hb3.pack_18.model.Room;
12.import com.hb3.pack_18.model.User;
13. 14.public class BusinessService {
15. 16. public static void main(String[] args) throws IOException, SQLException {
17.
18. Configuration config = new Configuration().configure();
19. SessionFactory sessionFactory = config.buildSessionFactory();
20. Session session = sessionFactory.openSession();
21. 22.
23. User user1 = new User();
24. user1.setName("chanyan");
25. Room room1 = new Room();
26. room1.setAddress("NTU-M8-419");
27. user1.setRoom(room1);
28. room1.setUser(user1);
29.
30. User user2 = new User();
31. user2.setName("shenbin");
32. Room room2 = new Room();
33. room2.setAddress("NTU-M8-418");
34. user2.setRoom(room2);
35. room2.setUser(user2);
36.
37. Transaction tx = session.beginTransaction();
38. session.save(user1);
39. session.save(user2);
40. tx.commit();
41. 42.
43. session.close();
44. sessionFactory.close();
45. }
46.}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved