第九章Hibernate照射一对一关联关系
第九章Hibernate映射一对一关联关系
第九章Hibernate映射一对一关联关系
- 共享主键关联
- 唯一外键关联
1.两个关联表使用相同的主键值
数据准备:
-- Create table create table USERS1 ( USERNAME VARCHAR2(40), PASSWORD VARCHAR2(40), ID NUMBER(8) not null )
Profile表用于保存用户的其他信息。
-- Create table create table PROFILE ( ID NUMBER(8) not null, EMAIL VARCHAR2(200), PHONE VARCHAR2(20), MOBILE VARCHAR2(20), ADDRESS VARCHAR2(200), POSTCODE VARCHAR2(10) )
其中,ID即是主键又是外键。因此称为共享主键。
User:
public class User implements java.io.Serializable { private static final long serialVersionUID = 1L; private Integer id; private String username; private String password; private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } public User() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
User映射配置:
<?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.crazy.User" table="USERS1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment"></generator> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="40" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="40" /> </property> <one-to-one name="profile" class="com.crazy.Profile"></one-to-one> </class> </hibernate-mapping>
Profile类:
public class Profile implements java.io.Serializable { private Integer id; private String email; private String phone; private String mobile; private String address; private String postcode; private User user; public Profile() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return this.postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } }
Profile映射配置:
其主键是使用user的。
<?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.crazy.Profile" table="PROFILE" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="foreign"> <param name="property">user</param> </generator> </id> <property name="email" type="java.lang.String"> <column name="EMAIL" length="200" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> <property name="mobile" type="java.lang.String"> <column name="MOBILE" length="20" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="200" /> </property> <property name="postcode" type="java.lang.String"> <column name="POSTCODE" length="10" /> </property> <one-to-one name="user" class="com.crazy.User"></one-to-one> </class> </hibernate-mapping>
测试代码:
public class HibernateTest { public static void main(String args[]){ HibernateTest test = new HibernateTest(); test.add(); } public void add(){ Profile profile = new Profile(); profile.setAddress("河南胜利路"); profile.setEmail("yesmeshtu2008@163.com"); profile.setMobile("13301239472145"); User user = new User(); profile.setUser(user); user.setUsername("grass"); user.setPassword("123"); user.setProfile(profile); Session session = new Configuration().configure().buildSessionFactory().openSession(); session.beginTransaction(); session.save(profile); session.save(user); session.getTransaction().commit(); } }
2.多对一关联的特殊形式,要求多方唯一
User类:
public class User implements java.io.Serializable { private Integer id; private String username; private String password; private Profile profile; public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } public User() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } }
User映射配置:
<?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.crazy.User" table="USERS1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="40" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="40" /> </property> <one-to-one name="profile" class="com.crazy.Profile"></one-to-one> </class> </hibernate-mapping>
Profile类:
public class Profile implements java.io.Serializable { private Integer id; private String email; private String phone; private String mobile; private String address; private String postcode; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Profile() { } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return this.phone; } public void setPhone(String phone) { this.phone = phone; } public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return this.postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } }
Profile映射配置:
<?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.crazy.Profile" table="PROFILE1" schema="SCOTT"> <id name="id" type="java.lang.Integer"> <column name="ID" precision="8" scale="0" /> <generator class="increment" /> </id> <property name="email" type="java.lang.String"> <column name="EMAIL" length="200" /> </property> <property name="phone" type="java.lang.String"> <column name="PHONE" length="20" /> </property> <property name="mobile" type="java.lang.String"> <column name="MOBILE" length="20" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="200" /> </property> <property name="postcode" type="java.lang.String"> <column name="POSTCODE" length="10" /> </property> <many-to-one name="user" class="com.crazy.User" unique="true"> <column name="user_id"></column> </many-to-one> </class> </hibernate-mapping>
配置如下:
<many-to-one name="user" class="com.crazy.User" unique="true"> <column name="user_id"></column> </many-to-one>
测试代码:
public class HibernateTest { public static void main(String args[]){ HibernateTest test = new HibernateTest(); test.add(); } public void add(){ Profile profile = new Profile(); profile.setAddress("河南胜利路"); profile.setEmail("yesmeshtu2008@163.com"); profile.setMobile("13309472145"); User user = new User(); profile.setUser(user); user.setUsername("grass"); user.setPassword("123"); user.setProfile(profile); Session session = new Configuration().configure().buildSessionFactory().openSession(); session.beginTransaction(); session.save(profile); session.save(user); session.getTransaction().commit(); } }