分类:
hibernate(2)
版权声明:本文为博主原创文章,未经博主允许不得转载。
- public class Person {
- private int id;
- private String name;
- private IdCard idCard;
- public Person() {
- }
- public Person(int id, String name, IdCard idCard) {
- this.id = id;
- this.name = name;
- this.idCard = idCard;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public IdCard getIdCard() {
- return idCard;
- }
- public void setIdCard(IdCard idCard) {
- this.idCard = idCard;
- }
- }
- public class IdCard {
- private int id;
- private Date usefullLife;
- private Person person;
- public IdCard() {
- }
- public IdCard(int id, Date usefullLife, Person person) {
- this.id = id;
- this.usefullLife = usefullLife;
- this.person = person;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public Date getUsefullLife() {
- return usefullLife;
- }
- public void setUsefullLife(Date usefullLife) {
- this.usefullLife = usefullLife;
- }
- public Person getPerson() {
- return person;
- }
- public void setPerson(Person person) {
- this.person = person;
- }
- }
one-to-one方式一:
IdCard.hbm.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.huawei.domain.IdCard" table="id_card">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="foreign" >
- <param name="property">person</param>
- </generator>
- </id>
- <property name="usefullLife" type="java.util.Date">
- <column name="usefull_life"/>
- </property>
- <one-to-one name="person" constrained="true"></one-to-one>
- </class>
- </hibernate-mapping>
Person.hbm.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.huawei.domain.Person">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="5" not-null="false" />
- </property>
- <one-to-one name="idCard"></one-to-one>
- </class>
- </hibernate-mapping>
one-to-one方式二:
IdCard.hbm.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.huawei.domain.IdCard" table="id_card">
- <id name="id" type="java.lang.Integer">
- <!--
- <column name="id" />
- <generator class="foreign" >
- <param name="property">person</param>
- </generator>
- -->
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="usefullLife" type="java.util.Date">
- <column name="usefull_life"/>
- </property>
- <!-- <one-to-one name="person" constrained="true"></one-to-one> -->
- <many-to-one name="person" column="person_id" unique="true"></many-to-one>
- </class>
- </hibernate-mapping>
Person.hbm.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.huawei.domain.Person">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="5" not-null="false" />
- </property>
- <one-to-one name="idCard" property-ref="person"></one-to-one>
- </class>
- </hibernate-mapping>
测试:
- Session s = null;
- try{
- s = HibernateSessionFactory.getSession();
- // Person person = (Person)s.get(Person.class, id);
- // System.out.println(person.getIdCard().getUsefullLife());
- IdCard idCard = (IdCard)s.get(IdCard.class, id);
- System.out.println(idCard.getPerson().getName());
- } catch (HibernateException e) {
- e.printStackTrace();
- }