Hibernate的执行流程和集合的映射关系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate的执行流程和集合的映射关系相关的知识,希望对你有一定的参考价值。

Hibernate的执行流程

技术分享


集合映射

准被hibernate的运行环境

配置hibernate.cfg.xml主配置文件

1、Set集合

写User.java类

package com.gqx.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * javaBean的设计
 * @author 郭庆兴
 *
 */
public class User {
	
	private int userId;
	private String userName;
	//一个用户对应多个地址
	private Set<String> address;
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Set<String> getAddress() {
		return address;
	}
	public void setAddress(Set<String> address) {
		this.address = address;
	}
	
}

映射文件User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping  package="com.gqx.collection">

	<class name="User" table="t_users">
		<!-- 主键字段 -->	
		<id name="userId" column="id">
			<generator class="native"/>
		</id>
		<property name="userName" ></property>
		<!-- set集合属性的映射,指定要映射的set集合的属性
			 table 集合属性要映射到的表 
			 key 指定属性集合表(t_address)的外键字段
			 element 指定集合表的其他字段-->
		<set name="address" table="t_address">
			<key column="uid"></key>
			<element column="address" type="string"></element>
		</set>
		
	</class>



</hibernate-mapping>

测试类如下:

public class App {
	private static SessionFactory sf;
	static{
		sf=new Configuration().configure().addClass(User.class).buildSessionFactory();
	}
	/**
	 * Set集合
	 */
	@Test
	public void test() {
		Session session=sf.openSession();
		session.beginTransaction();
		
		//**************保存************
		Set<String> addressSet=new HashSet<String>();
		addressSet.add("黄石");
		addressSet.add("荆州");
		User user=new User();
		user.setAddress(addressSet);
		user.setUserName("gqxing");
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}
}

 2、List集合User.java

package com.gqx.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * javaBean的设计
 * @author 郭庆兴
 *
 */
public class User {
	
	private int userId;
	private String userName;
	//一个用户对应多个地址
	private List<String> addressList=new ArrayList<String>();
	
	
	public void setAddressList(List<String> addressList) {
		this.addressList = addressList;
	}
	public List<String> getAddressList() {
		return addressList;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
}

映射文件(User.hbm.xml)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping  package="com.gqx.collection">

	<class name="User" table="t_users">
		<!-- 主键字段 -->	
		<id name="userId" column="id">
			<generator class="native"/>
		</id>
		<property name="userName" ></property>
		
		<!-- List集合
			list-index:指定的是集合排列的顺序(因为要保证List集合的有序)
		 -->
		 <list name="addressList" table="t_addressList">
		 	<key column="uid"></key>
		 	<list-index column="idx"></list-index>
		 	<element column="address" type="string"></element>
		 </list>
		
	</class>



</hibernate-mapping>

测试类:

    /**
	 * List集合
	 */
	@Test
	public void testSaveList() {
		Session session=sf.openSession();
		session.beginTransaction();
		
		//**************保存************
		User user=new User();
		user.setUserName("gqxing");
		user.getAddressList().add("武汉");
		user.getAddressList().add("上海");
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}

 3、Map集合

User.java

package com.gqx.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * javaBean的设计
 * @author 郭庆兴
 *
 */
public class User {
	
	private int userId;
	private String userName;
	//一个用户对应多个地址
	private Map<String, String> addressMap=new HashMap<String, String>();
	
	
	public void setAddressMap(Map<String, String> addressMap) {
		this.addressMap = addressMap;
	}
	public Map<String, String> getAddressMap() {
		return addressMap;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	
}

 映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping  package="com.gqx.collection">

	<class name="User" table="t_users">
		<!-- 主键字段 -->	
		<id name="userId" column="id">
			<generator class="native"/>
		</id>
		<property name="userName" ></property>
		
		 <!-- 
		 	map集合映射
		 	key:指定外键字段
		 	map-key:指定map的key
		 	element:指定map的value
		  -->
		  <map name="addressMap" table="t_addressMap">	
		  	<key column="uid"></key>
		  	<map-key type="string" column="shortName"></map-key>
		  	<element column="address" type="string"></element>
		  </map>
	</class>



</hibernate-mapping>

 测试类

    /**
	 * List集合
	 */
	@Test
	public void testSaveMap() {
		Session session=sf.openSession();
		session.beginTransaction();
		
		//**************保存************
		User user=new User();
		user.setUserName("gqxing");
		user.getAddressMap().put("A002", "黄石");
		user.getAddressMap().put("A001", "荆州");
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}

 


 

 集合的获取

测试类

    /**
	 * 获取 
	 */
	@Test
	public void testGet() {
		Session session=sf.openSession();
		session.beginTransaction();
		
		//获取
		User user=(User)session.get(User.class, 2); //及时加载
		System.out.println(user.getUserId());
		System.out.println(user.getUserName());
		//当查询用户,可以获取与亲关联的list集合的数据(因为有正确的映射)
		//当遇到到集合数据的使用时,才向数据库发送执行的sql语句(懒加载)
		System.out.println(user.getAddressList());
		session.getTransaction().commit();
		session.close();
	}

 

以上是关于Hibernate的执行流程和集合的映射关系的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate的执行流程——SessionFactory的创建

(转)Hibernate框架基础——一对多关联关系映射

hibernate的映射关系

Hibernate4学习总结

Hibernate (开源对象关系映射框架)

Hibernate - 超越对象/关系映射?