Getting started 22 - a pair of entity images
Suppose we have previous examples of User and Room are one-on-one relationship, that is, everyone allocates a room, first look at these two categories:
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.
Public class room {
Private long id;
PRIVATE STRING ADDRESS
PRIVATE User;
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 user getuser () {
Return User;
}
Public void setuser (user user) {
THIS.USER = User;
}
}
To image the one-on-one relationship between User and Room, we can have two ways, one is through foreign key reference, in the previous multi-to-one example, even foreign key reference, we are now limited to one One-to-one, as long as the unique = "true" on the
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
COLUMN = "room_id" Class = "Onlyfun.caterpillar.Room" Cascade = "all" Unique = "true" /> clas> hibernate-maping> This completes a one-way one-to-one mapping, we can add a reference back to User settings on Room.hbm.xml, making it a two-way one-to-one mapping, as follows: Room.hbm.xml XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> id> Class = "Onlyfun.caterpillar.user" Property-ref = "room" /> clas> hibernate-maping> In the 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"); User1.setroom (room); Room.Setuser (user1); Session session = sessionFactory.openSession (); Transaction tx = session.begintransaction (); Session.save (user1); TX.comMit (); session.close (); SessionFactory.Close (); } } The actual example of the data sheet is the same as the multi-to-one image, but now a USER can only correspond to one Room. Another one-to-one way is to use the primary key to limit the primary key of the two data tables, so that the user and Room is one-on-one relationship, in user.hbm.xml, as long as User.hbm.xml XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> id> Property> Class = "Onlyfun.caterpillar.Room" Cascade = "all" /> clas> In Room.hbm.xml, the primary key must be restricted to the primary key of the User, and on the property, use constrained = "true" to tell Hibernate reference to the primary key: Room.hbm.xml XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> User param> generator> id> Class = "Onlyfun.caterpillar.user" Constrained = "true" /> clas> hibernate-maping> As long as the image file can be changed, the actual storage example in the database does not need to be modified, and the actual storage example in the database is as follows: MySQL> Select * from user; -------- ----------- | User_id | NAME | -------- ----------- | 1 | Bush | | 2 | CATERPILLAR | -------- ----------- 2 rows in set (0.00 sec) Mysql> Select * from room; ------- ---------- | ROOM_ID | Address | ------- ---------- | 1 | NTU-M8-419 | | 2 | NTU-M8-420 | ------- ---------- 2 rows in set (0.00 sec)