hibernate中配置一对一映射(基于主键)

Person    id(pk)

              name

IdCard    id(pk,fk)

              usefulDate  

 

   Person.java

public class Person {
	private int id;
	private String name;
	private IdCard idCard;
         //getters and setters
}

    Person.hbm.xml

<hibernate-mapping package="com.susan.hibernate.entity">
	<class name="Person" table="tperson">
		<id name="id">
		<generator class="sequence">
    		<param name="sequence">seq_person</param>
    		</generator>
		</id>
		<property name="name" column="name" />
		<one-to-one name="idCard"/>
          </class>
</hibernate-mapping>

 

IdCard.java

public class IdCard {
	private int id;
	private Date usefulDate;
	private Person person;
         //setters and getters
}

 

 IdCard.hbm.xml

<hibernate-mapping package="com.susan.hibernate.entity">
	<class name="IdCard" table="tidcard">
		<id name="id">
    		<generator class="forgein">
    		<param name="property">person</param>
    		</generator>
		</id>
		<property name="usefulDate" column="usefulDate" />
		<one-to-one name="person" contrained="true"/>
         </class>
</hibernate-mapping>

 

你可能感兴趣的:(xml,Hibernate)