Hibernate的一对一关系(转)
2008-01-05 09:28:57 来源:WEB开发网核心提示:一对一关系的维护有两种,分为主键关联和唯一外键关联一对一关系的维护有两种,Hibernate的一对一关系(转),分为主键关联和唯一外键关联主键关联:(双向)(主控方) Person.javaPerson { PRivate String username; //对被控方的引用 private Addr
一对一关系的维护有两种,分为主键关联和唯一外键关联
一对一关系的维护有两种,分为主键关联和唯一外键关联
主键关联:(双向)
(主控方)
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高级应用:性能优化策略
- ››hibernate 多对多关系详解(包括中间表,一对多字表...
- ››Hibernate实现mysql数据库limit查询方法
- ››Hibernate 之父:是时候升级到Java EE 6了
- ››Hibernate查询
- ››Hibernate和iBATIS比较(摘自网络)
- ››Hibernate使用Projections进行聚合操作
- ››hibernate中java.util.Date类型映射
- ››hibernate中update与saveOrUpdate的区别
- ››Hibernate各种映射关系总结
- ››Hibernate过滤器使用窍门
- ››Hibernate属性查询简单讲述
更多精彩
赞助商链接