Hibernate entry 23 - Multi-to-many physical images

xiaoxiao2021-03-06  43

Getting started 23 - Multi-to-many physical images

Suppose now has two categories of User and Server, one user can be authorized to use multiple Server, and the user who is authorized to use it on Server is also a multi-to-many relationships on both USER and SERVER. In the program design, it is basically not recommended to establish a multi-relational relationship directly between USER and Server, which will make User and Server dependent, usually through a mediation category to maintain a lot of multi-relationships between the two. Avoid interdependence of both. If you must directly establish multiple multi-relationships between User and Server, Hibernate is also supported. Basically, as long as you understand several entity images previously introduced, it is not difficult to establish multi-to-many associations in configuration. Let's take a look at the User and Server categories we designed:

User.java

Package online.

Import java.util. *;

PUBLIC CLASS User {

Private long id;

PRIVATE STRING NAME;

Private set servers = new hashset ();

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public set getServers () {

Return Servers;

}

Public void setServers (SET Servers) {

THIS.SERVERS = Servers;

}

}

Server.java

Package online.

Import java.util. *;

Public class server {

Private long id;

PRIVATE STRING ADDRESS

Private set users = new hashset ();

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getaddress () {

Return Address;

}

Public void setaddress (String address) {

THIS.ADDRESS = Address;

}

Public set getusers () {

Return User;

}

Public void setUsers (SET Users) {

THIS.USERS = User;

}

}

Each of this is used to save each other's relationship. On the multi-to-many-multi-relational map, we can establish a one-way or bidirectional relationship, which directly introduces the two-way relational image, and will be related to the Inverse = "True". Maintenance is maintained by one of them, such as the result, in the original code, but also compare the Java-related relationship maintenance, that is, both parties are set to the other party. Let's take a look at user.hbm.xml:

User.hbm.xml

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Table = "User_server"

Cascade = "Save-Update">

COLUMN = "server_id" />

In the database, the multi-to-multi-relationship between the data table is done through a data sheet, for example, in this example, the USER data table is one-to-many, and the user_server is more pairs of Server. To complete the multi-to-many relationships of USER to Server, in the user_server data table, the user_id is common as the primary key, user_id as a foreign key reference to the USER, and Server_ID is used as a foreign key reference to Server. Take a look at the server.hbm.xml mapping file:

Server.hbm.xml

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Table = "User_server"

Inverse = "true" cascade = "save-update">

COLUMN = "User_ID" />

Setting with user.hbm.xml, just adding inverse = "true", indicating that the maintenance of the relationship is added to the other end, note that we are set to Save-Update in User and Server, in pair In many relationships, all, delete and other cascade are meaningless, because many-to-many, do not be deleted because the parent object is deleted, but caused by the sub-objects being removed, because there may be other parent objects to this Child object. We use the following programs to test:

Hibernatetest.java

Import Onlyfun.caterpillar. *;

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Public class hibernatetest {

Public static void main (string [] args) throws hibernateException {

SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();

Server Server1 = new server ();

Server1.Setaddress ("PC-219");

Server Server2 = new server ();

Server2.SetAddress ("PC-220");

Server Server3 = New Server ();

Server3.Setaddress ("PC-221");

User USER1 = New user ();

User1.setname ("CATERPILLAR");

User USER2 = New user ();

User2.setname ("momor");

User1.getServers (). Add (server1);

User1.getServers (). Add (server2);

User1.getServers (). Add (server3);

Server1.getusers (). Add (user1);

Server2.getusers (). Add (user1);

Server3.getusers (). Add (user1);

User2.getServers (). Add (server1);

User2.getServers (). Add (server3);

Server1.getusers (). Add (user2);

Server3.getusers (). Add (user2);

Session session = sessionFactory.openSession ();

Transaction tx = session.begintransaction ();

Session.save (user1);

Session.save (user2);

TX.comMit ();

session.close ();

SessionFactory.Close ();

}

}

Note Since INVERSE = "True" is set, the mutual reference between USER and Server must be set separately, and see how actually stored in the database: mysql> select * from User;

-------- -----------

| User_id | NAME |

-------- -----------

| 1 | CATERPILLAR |

| 2 | MoMor |

-------- -----------

2 rows in set (0.00 sec)

MySQL> SELECT * from User_server;

----------- ---------

| Server_ID | User_ID |

----------- ---------

| 1 | 1 |

| 2 | 1 |

| 3 | 1 |

| 1 | 2 |

| 2 | 2 |

----------- ---------

5 rows in set (0.00 sec)

MySQL> Select * from server;

----------- ---------

| Server_ID | Address |

----------- ---------

| 1 | PC-219 |

| 2 | PC-221 |

| 3 | PC-220 |

----------- ---------

3 rows in set (0.00 sec)

For more examples, more examples and instructions, you can refer to Hibernate In Action 6.3.

转载请注明原文地址:https://www.9cbs.com/read-63522.html

New Post(0)