HIBERNATE,JPA Object Association Mapping

Source: Internet
Author: User
Keywords Hibernate relational mapping JPA objects
Tags address class example key listing name objects public class

The correlation mapping is a complex mapping relationship in the mapping relationship, which has a pair of more than one or one pairs and many pairs of relationships. They are subdivided into one-way and two-way points. Let's introduce each of the following.

One-way Onetoone

One-way one-to-one is the simplest of the relational mappings, simply that you can query the other party from the associated side, but not the reverse. We use the example below to illustrate that the person entity class in Listing 1 and the address class in Listing 2 are one-way one-to-one relationships where we can query the contents of a person's corresponding address, but we cannot Value to query for the person who corresponds to this value.

Listing 1. Owning side of one-way one-to-one relationship

@Entity public class Person implements Serializable {private static final long serialversionuid = 1L; @Id @GeneratedValue ( Strategy = Generationtype.auto) private Long ID; Private String http://www.aliyun.com/zixun/aggregation/11696.html ">name; private int age; @OneToOne private address address; Getters & Setters}

Listing 2. The opposite end of one-way one-to-one relationship

@Entity public class address implements Serializable {private static final long serialversionuid = 1L; @Id @GeneratedValue ( Strategy = Generationtype.auto) private Long ID; Private String Street; Private String City; Private String country; gettes& Setters}

Figure 1. ER diagram of one-way one-to-one relationship

As you can see from their ER diagram in Figure 1, this one-way one-to-one relationship is mapped in the database as a form of a key outside of the bond. Where the sender of the relationship stores a foreign key that points to the receiving end of the relationship. In this example, the address_id in the person table is a foreign key that points to the address, and by default the field name of the foreign key is made up of the name of the table to which it points "_" Plus "ID". Of course, we can also modify the field according to our preferences, the modified method is to use @JoinColumn this annotation. In this example we can add this annotation to the address attribute in the person class.

Related Article

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.