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