Hibernate的一对一关系

2008-02-23 09:54:59来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

一对一关系的维护有两种,分为主键关联和唯一外键关联

一对一关系的维护有两种,分为主键关联和唯一外键关联

主键关联:(双向)
(主控方)

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>

上一篇: [学习小记]Java的反射机制
下一篇: 列出当前系统的硬盘盘符

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:我的J2ME编程联系(3)——Gauge

下一篇:Tomcat 5.5.7配置一个APP问题