hibernate 映射文件怎么配置 一对一

比如说有两个对象 a b 我要在A的映射文件中设置它和B的一对一,我该怎么做呢,请各位大神帮忙解决一下

1个回答

============Person .java
[code="java"]
public class Person implements java.io.Serializable {

private Long id;
private String name;
private Address address;

[/code]

===========Address .java

[code="xml"]
public class Address implements java.io.Serializable {
private Long id;
private Person person;
private String detail;

[/code]

===================Person.hbm.xml
[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">









姓名


<!-- cascade="all":在保存person对象的时候,级联保存person对象关联的address对象 -->


[/code]
====================Address.hbm.xml.
[code="xml"]

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">





<!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符 -->

person




详细地址


<!-- 表示在address表存在一个外键约束,外键参考相关联的表person -->


[/code]

================Hibernate配置
[code="xml"]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


root

jdbc:mysql://localhost:3306/mydb


org.hibernate.dialect.MySQLDialect

xiaohui

com.mysql.jdbc.Driver

true
true

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问