HIbernate关系映射
Idcard.hbm.xml:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyy.domain.Idcard">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="validate" type="java.util.Date">
<column name="validate"/>
</property>
<!-- 配置one-to-one
constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列
-->
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyy.domain.Person">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned"/>
</id>
<property name="name" type="java.lang.String">
<column name="name"/>
</property>
<!-- 配置one-to-one -->
<one-to-one name="idcard" />
</class>
</hibernate-mapping>
Person p1=new Person();
p1.setId(2);
p1.setName("scott1");
Idcard idcard=new Idcard();
idcard.setValidate(new Date());
idcard.setPerson(p1);
session.save(p1);
session.save(idcard);
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyy.domain.Idcard"><!--这里怎么没有对用的数据库表名呢?-->
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="validate" type="java.util.Date">
<column name="validate"/>
</property>
<!-- 配置one-to-one
constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列
-->
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyy.domain.Idcard"><span style="color: #FF0000;">这里怎么没有对用的数据库表名呢?</span>
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="validate" type="java.util.Date">
<column name="validate"/>
</property>
<!-- 配置one-to-one
constrained:不配置,则不会自动生成外键列,配置了,则会生成外键列
-->
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
<class name="com.project.hibernate.Person" table="t_person">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
<class name="com.project.hibernate.IdCard" table="t_idCard">
<id name="id">
<generator class="native"/>
</id>
<property name="cardNo"/>
<many-to-one name="person" unique="true"/>
</class>
IdCard idCard = new IdCard();
idCard.setCardNo("1001");
Person person = new Person();
person.setName("张三");
//建立关联
person.setIdCard(idCard);
//抛出TransientObjectException
//因为IdCard为Transient状态
session.save(person);
IdCard idCard = new IdCard();
idCard.setCardNo("1001");
session.save(idCard);
Person person = new Person();
person.setName("张三");
//建立关联
person.setIdCard(idCard);
session.save(person);
Person person = (Person)session.load(Person.class, 2);
System.out.println("person.name=" + person.getName());
System.out.println("person.cardNo=" + person.getIdCard().getCardNo());