on A study Hibernate The basic mapping, that is, single-table mapping, it is easy to understand, but for the relational database, the existence of a relationship between tables is not very small, the relationship between the database is established through the main foreign key , the reaction to Hibernate How do you get through object performance? Let's continue learning about Hibernate 's Object-relational mapping.
Let's start with the most common many-to-one-to-many relationships:
Many-to-one
The so-called many-to-one, in the database is to add a foreign key at one end associated to the end, such as the user (User) and the group where the user is located (Group) Relationship: aUserbelong to only oneGroup, aGrouphave multipleGroup, and can be passedUsergets the location of theGroup. With the following class diagram and table relationshipsHibernatethe mapping implementation:
1, first the entity classUserand theGroup
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;//Associated Group properties (added in user entity Add user group, indicating that the group is visible through the user) 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) {this.group = group;}}
we are at one end of the many ( User ) to add Group Property Reference, so that you can pass the User Object gets Group object.
2, mapping file configuration
User Groups Group.hbm.xml
User User.hbm.xml
Through the configuration file we see that the implementation of a many-to-one relationship map is:
at one end of the many ( User ) using the following label mappings:
<many-to-onename= "group" column= "GroupID"/>
after the mapping succeeds, the following is the operation of the object (additional - persisted to the database), with the above mappings, we can Hibernate the cascade operation of the entity to quickly operate. But that's not enough, so here's the question of cascading operations. For example, we need to add users under User groups and groups:
Cascading issues:
Session = Hibernateutils.getsession (); Session.begintransaction (); Group Group = new Group (); Group.setname ("System Administrator"); User User1 = new user (); User1.setname ("Zhang San"); User1.setgroup (group); User User2 = New user (), User2.setname ("John Doe"), User2.setgroup (group), Session.save (user1); Session.save (user2);// The cleanup cache is an error transientobjectexception//because group is transient state, there is no session, no matching data in the database//and user is persistent state, Hibernate cannot find group object in cache while scavenging cache//conclusion: An object of the persistent state cannot refer to an object Session.gettransaction () of the transient state. commit ();
As the comment says, a transientobjectexception error occurs, and in the save object (user) process , the object has a property (foreign key) Another object that is not persisted (the Unsaved object group), the workaround is to save the previous object by saving the "associated" object, and of course we can achieve our needs, But hibernate provides a quicker way to See the Cascade operation and configuration of hibernate below.
Cascade Operationscascading is a cascading operation between objects that affects only additions, deletions, and modifications. Hibernate 's control of cascading is configured through cascade in the Relationship tab of the configuration file .
Also to add user groups and users as an example, we need to add to <many-to-one>:
<many-to-one name= "group" column= "GroupID" cascade= "Save-update"/>
"Cascade" is a description of the operation of two or more associated objects, and whether or not an object is associated with a similar operation when one of the objects is being manipulated. For example, we save the user whether or not they also save their group.
Cascading (Cascade) also has the following common properties:
(1) None: When the current object is saved, deleted, or modified, its subordinate objects (associated objects) are not cascaded. It is the default value.
(2) Save-update: When saving, updating the current object, Cascade Save, update the satellite object (temporary object, free object).
(3) Delete: Cascade deletes the satellite object when the current object is deleted.
(4) All: Cascade operations in all cases, including save-update and delete, and so on.
(5) Delete-orphan: Delete this object while deleting the orphan object that is associated with the current object (used only in a one-to-many association relationship).
These we can set according to the specific business.
Summarize
Hibernate relationship mapping reaction is the relationship between the class, the relational database is the primary foreign key relationship, database and UML we are very familiar with the mapping is not a problem, of course, the relationship mapping in Hibernate is the most basic content, project practice is essential. The following continues hibernate's one-to-many mappings (including bidirectional mappings).
Hibernate mapping (ii)--many-to-one relationship mapping