One-to-many bidirectional association: User corresponds to multiple order
User.xml:
<class name= "Com.examp.ShoppingOnline.User"
table= "User"
Discriminator-value= "C" >
<id name= "id" >
<generator class= "Increment"/>
</id>
<property name= "username"/>
<property name= "Password"/>
<property name= "Realname"/>
<property name= "Tel"/>
<property name= "Address"/>
<property name= "Zip"/>
<property name= "Email"/>
<set name= "Orders" table= "Orders" cascade= "All" inverse= "true" >
<key column= "user_id"/>
<one-to-many class= "Order"/>
</set>
</class>
Order.xml:
<class name= "Com.examp.ShoppingOnline.Order"
table= "Orders"
Discriminator-value= "C" >
<id name= "id" >
<generator class= "Increment"/>
</id>
<property name= "OrderNo"/>
<property name= "Realname"/>
<property name= "Address"/>
<property name= "Zip"/>
<property name= "Tel"/>
<property name= "Payment"/>
<property name= "Email"/>
<property name= "Memo"/>
<property name= "Price"/>
<property name= "Time"/>
<property name= "Tag"/>
<many-to-one name= "user" class= "user" column= "user_id" cascade= "All"/>
</class>
Note that if one of the parties does not add inverse= "true", it can be more normal, but deleting will cause an error.
The inverse in hibernate are often applied in table relational mappings,
There are two kinds of values for inverse, "true" and "false". Inverse= "False" is the default value, and if set to TRUE indicates that the object's state change is not synchronized to the database; false is the opposite;
Inverse's role: in Hibernate, the inverse setting determines who maintains the relationship between tables and tables.
We say that the improper establishment of inverse will lead to poor performance, in fact, it is said that inverse improper establishment, will produce redundant SQL statements or even cause the JDBC exception throw. This is where we need to be concerned when building entity-class relationships. In general, Inverse=true is the recommended use, two-way association between the two sides set Inverse=false, will cause both sides to repeat the same relationship update. But if both sides set up inverse=true words, both sides do not maintain the update of the relationship, this is also not good, fortunately, one end of the many: Many-to-one default is Inverse=false, to avoid the generation of such errors. But many-to-many will not have this default setting, so many people often use inverse=true on both ends of the many-to-many, resulting in the connection table data is not recorded at all, because they are not responsible for the two points to maintain the relationship. So, the best thing about bidirectional correlation is that one end is inverse=true and the other side is inverse=false. General Inverse=false will put on more than one end, then someone asked, many-to-many on both sides are more, inverse exactly where? In fact, hibernate establishes many-to-many relationships and separates them into two one-to-many relationships, connecting a connection table in the middle. So the general existence of a one-to-many relationship, it can also be said: A pair of many is the basic component of many.
Cascade defines a cascade relationship between objects on both sides of a relationship, whereas inverse defines a relationship and an object's cascading relationship.
Inverse is valid only for Set+one-to-many (or many-to-many), not for Many-to-one, one-to-one. Cascade is valid for relationship tokens.
Inverse works on the collective object as a whole, cascade one element in the collection object, and if the collection is empty, Cascade does not raise the associated operation.
So if inverse is not set at User.hbm.xml, it defaults to false, then it refreshes the cascade relationship because USER_ID is
The order table is set, so the user_id=null will appear, so it will error.
When the User.hbm.xml setting inverse= "true", it will not be updated to update the Cascade relationship, you can directly enter the database level operation data, so the above delete the user at the same time also the Cascade relationship of the article deleted.
Hibernate cascade Delete