Note: "Foreign key-based" hibernate one-way 1->1 Association

Source: Internet
Author: User

Person associated with address: one-way 1->1, "foreign key-based"

Person.java

Package org.crazyit.app.domain;

Import Javax.persistence.CascadeType;
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.OneToOne;
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;

One-way 1->1: single-Connection relationship
Define the address entity associated with the person entity
@OneToOne (targetentity = address.class,//Specify associated object
Cascade = cascadetype.all//Specifies cascade mode
)
Map a foreign key column named address_id, referencing the ADDRESS_ID primary key column of the associated entity's corresponding table
@JoinColumn (name= "address_id",
Referencedcolumnname= "address_id", Unique=true)
private address 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 Address getaddress () {
return address;
}

public void setaddress (address address) {
this.address = address;
}

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.Table;

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

@Id
@GeneratedValue (strategy=generationtype.identity)
@Column (name= "address_id")
Private Integer ID;
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;
}

}

Data Model:

Note: "Foreign key-based" hibernate one-way 1->1 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.