Make a small note of your understanding of these two attributes.
If there are two classes of
Class group{
private int id;
Private set<user> users = new ...
}
Class user{
private int id;
Private group group = new.
}
They're a one-to-many relationship. Group (1)---> User (N)
The inverse property means whether to maintain the relationship between them, in other words, whether to set the value of the foreign key group_id
When we are in
Group.hbm.xml
<set name= "Users" >
</set>
Default inverse = False that group is a maintenance relationship
So
User U = new user ();
Group G = new Group ();
Set set = new Hshset ();
Set.add (U);
G.setset (set);
Last Session.save (g); You can do it.
However, when you find that you are throwing an exception, it leads
Cascade the Cascade
Because group is associated with user, user does not exist when saving group, so group wants to set the value of foreign key group_id, because the database does not exist this user record
Unless you Session.save (U) before saving the group
or set
Cascade = "Save-update"
So that it can be saved successfully.
A particular point to note is that
There is no inverse on the <many-to-one> side because hibernate requires one party to maintain the relationship.
Cascade does not affect
All of the above code is tapped. Some errors: It's good to understand the two attributes.
Chatting with inverse and cascade in hibernate