Hibernate one-to-multiple bidirectional Association
When testing one-to-multiple bidirectional Association, some questions are raised, which are related to the inverse attribute.
1. Database Structure
Customer table: {customerid, customername}
Orders table: {orderid, ordername, idcustomer}, where idcustomer is a foreign key, indicating the customer ID.
(I used the ms SQL Server 2000 Personal Edition. At the beginning, I created a new order table, and the results always reported an error, saying there was a syntax error near 'order, later I realized that order is the keyword of the database. Therefore, do not duplicate names with database keywords !)
2. Customer. HBM. xml
<Hibernate-mapping>
<Class name = "com. Persistent. Customer" table = "customer">
<ID name = "ID" column = "customerid" unsaved-value = "null">
<Generator class = "increment"> </generator>
</ID>
<Property name = "name" column = "customername"> </property>
<Set name = "orders" cascade = "all" lazy = "false">
<Key column = "idcustomer"> </key>
<One-to-operate class = "com. Persistent. Order"/>
</Set>
</Class>
</Hibernate-mapping>
3. Order. HBM. xml
<Hibernate-mapping>
<Class name = "com. Persistent. Order" table = "orders">
<ID name = "ID" column = "orderid" unsaved-value = "null">
<Generator class = "increment"> </generator>
</ID>
<Property name = "name" column = "ordername"> </property>
<Role-to-one name = "customer"
Column = "idcustomer"
Class = "com. Persistent. Customer"
Lazy = "false"
Cascade = "all">
</Role-to-one>
</Class>
</Hibernate-mapping>
4. Customer. Java and order. Java
Public class customer ...{
Private long ID;
Private string name;
Private set orders = new hashset ();
...
}
Public class order ...{
Private long ID;
Private string name;
Private customer;
...
}
5. Results
When testing the inverse attribute of <set/>, it is found that whether inverse is true or false, saving the customer or order separately can be performed correctly. It seems that inverse does not play a role at this time, which is a bit confusing, is the ing file incorrect ?!
**************************************** **************************************** *********