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

hibernate3學習筆記(二十)|關系映射:多對多

編輯:關於JAVA

要實現多對多的對應,一般可以借由一個中間表來完成。也就是借由一對多,多對一來完成。

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 user_server (
7. user_id INT(11),
8. server_id INT(11)
9.);
10. 11.CREATE TABLE server (
12. id INT(11) NOT NULL auto_increment PRIMARY KEY,
13. address VARCHAR(100) NOT NULL default '' 14.);

User.java

1.package com.hb3.pack_19.model;
2.
3.import java.util.Set;
4. 5.public class User {
6.
7. private Integer id;
8. private String name;
9. private Set<Server> servers;
10.
11. public User() {
12. }
13. 14. public Integer getId() {
15. return id;
16. }
17. public void setId(Integer id) {
18. this.id = id;
19. }
20. public String getName() {
21. return name;
22. }
23. public void setName(String name) {
24. this.name = name;
25. }
26. public Set<Server> getServers() {
27. return servers;
28. }
29. public void setServers(Set<Server> servers) {
30. this.servers = servers;
31. }
32.}

Server.java

1.package com.hb3.pack_19.model;
2.
3.import java.util.Set;
4. 5.public class Server {
6. 7. private Integer id;
8. private String address;
9. private Set<User> users;
10.
11. public String getAddress() {
12. return address;
13. }
14. public void setAddress(String address) {
15. this.address = address;
16. }
17. public Integer getId() {
18. return id;
19. }
20. public void setId(Integer id) {
21. this.id = id;
22. }
23. public Set<User> getUsers() {
24. return users;
25. }
26. public void setUsers(Set<User> users) {
27. this.users = users;
28. }
29.}

Server.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_19.model.Server" table="server">
10. 11. <id name="id" column="id">
12. <generator class="native"/>
13. </id>
14. 15. <property name="address" type="java.lang.String"/>
16.
17. <set name="users"
18. table="user_server"
19. inverse="true"
20. cascade="save-update">
21.
22. <key column="server_id"/>
23. <many-to-many class="com.hb3.pack_19.model.User"
24. column="user_id"/>
25. </set>
26. </class>
27. 28.</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_19.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. <set name="servers"
18. table="user_server"
19. cascade="save-update">
20.
21. <key column="user_id"/>
22. <many-to-many class="com.hb3.pack_19.model.Server"
23. column="server_id"/>
24. </set>
25. </class>
26. 27.</hibernate-mapping>

測試代碼:

1.package com.hb3.pack_19;
2.
3.import java.io.IOException;
4.import java.sql.SQLException;
5.import java.util.HashSet;
6. 7.import org.hibernate.Session;
8.import org.hibernate.SessionFactory;
9.import org.hibernate.Transaction;
10.import org.hibernate.cfg.Configuration;
11. 12.import com.hb3.pack_19.model.Server;
13.import com.hb3.pack_19.model.User;
14. 15.public class BusinessService {
16. 17. public static void main(String[] args) throws IOException, SQLException {
18.
19. Configuration config = new Configuration().configure();
20. SessionFactory sessionFactory = config.buildSessionFactory();
21. Session session = sessionFactory.openSession();
22. 23.
24. Server server1 = new Server();
25. server1.setAddress("PC-219");
26. server1.setUsers(new HashSet<User>());
27. 28. Server server2 = new Server();
29. server2.setAddress("PC-220");
30. server2.setUsers(new HashSet<User>());
31.
32. Server server3 = new Server();
33. server3.setAddress("PC-221");
34. server3.setUsers(new HashSet<User>());
35.
36. User user1 = new User();
37. user1.setName("shenbin");
38. user1.setServers(new HashSet<Server>());
39.
40. User user2 = new User();
41. user2.setName("chenyan");
42. user2.setServers(new HashSet<Server>());
43. 44. user1.getServers().add(server1);
45. user1.getServers().add(server2);
46. user1.getServers().add(server3);
47. server1.getUsers().add(user1);
48. server2.getUsers().add(user1);
49. server3.getUsers().add(user1);
50. 51. user2.getServers().add(server1);
52. user2.getServers().add(server3);
53. server1.getUsers().add(user2);
54. server3.getUsers().add(user2);
55.
56. Transaction tx= session.beginTransaction();
57. session.save(user1);
58. session.save(user2);
59. tx.commit();
60. 61. 62. session.close();
63. sessionFactory.close();
64. }
65.}

但是,多對多映射由於使用了中介表格,查詢效率不高。這不是一個很好的設計方式,應盡量避免多對多關聯的設計。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved