Hibernate的一对一关系[Java编程]
本文“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>
以上是“Hibernate的一对一关系[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |