Hibernate one-to-multiple bidirectional Association

Source: Internet
Author: User

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 ?!

**************************************** **************************************** *********

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.