程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Hibernate的一對一關系

Hibernate的一對一關系

編輯:JAVA編程入門知識

  一對一關系的維護有兩種,分為主鍵關聯和唯一外鍵關聯

  一對一關系的維護有兩種,分為主鍵關聯和唯一外鍵關聯

  主鍵關聯:(雙向)

  

(主控方)
Person.java
Person {
private String username;
//對被控方的引用
private Address address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address= address;
}
}
(被動方)
Address.java
Address{
private String addressName;
//對主控方的引用
private Person person;
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName= addressName;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person= person;
}
public String toString() {
return new ToStringBuilder(this).
append("addressName", addressName).
toString();
}
public boolean equals(Object o) {
if ( !(o instanceof Address) {
return false;
}
Address rhs = (Address) o;
return new EqualsBuilder()
.appendSuper(super.equals(o))
.append(this.addressName, rhs.getAddressName())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder(17, 37).
append(addressName).
toHashCode();
}
}
mapping 文件
<class name="Person" table="person">
<id name="id" type="long" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<one-to-one
name="address"
class="Address"
cascade="all"
constrained="false"
/>
</class>
<class name="Address" table="Address" >
<id name="id" column="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" class="Person" constrained="true"/>
<property name="addressName" column="addressName" type="string"/>
</class>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved