Hibernate Getting Started 19 - a pair of multi-physical images

xiaoxiao2021-03-06  42

Getting started 19 - a pair of multi-physical images

In the previous topic, User is multi-object to Room, in turn, Room is one-to-many, one Room can use multiple User accommodation, our User category is designed as follows:

User.java

Package online.

PUBLIC CLASS User {

Private long id;

PRIVATE STRING NAME;

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;

}

}

This time does not include the ROOM property in the user, we use set in the ROOM category to store User, indicating that the user accommodation in 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;

}

}

About User mapping file User.hbm.xml is as follows:

User.hbm.xml

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

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

The Room's mapping file Room.hbm.xml is as follows, we use to make a collection image, and use to specify a pair of multi-relationships for User: room.hbm.xml

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

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

We use the following program to test the storage of data:

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 ("CATERPILLAR");

Room.getusers (). Add (user1);

Room.getusers (). Add (user2);

Session session = sessionFactory.openSession ();

Transaction tx = session.begintransaction ();

Session.save (user1);

Session.save (user2);

Session.save (room);

TX.comMit ();

session.close ();

SessionFactory.Close ();

}

}

Take a look at the content actually stored in the database:

MySQL> Select * from user;

------- ------------- --------- | User_id | name | room_id |

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

| 1 | Bush | 1 |

| 2 | CATERPILLAR | 1 |

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

2 rows in set (0.00 sec)

Mysql> Select * from room;

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

| ROOM_ID | Address |

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

| 1 | NTU-M8-419 |

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

1 row in set (0.00 sec)

In this side, we will lead two themes first, the first is every time we use save () store all the temporary objects? In fact, we will simplify only Save (Room), which can simplify only SAVE (Room), and USER1, USER1, which is also automatically stored. The second is that the relationship between User and Room We now only design one-way, in fact, we will design the two-way association between them. These two topics are worth discussing independent, and will later introduce.

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

New Post(0)