当前页面: 开发资料首页 → Eclipse 专题 → Eclipse快速上手Hibernate--5. 关联映射(一对一)
摘要: Eclipse快速上手Hibernate--5. 关联映射(一对一)
Person.java
/*
* Hibernate - 关联(Association)映射
* 创建日期 2005-4-25
* @author javamxj(分享java快乐)
* @link Blog: htpp://blog.csdn.net/javamxj/
* htpp://javamxj.mblogger.cn
*/
package javamxj.hibernate.association.one2one;
/**
* @hibernate.class table = "T_Person"
*/
public class Person {
private Long id;
private String username;
private Address address;
/**
* @hibernate.id
* generator-class="foreign"
* @hibernate.generator-param
* name="property"
* value="address"
*/
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
/**
* @hibernate.property
* length="15"
* not-null="true"
*/
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
/**
* @hibernate.one-to-one
* cascade="all"
*/
public Address getAddress() {return address;}
public void setAddress(Address address) {this.address = address;}
}
Address.java
package javamxj.hibernate.association.one2one;
/**
* @hibernate.class table = "T_Address"
*/
public class Address {
private String country;
private String city;
private String street;
private String zipCode;
private Long id;
public Address() {}
public Address(String country, String city, String street, String zipcode) {
super();
this.country = country;
this.city = city;
this.street = street;
this.zipCode = zipcode;
}
/**
* @hibernate.id
*generator-class="hilo"
*unsaved-value="null"
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* @hibernate.property
* length = "12"
*/
public String getCity() {return city;}
public void setCity(String city) {this.city = city;}
/**
* @hibernate.property
* length = "12"
*/
public String getCountry() {return country;}
public void setCountry(String country) {this.country = country;}
/**
* @hibernate.property
* length = "6"
*/
public String getZipCode() {return zipCode;}
public void setZipCode(String number) {this.zipCode = number;}
/**
* @hibernate.property
* length = "12"
*/
public String getStreet() {return street;}
public void setStreet(String street) {this.street = street;}
}Person.java
/*
* Hibernate - 关联(Association)映射
* 创建日期 2005-4-25
* @author javamxj(分享java快乐)
* @link Blog: htpp://blog.csdn.net/javamxj/
* htpp://javamxj.mblogger.cn
*/
package javamxj.hibernate.association.one2one;
/**
* @hibernate.class table = "T_Person"
*/
public class Person {
private Long id;
private String username;
private Address address;
/**
* @hibernate.id
*generator-class="hilo"
*unsaved-value="null"
*/
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
/**
* @hibernate.property
* length="15"
* not-null="true"
*/
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
/**
* @hibernate.many-to-one
* column = "fk_Address"
* cascade="all"
* unique="true"
*/
public Address getAddress() {return address;}
public void setAddress(Address address) {this.address = address;}
}