标签:
1.按照外键映射(Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射)
实现需要:
创建实体类Users1和Resume1
public class Users1 {
private Integer userid;
private String username;
private String userpass;
private Resume1 resume1;
}
***********
public class Resume1 {
private Integer resid;
private String resname;
private String rescardno;
private Users1 users1;
}
-----------------------
配置文件Users1.hbm.xml和Resume1.hbm.xml:
Users1.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetoone_fk">
<class name="Users1" table="USERS1">
<id name="userid" column="USERID" >
<generator class="native"></generator>
</id>
<property name="username" column="USERNAME" type="string"></property>
<property name="userpass" column="USERPASS" type="string"></property>
<one-to-one name="resume1" class="Resume1" property-ref="users1"></one-to-one>
</class>
</hibernate-mapping>
*********************
Resume1.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetoone_fk">
<class name="Resume1" table="RESUME1">
<id name="resid" column="RESID" >
<generator class="native"></generator>
</id>
<property name="resname" column="RESNAME" type="string"></property>
<property name="rescardno" column="RESCARDNO" type="string"></property>
<many-to-one name="users1" class="Users1" cascade="all" column="RESUSERID" unique="true"></many-to-one>
</class>
</hibernate-mapping>
------------------------------------
2.主键映射
实现需要:
创建实体类Users2和Resume2
public class Users2 {
private Integer userid;
private String username;
private String userpass;
private Resume2 resume2;
}
*******************:
public class Resume2 {
private Integer resid;
private String resname;
private String rescardno;
private Users2 users2;
}
----------------------------
配置文件Users1.hbm.xml和Resume1.hbm.xml
Users1.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetoone_pk">
<class name="Users2" table="USERS2">
<id name="userid" column="USERID" >
<generator class="foreign">
<param name="property">resume2</param>
</generator>
</id>
<property name="username" column="USERNAME" type="string"></property>
<property name="userpass" column="USERPASS" type="string"></property>
<one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
************************
Resume2.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.onetoone_pk">
<class name="Resume2" table="RESUME2">
<id column="RESID" name="resid">
<generator class="native"/>
</id>
<property column="RESNAME" name="resname" type="string"/>
<property column="RESCARDNO" name="rescardno" type="string"/>
<one-to-one name="users2" cascade="all" class="Users2"/>
</class>
</hibernate-mapping>
==========================================
一 。组件映射(建立关系数据模型的一个重要原则是在不会导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系)
实现需求:
创建EmpHomeAddress和EmpInfo
public class EmpHomeAddress {
private String ehomestreet;
private String ehomecity;
private String ehomeprovince;
private String ehomezipcode;
private EmpInfo empinfo;
}
--------------
public class EmpInfo {
private Integer eid;
private String ename;
private EmpHomeAddress ehome;
}
*************************
创建配置文件EmpInfo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.component">
<class name="EmpInfo" table="EMPINFO">
<id name="eid" column="EID">
<generator class="native"></generator>
</id>
<property name="ename" column="ENAME" type="string"></property>
<component name="ehome" class="EmpHomeAddress">
<parent name="empinfo"/>
<property name="ehomestreet" column="EHOMESTREET" type="string"></property>
<property name="ehomecity" column="EHOMECITY" type="string"></property>
<property name="ehomeprovince" column="EHOMEPROVINCE" type="string"></property>
<property name="ehomezipcode" column="EHOMEZIPCODE" type="string"></property>
</component>
</class>
</hibernate-mapping>
标签:
原文地址:http://www.cnblogs.com/ainiaiwo/p/5842377.html