Getting Started with Hibernate 21 - Two - way association with Inverse Settings

xiaoxiao2021-03-06  39

Getting Started 21 - Two - way association with Inverse Settings

We have previously made a one-way pair of unidirectional and contraders in User and Room, we can also let the user with each other, form a two-way association, to the use of the Room object, specifically, Design as follows:

User.java

Package online.

PUBLIC CLASS User {

Private long id;

PRIVATE STRING NAME;

PRIVATE ROOM

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 room getroom () {

Return Room;

}

Public void setroom (room room) {

THIS. ROOM = Room;

}

}

Room.java

Package online.

Import java.util. *;

Public class room {

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;

}

}

And the corresponding mapping files are as follows, first is user.hbm.xml:

User.hbm.xml

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

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

COLUMN = "room_id"

Class = "Onlyfun.caterpillar.Room" />

Come back Room.hbm.xml:

Room.hbm.xml

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

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

This forms a two-way associated image between User and Room, we can 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 ();

Room Room = New Room ();

Room.SetAddress ("NTU-M8-419");

User USER1 = New user ();

User1.setname ("bush");

User USER2 = New user ();

User2.setname ("bush");

/ *

* Because INVERSER is not set, it is only necessary to maintain from Parent.

* /

//User1.setroom (ROOM);

//User2.setroom (ROOM);

Room.getusers (). Add (user1);

Room.getusers (). Add (user2);

Session session = sessionFactory.openSession (); Transaction tx = session.begintransaction ();

Session.save (room);

TX.comMit ();

session.close ();

SessionFactory.Close ();

}

}

Basically, in the storage of data, this is enough, but such a design will effectively problem, obviously, this program will be maintained by the association between Room and User, and if Room is said to be Self-saved, then store the plurality of USERs it included, then the association of each USER update (Update), in particular, this program must implement the following SQL:

Hibernate: INSERT INTO ROOM (Address, Room_id) VALUES (?,?)

Hibernate: INSERT INTO User (Name, Room_ID, User_ID) VALUES (?,?,?)

Hibernate: INSERT INTO User (Name, Room_ID, User_ID) VALUES (?,?,?)

Hibernate: Update user set room_id =? Where user_id =?

Hibernate: Update user set room_id =? Where user_id =?

For Room, it doesn't know that the user is not a stored object, or even if it has been stored objects, I don't know if the ROOM_ID on the USER table is not a reference to the ROOM table, so it must Update for your own User object to ensure that Room_ID on the USER table points to yourself. If the associated maintenance is given to the user, it is easier because each user corresponds to a Room, and when it is stored, each object in the set must be checked, in order to hand over the associated maintenance to the User. We can modify in in room.hbm.xml, plus inverse = "true", indicating that the associated maintenance "reverse" is handed over to the USER:

Room.java

Since the associated maintenance is handed over to User, we must be stored when it is stored, it is clear that Room is set to User, that is, it must be made:

/ *

* Because there is user maintenance association, you must call SETROOM

* /

User1.setroom (room);

User2.setroom (room);

Room.getusers (). Add (user1);

Room.getusers (). Add (user2);

This is more than the inverse = "true" sets more than a specified action, you must multi-key words, the increase in efficiency, Hibernate's persistent layer administrator will store Room, then Save User, so you can save the update action, specifically, that is, the following SQL: Hibernate: Insert Into Room (address, room_id) Values ​​(?,?)

Hibernate: INSERT INTO User (Name, Room_ID, User_ID) VALUES (?,?,?)

Hibernate: INSERT INTO User (Name, Room_ID, User_ID) VALUES (?,?,?)

Different from previously, since the association is given to the user maintenance, this Room does not need one by one by one, to determine each room_id pointing from its own. If INVERSE = "True" is specified, how do you specify Room to User? Then User and Room will be stored, but they have not been associated with each other, that is, the USER will not refer to Room, the User table Room_ID will be NULL, for example::

MySQL> Select * from user;

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

| User_id | Name | Room_ID |

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

| 1 | Bush | NULL |

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

Mysql> Select * from room;

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

| ROOM_ID | Address |

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

| 1 | NTU-M8-419 |

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

As a summary, when setting up a two-way association, the association is maintained by many "more", which is mostly maintained by the "one", which can be set by INVERSE, no The fixed inverse can basically run, but the performance will be poor. Setting Inverse, you must set the reference of both parties. With this topic, Room is to be set to User, and User must know the existence of Room, which is more than setting Inverse needs to type more words. However, from another aspect, compare the intuition of the programming (single view of the User and Room categories, both of which should be explicit).

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

New Post(0)