Getting started 18 - Multi-to-one physical image
A entity is simple to say that there is a table object in the database, and has a database identification (Database Identity), the previously introduced Component object is not an entity, it has no self-owned database recognition, specific, it doesn't have ID attributes, Hibernate does not give it an ID value. The relationship between the entity and the entity is: one-to-one, more one, one, more, more. Among them, many of the most common entity relationships, give a simple example, suppose you write a dormitory management system, in general, the relationship between the tenant and the room is a multi-to-one relationship, because a room It can be assigned to multiple people (student dormitory, it should generally be two to eight people, don't wait! Look at the school's environment). If you are represented by the program, first look at the Room category, we only set the address attribute in it:
Room.java
Package online.
Public class room {
Private long id;
PRIVATE STRING ADDRESS
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;
}
}
Note that this category is different from the Component object. It has ID attributes, in the stored database, Hibernate will give it a value; the guest's words we design a User category:
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;
}
}
In Java, a Room object can be referenced by multiple User objects, that is, the relationship of User's relationship is multi-to-one relationship, we can also design Room's relationship with User, designed it as a one more. This is discussed in the next topic. At this stage, we will pay attention to multi-to-one mapping, our room map file Room.hbm.xml is simple, 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>
clas>
hibernate-maping>
Come back by user.hbm.xml, we use
User.hbm.xml
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
COLUMN = "room_id" Class = "Onlyfun.caterpillar.Room" /> clas> hibernate-maping> The USER table corresponding to the USER is a reference to the ROOM table, of course, the most important thing is not forgotten to specify the location and name of the image file in hibernate.cfg.xml: Hibernate.cfg.xml XML Version = '1.0' encoding = 'big5'?>>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> ...... Hibernate-Configuration> We use the following procedure to simply test the results of the store: 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 Room1 = New Room (); ROOM1.SETADDRESS ("NTU-M8-419"); Room Room2 = New Room (); Room2.Setaddress ("NTU-G3-302"); User USER1 = New user (); User1.setname ("bush"); User1.setroom (ROOM1); User USER2 = New user (); User2.setname ("CATERPILLAR"); User2.setroom (ROOM1); User USER3 = New user (); User3.setname ("momor"); User3.setroom (ROOM2); Session session = sessionFactory.openSession (); Transaction tx = session.begintransaction (); Session.save (room1); Session.save (room2); Session.save (user1); Session.save (user2); Session.save (user3); TX.comMit (); session.close (); SessionFactory.Close (); } } Take a look at what is stored in the database: MySQL> Select * from user; -------- ----------- --------- | User_id | Name | Room_ID | -------- ----------- --------- | 1 | Bush | 1 | | 2 | CATERPILLAR | 1 | | 3 | MoMor | 2 | -------- ----------- --------- 3 rows in set (0.00 sec) Mysql> Select * from room; ------- ---------- | ROOM_ID | Address | ------- ---------- | 1 | NTU-M8-419 | | 2 | NTU-G3-302 | ------- ----------