`

第九章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();
	}
}
 
分享到:
评论

相关推荐

    hibernate学习笔记

    hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person----&gt;IdCard) 8 hibernate一对一主键关联映射(双向关联Person&lt;----&gt;IdCard) 9 hibernate一对一唯一外键关联映射...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

     15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part3

     15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part1.rar

     15.1 映射一对一关联  15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类...

    Hibernate实战(第2版 中文高清版)

     7.3.1 多态的多对一关联   7.3.2 多态集合   7.3.3 对联合的多态关联   7.3.4 每个具体类一张多态表   7.4 小结   第8章 遗留数据库和定制SQL   8.1 整合遗留数据库   8.1.1 处理主键   8.1.2 带...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    Hibernate+中文文档

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    HibernateAPI中文版.chm

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    hibernate3.2中文文档(chm格式)

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    Hibernate中文详细学习文档

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    Hibernate 中文 html 帮助文档

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional ...

    Hibernate教程

    8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...

    hibernate 体系结构与配置 参考文档(html)

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联...

    最全Hibernate 参考文档

    7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional ...

    精通Java Web整合开发(第2版)

    12.4.4 一对一关联的annotation注解实现536 12.4.5 多对一单向关联的annotation注解实现538 12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向...

    hibernate 教程

    一对一 5.1.12. 组件(component), 动态组件(dynamic-component) 5.1.13. 子类(subclass) 5.1.14. 连接的子类(joined-subclass) 5.1.15. map, set, list, bag 5.1.16. 引用(import) 5.2. ...

    Hibernate3的帮助文档

    8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...

    hibernate3.04中文文档.chm

    8. 关联关系映射 8.1. 介绍 8.2. 单向关联(Unidirectional associations) 8.2.1. 多对一(many to one) 8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联...

Global site tag (gtag.js) - Google Analytics