Hibernate ing (2) -- Multi-to-one ing
The previous article learned the basic Hibernate ing of Hibernate, that is, single-Table ing, which is easy to understand. However, for relational databases, the relationship between tables is indispensable, the relationships in relational databases are established through the primary and Foreign keys. How does Hibernate express its performance through objects? Next we will continue to learn the object relationship ing of Hibernate.
Let's start with the most common many-to-one and one-to-many relationships:
Many-to-one
A multi-to-one interface is used to add a foreign key to one end in a database. For example, the relationship between a User and a Group is as follows: A User only belongs to one Group, and a Group has multiple groups. You can use the User to obtain the Group. The following class diagram and table relationship show the implementation of Hibernate ing:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8aDI + MaGiytfPyMrHyrXM5cDgVXNlcrrNR3JvdXA8L2gyPgo8cHJlIGNsYXNzPQ = "brush: java;"> package tgb. hibernate;/** User Group */public class Group {private int id; private String name; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}} package tgb. hibernate;/*** User * @ author Jones **/public class User {private int id; private String name; // associate User group attributes (add User groups in User entities, indicates that the user can see the Group) private group Group; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Group getGroup () {return group;} public void setGroup (Group group Group) {this. group = group ;}}
We add Group attribute reference at multiple ends (users), so that the Group object can be obtained through the User object.
2. ing file configuration
User Group. hbm. xml
User. hbm. xml
Through the configuration file, we can see that the implementation of multi-to-one relationship ing is:
Use the following tag ing at multiple ends (users:
After the ing is successful, operations on the object (adding, deleting, modifying, querying, and persisting to the database) will follow. With the above ing, we can perform quick operations on the object through the Hibernate cascade operation. However, this is not enough. Here we will look at the problem of cascade operations. For example, we need to add users in the user group and group:
Cascade problems:
Session = HibernateUtils. getSession (); session. beginTransaction (); Group group = new Group (); group. setName ("System Administrator"); User user1 = new User (); user1.setName ("zhangsan"); user1.setGroup (group); User user2 = new User (); user2.setName ("Li Si"); user2.setGroup (group); session. save (user1); session. save (user2); // The error TransientObjectException occurs when the cache is cleared. // The Group is in Transient status and is not session, no matching data in the database // and the User is in the Persistent state. hibernate cannot find the Group object in the cache when clearing the cache // conclusion: objects in the Persistent State cannot reference objects in the Transient state session. getTransaction (). commit ();
As mentioned in the comment, a TransientObjectException error occurs. During the object (user) saving process, the object has an attribute (foreign key) another non-Persistent Object (unsaved object group) Associated. The solution is to first save the object "associated" to save the previous object, of course, we can achieve our needs in this way, but Hibernate provides a more efficient way. Next we will look at the cascading operation and configuration of Hibernate.
Cascade operations
Cascading is a chained operation between objects. It only affects addition, deletion, and modification. Hibernate uses cascade to configure cascading in the link tag of the configuration file.
For example, to add user groups and users Add:
"Cascade" indicates whether to perform similar operations on the associated objects when two or more associated objects are operated on an object. For example, whether or not the user is saved in the group.
Cascade also has the following common attributes:
(1) none: when saving, deleting or modifying the current object, cascade operations on its affiliated objects (associated objects) are not performed. It is the default value.
(2) save-update: when saving and updating the current object, cascade save and update the affiliated object (temporary object, free object ).
(3) delete: when the current object is deleted, the secondary object is deleted cascade.
(4) all: cascade operations in all cases, including save-update and delete operations.
(5) delete-orphan: delete this object and delete the orphan object that is not associated with the current object (only used in One-to-multiple associations ).
You can set these parameters based on your business.
Summary
The relationship ing in Hibernate reflects the relationship between classes, and the relationship between primary and Foreign keys in the relational database. We are familiar with both the database and UML, so there is no problem with ing, of course, link ing is the most basic content in Hibernate, and project practice is essential. Next we will continue the one-to-multiple Hibernate ing (including bidirectional ing) of Hibernate ).