Hibernate:1对1关系总结。

QQ和QQ空间是1对1关系:配置如下

一、主键关联,双方共同维护表关系,以主键关联

public class QQ {

	private Long id;
private QQZone zone;
}
public class QQZone {

	private Long id;
private QQ qq;
}
<hibernate-mapping package="com.bohe.domain">

	<class name="QQ" table="qq">
<id name="id" column="ID">
<generator class="native" />
</id>
<one-to-one name="zone" /> </class> </hibernate-mapping>
<hibernate-mapping package="com.bohe.domain">

	<class name="QQZone" table="qqzone">
<id name="id" column="ID">
<generator class="native" />
</id>
<one-to-one name="qq" /> </class> </hibernate-mapping>

二、用many-to-one模仿one-to-one,qq表为主表,qqzone为从表

public class QQ {

	private Long id;
private QQZone zone;
}
public class QQZone {

	private Long id;
private QQ qq;
}
<hibernate-mapping package="com.bohe.domain">

	<class name="QQ" table="qq">
<id name="id" column="ID">
<generator class="native" />
</id>
<one-to-one name="zone" /> </class> </hibernate-mapping>
<hibernate-mapping package="com.bohe.domain">

	<class name="QQZone" table="qqzone">
<id name="id" column="ID">
<generator class="native" />
</id> <many-to-one name="qq" column="qq_id" unique="true"/>
</class> </hibernate-mapping>
上一篇:ZeroMQ接口函数之 :zmq_ctx_get - 得到环境上下文的属性


下一篇:Ubuntu 12.04更换显示器后显示“显示不支援”,只能进恢复模式工作