The use of property-ref in Hibernate is often used to solve the one-to-least relationship between legacy databases.

Source: Internet
Author: User

1. For example, table class (classid, class_no, classname) and student (studentid, studentname, class_no ),
Classid and studentid are primary keys.
The two tables are one-to-many relationships, and the two tables must be associated through classno.
In general, the data is stored in the student table as a foreign key through classid.
2. The specific hibernate configuration file is as follows:
Class. HBM. xml:
<Property
Name = "classno"
Type = "Java. Lang. String"
Column = "class_no"
Length = "30"
/>
<! -- Associations -->
<Set name = "Students"
Lazy = "false"
Inverse = "true"
Cascade = "all-delete-Orphan"
>

<Key column = "class_no" property-ref = "classno"/>

<One-to-least
Class = "student"
/>
</Set>

Student. HBM. xml:

<Role-to-one
Name = "class"
Class = "class"
Not-null = "true"
Property-ref = "classno"
>
<Column name = "class_no"/>
</Role-to-one>

3. Notes:
The property-ref attribute is generally used to solve the problem of the one-to-one relationship between legacy databases.
Property-ref (optional) Name of the corresponding property in the class associated with the other key. If not specified, the primary key of the associated class is used.
Property-ref = whether it is the field name in the database table, but the attribute name in the defined Java class. Be sure to pay attention to it.

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.