Hibernate持续更新映射组件关系

Posted sihai_1991

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate持续更新映射组件关系相关的知识,希望对你有一定的参考价值。

知识准备:hibernate将对象分为二种类型

1. 含有OID的对象:实体类型

2. 不含OID的对象:值类型

案例:客户和公司地址和家庭地址

声明:客户和地址是整体与部分的关系,在数据库中只需要建立一个表Users

第一步,创建表:

use db3;

drop table if exists users;
create table if not exists users(
	id int primary key auto_increment,
	name varchar(10),
	salary double,
	com_privince varchar(10),
	com_city varchar(10),
	com_area varchar(10),
	home_privince varchar(10),
	home_city varchar(10),
	home_area varchar(10));
查看users表结构(desc user):

mysql> use db3;
Database changed
mysql> desc users;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| name          | varchar(10) | YES  |     | NULL    |                |
| salary        | double      | YES  |     | NULL    |                |
| com_privince  | varchar(10) | YES  |     | NULL    |                |
| com_city      | varchar(10) | YES  |     | NULL    |                |
| com_area      | varchar(10) | YES  |     | NULL    |                |
| home_privince | varchar(10) | YES  |     | NULL    |                |
| home_city     | varchar(10) | YES  |     | NULL    |                |
| home_area     | varchar(10) | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)

第二步:建立实体类并配置映射文件

User.java

package cn.lsh.web.hibernate.domain;

public class User 
	private Integer id;//编号
	private String name;//姓名
	private Double salary;//薪水
	private Address comAddress;//公司地址
	private Address homeAddress;//家庭地址
	public User() 

	
	public User(String name, Double salary) 
		super();
		this.name = name;
		this.salary = salary;
	
	public Integer getId() 
		return id;
	
	public void setId(Integer id) 
		this.id = id;
	
	public String getName() 
		return name;
	
	public void setName(String name) 
		this.name = name;
	
	public Double getSalary() 
		return salary;
	
	public void setSalary(Double salary) 
		this.salary = salary;
	
	public Address getComAddress() 
		return comAddress;
	
	public void setComAddress(Address comAddress) 
		this.comAddress = comAddress;
	
	public Address getHomeAddress() 
		return homeAddress;
	
	public void setHomeAddress(Address homeAddress) 
		this.homeAddress = homeAddress;
	

User.java类所对应的映射文件:

User.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping package="cn.lsh.web.hibernate.domain">
    	<class name="User" table="users" >
    		<id name="id" column="ID" type="integer">
    			<generator class="native"/>
    		</id>
    		<property name="name" column="NAME" type="string"/>
    		<property name="salary" column="SALARY" type="double"/>
    		
    		<!-- component专用于映射组件类型 -->
    		<component name="comAddress" class="Address">
    			<property name="province" column="COM_PRIVINCE" type="string"/>
    			<property name="city" column="COM_CITY" type="string"/>
    			<property name="area" column="COM_AREA" type="string"/>
    		</component>
    		<component name="homeAddress" class="Address">
    			<property name="province" column="HOME_PRIVINCE" type="string"/>
    			<property name="city" column="HOME_CITY" type="string"/>
    			<property name="area" column="HOME_AREA" type="string"/>
    		</component>
    	</class>
    </hibernate-mapping>

Address.java

//部分方、无id属性,值类型
public class Address 
	private String province;//省
	private String city;//市
	private String area;//区
	public Address()
	public Address(String province, String city, String area) 
	super();
	this.province = province;
	this.city = city;
	this.area = area;
	
	//省略set/get方法

需求1保存客户和地址

第三步:建立ComponentDao.java(使用junit)

//添加客户
	@Test
	public void addUser()
		Address comAddress = new Address("湖南省","长沙市","雨花区");
		Address homeAddress = new Address("湖南省","长沙市","望城区");
		
		User user = new User("王五",9000D);
		//设置整体和部分的关系
		user.setComAddress(comAddress);
		user.setHomeAddress(homeAddress);
		Session session = HibernateUtils.getSession();
		Transaction t = session.getTransaction();
		try
			t.begin();
			session.save(user);
			t.commit();
		catch(Exception e)
			e.printStackTrace();
			t.rollback();
		finally
			session.close();
		
	

</pre><pre>

需求2更新客户和地址

//更新客户
	@Test
	public void updateUser()
		Session session = HibernateUtils.getSession();
		Transaction t = session.getTransaction();
		try
			t.begin();
			User user = (User)session.get(User.class, 2);
			user.getComAddress().setArea("白云区");
			user.getHomeAddress().setArea("花都区");
			session.save(user);
			t.commit();
		catch(Exception e)
			e.printStackTrace();
			t.rollback();
		finally
			session.close();
		
	

需求3删除客户和地址

//删除客户
	@Test
	public void deleteUser()
		Session session = HibernateUtils.getSession();
		Transaction t = session.getTransaction();
		try
			t.begin();
			User user = (User)session.get(User.class, 3);
			
			session.delete(user);
			t.commit();
		catch(Exception e)
			e.printStackTrace();
			t.rollback();
		finally
			session.close();
		
	
查询结果如下:

select * from users;


Hibernate关于映射组件关系(关键部分代码):

<!-- component专用于映射组件类型 -->
    		<component name="comAddress" class="Address">
    			<property name="province" column="COM_PRIVINCE" type="string"/>
    			<property name="city" column="COM_CITY" type="string"/>
    			<property name="area" column="COM_AREA" type="string"/>
    		</component>
    		<component name="homeAddress" class="Address">
    			<property name="province" column="HOME_PRIVINCE" type="string"/>
    			<property name="city" column="HOME_CITY" type="string"/>
    			<property name="area" column="HOME_AREA" type="string"/>
    		</component>





以上是关于Hibernate持续更新映射组件关系的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate组件映射

Hibernate之组件映射

hibernate关系映射

主键映射和Hibernate映射

.Hibernate一对一映射与组件映射

hibernate中的组件映射