Note: "Hibernate bidirectional 1->n association with connected tables"

Source: Internet
Author: User

Person associated with address: bidirectional 1->n, "Connected table", N-terminal control correlation

Person.java

Package org.crazyit.app.domain;

Import Java.util.HashSet;
Import Java.util.Set;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.OneToMany;
Import javax.persistence.Table;

@Entity
@Table (name = "Person_inf")
public class Person {

@Column (name = "person_id")
@Id
@GeneratedValue (strategy = generationtype.identity)
Private Integer ID;

private String name;

Private Integer age;

Two-way 1->n:1 to N correlation relation
Define all the associated address for the person entity
@OneToMany (targetentity = address.class,//
Mappedby = "person")//Specifies the Mappedby attribute, indicating that the person entity does not control the association relationship.
Private set<address> addresses = new hashset<address> ();

Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public set<address> getaddresses () {
return addresses;
}

public void setaddresses (set<address> addresses) {
This.addresses = addresses;
}

Public Integer Getage () {
return age;
}

public void Setage (Integer age) {
This.age = age;
}

}

Address.java

Package org.crazyit.app.domain;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import javax.persistence.JoinTable;
Import Javax.persistence.ManyToOne;
Import javax.persistence.Table;

@Entity
@Table (name = "Address_inf")
public class Address {

@Id
@Column (name = "address_id")
@GeneratedValue (strategy = generationtype.identity)
Private Integer ID;

Define the person entity associated with the address entity
@ManyToOne (targetentity = person.class)
@JoinTable (name = "Person_address",//Specify the connection table name
Specifies the ADDRESS_ID primary key column for the address_id column reference of the current entity's corresponding data table in the Join table
Joincolumns = @JoinColumn (name = "address_id",//
Referencedcolumnname = "address_id",//
Unique = True),//Add uniqueness Constraint (unique) To ensure address only corresponds to one person entity
The person_id column in the direct join table references the PERSON_ID primary key column of the corresponding data list for the current Entity Association entity
Inversejoincolumns = @JoinColumn (name = "person_id",//
Referencedcolumnname = "person_id")//
)
private person person;

Address details
Private String Addressdetail;

Non-parametric construction method
Public Address () {
}

Initialize all member variables
Public Address (String addressdetail) {
This.addressdetail = Addressdetail;
}

Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

Public String Getaddressdetail () {
return addressdetail;
}

public void Setaddressdetail (String addressdetail) {
This.addressdetail = Addressdetail;
}

Public Person Getperson () {
return person;
}

public void Setperson (person person) {
This.person = person;
}

}

#数据模型

Note: "Hibernate bidirectional 1->n association with connected tables"

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.