1
回答
Hibernate set集合映射,取完数据会多发出一条删除和插入sql语句,求解?
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   

Hibernate新手,写了下面的映射


<set name="set" table="t_set" lazy="false">
			<key column="set_id"/>
			<!-- 
				如果集合里的元素是普通属性的话,那么用<element>标签就Ok了。
				如果集合里的元素是类的话,那么就要用
				<composite-element class=""></composite-element>
			 -->
			<composite-element class="com.line.model.Thing">
				<property name="thing_name"/>
				<property name="thing_type"/>
			</composite-element>
		</set>
单元测试代码以及结果



package com.line.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CollectionTest extends TestCase {
	
	public void testSave(){
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = null;
		
		try{
			session = factory.openSession();
			session.beginTransaction();
			
			User user = new User();
			user.setName("俊东");
			
			//建立Set集合,往里添加Thing对象
			Set<Thing> set = new HashSet();
			Thing thing = new Thing();
			thing.setThing_name("牙刷");
			thing.setThing_type("生活用品");
			set.add(thing);
			
			user.setSet(set);
			
			//建立其他集合,测试普通属性
			List list = new ArrayList();
			list.add("a");
			list.add("b");
			
			user.setList(list);
			
			String[] strings = new String[]{"c","d"}; 
			
			user.setArray(strings);
			
			Map map = new HashMap();
			map.put("k", "v");
			map.put("k1", "v1");
			
			user.setMap(map);
			
			session.save(user);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
	}
         public void testLoad(){
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = null;
		
		try{
			session = factory.openSession();
			session.beginTransaction();
			
			User user = (User) session.get(User.class,1);
			
			System.out.println("user.name = " + user.getName());
			System.out.println("user.list = " + user.getList());
			System.out.println("user.map = " + user.getMap());
			System.out.println("user.array = " + user.getArray());
			System.out.println("user.set=" + user.getSet());
			
			session.getTransaction().commit();
			
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
	}
}

打印台信息:

Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_ from t_user user0_ where user0_.id=?
Hibernate: select array0_.key_id as key1_0_, array0_.array_value as array2_0_, array0_.array_index as array3_0_ from array array0_ where array0_.key_id=?
Hibernate: select set0_.set_id as set1_0_, set0_.thing_name as thing2_0_, set0_.thing_type as thing3_0_ from t_set set0_ where set0_.set_id=?
user.name = 俊东
Hibernate: select list0_.list_id as list1_0_, list0_.list_value as list2_0_, list0_.list_index as list3_0_ from list list0_ where list0_.list_id=?
user.list = [a, b]
Hibernate: select map0_.map_id as map1_0_, map0_.map_value as map2_0_, map0_.idx as idx0_ from map map0_ where map0_.map_id=?
user.map = {k=v, k1=v1}
user.array = [Ljava.lang.String;@134fada
user.set=[com.line.model.Thing@14987e8]
Hibernate: delete from t_set where set_id=? and thing_name=? and thing_type=?
Hibernate: insert into t_set (set_id, thing_name, thing_type) values (?, ?, ?)

不知道为什么,我运行单元测试的时候,查完数据后,在提交事务的时候,就会发出一条上面红色的insert和delete语句,做了无用功,搞不懂为什么,个人猜测有可能是Hibernate里的set集合采用的是PersistenSet。

举报
Line
发帖于5年前 1回/797阅
顶部