One-to-one foreign key two-way join table Creation

Source: Internet
Author: User

First, there are two classes. In China, they adopt a unique wife, that is, one husb and can only correspond to one wife.

Wife class. The generated table is t_wife.

/** *  */package com.maybe.test_1;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.Table;/** * @author MayBe * * function: */@Entity@Table(name="t_wife")public class Wife {private Integer id;private String name;private Husband husband;@Id@GeneratedValuepublic Integer getId() {return id;}public String getName() {return name;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}@OneToOnepublic Husband getHusband() {return husband;}public void setHusband(Husband husband) {this.husband = husband;}}
Husband class. The generated table is t_husband.

/** *  */package com.maybe.test_1;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;/** * @author MayBe * * function: */@Entity@Table(name="t_husband")public class Husband {private Integer id;private String name;private Wife wife;@Id@GeneratedValuepublic Integer getId() {return id;}public String getName() {return name;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}@OneToOnepublic Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}}
Because it is a one-to-one bidirectional Association, both the wife class and the husband class contain references of the other party.

The hibernate. cfg. xml file is as follows:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

Use Junit for testing. The table creation statement is as follows:

Drop table t_wife

Create table t_husband (
Id int identity not null,
Name varchar (255 ),
Wife_id int,
Primary key (id)
)

Create table t_wife (
Id int identity not null,
Name varchar (255 ),
Husband_id int,
Primary key (id)
)

Alter table t_husband
Add constraint FK_hxagmw4p5aym8m63pxd6h2wwx
Foreign key (wife_id)
References t_wife

Alter table t_wife
Add constraint FK_fi3kodkmubgryyblf4935y4dk
Foreign key (husband_id)
References t_husband


It is not hard to see that this One-to-one relationship is redundant, and the design of this table is obviously problematic because One-to-one is automatically generated by hibernate when we use annotations, the solution is very simple. You only need to add an attribute to the annotation, such as the husb and class:

/** *  */package com.maybe.test_1;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;/** * @author MayBe * * function: */@Entity@Table(name="t_husband")public class Husband {private Integer id;private String name;private Wife wife;@Id@GeneratedValuepublic Integer getId() {return id;}public String getName() {return name;}public void setId(Integer id) {this.id = id;}public void setName(String name) {this.name = name;}@OneToOne(mappedBy="husband")public Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}}

We use the mappedby attribute in onetoone (usually used in bidirectional associations), specify mappedBy, and specify the owner of the ing relationship (husband in the Wife class ), the generated data table is shown below:

Create table t_husband (
Id int identity not null,
Name varchar (255 ),
Primary key (id)
)

Create table t_wife (
Id int identity not null,
Name varchar (255 ),
Husband_id int,
Primary key (id)
)

Alter table t_wife
Add constraint FK_fi3kodkmubgryyblf4935y4dk
Foreign key (husband_id)
References t_husband

The table generated this time is not redundant. In the wife table, there is a foreign key husband_id, which is used to maintain the contact between the two objects. Therefore, the mappedBy attribute must be added to the two-way relationship to ensure the correctness of the database table.

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.