Hibernate one-to-one association

Source: Internet
Author: User

There are two ways to map to one to each: Primary key association and foreign Key Association

1. A primary key association is a one-to-one association that references the primary key of another table

1. Because of the primary key correlation, the primary key values of the associated records of the two tables associated with the primary key must be kept in sync. This means that we only need to set the primary key generator for one table and the primary key of the other table to share the same primary key value.

2. The query statement indicates that the primary key association uses a left outer join, which can be modified by modifying the Fetch attribute value in <one-to-one> to "select" To send a single SELECT statement each time (the <one-to-one of both ends of the association must be The fetch in > is set to "select" to be valid). However, this reduces efficiency (because multiple SELECT statements need to be sent).

3. The immediate load policy is used by default in the primary key association, and setting lazy= "proxy" in <one-to-one> cannot use the lazy load policy (that is, setting the Lazy property is not valid).

4. Description of the constrained Attribute (association constraint) in <one-to-one>: The--constrained property value defaults to False, and when constrained is false, the There is no referential relationship between the two generated tables, and the immediate load policy is used.

2. Foreign Key Association. is essentially a one-to-many degenerate form.

For a one-to-one affinity relationship for foreign key associations, the default load policy is lazy loading, changing it to load immediately:

--Change the value of the lazy property to False, if you do not want user= (User) session.get (User.class, 1L), or lazy loading, you will need to User.hbm.xml <many-to-one> Change the value of the property to false.

--Set the fetch attribute to "join", which is more efficient than lazy to false.

A one-way foreign key association is the same as a single-way foreign key association in the database table format, the difference is

Java program. Two-way foreign key associations can call each other through hibernate between two classes, while one-way foreign key associations can only be called in One direction.

The shared primary key method is to restrict the primary key of two data tables to use the same value, and to form a one-to-one mapping relationship through the primary key.

The only foreign key is a one-to-one mapping relationship between the foreign key of a table and the unique primary key of another table, which is actually a multi-pair special case.

Hibernate one-to-one association

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.