论坛首页 Java企业应用论坛

getHibernateTemplate().find()和executeFind()的问题

浏览 8689 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-20  
如下方法:
public TaxConfig getTaxConfig(String sys, String key)throws RuntimeException {
List<TaxConfig> results = find(" from TaxConfig where sysName=? and key=?",obj);
		
		if(results == null || results.isEmpty() ){
			throw new RuntimeException("taxConfig not exist!");
		}else{
			return (TaxConfig)results.get(0);
		}
		
	}


第一次查询时结果正常,输出的SQL语句也正常.输出的SQL如下:
select taxconfig0_.F_SYSNAME as F1_10_, taxconfig0_.F_KEY as F2_10_, taxconfig0_.F_NOTE as F3_10_, taxconfig0_.F_VALTYPE as F4_10_, taxconfig0_.F_VAL as F5_10_ from tax_config taxconfig0_ where taxconfig0_.F_SYSNAME='sagd' and taxconfig0_.F_KEY='gantPath'
第二次查询时出错.错误如下:
hql is ... from TaxConfig where sysName='sagd' and key='gantPath'
Hibernate: select taxconfig0_.F_SYSNAME as F1_10_0_, taxconfig0_.F_KEY as F2_10_0_, taxconfig0_.F_NOTE as F3_10_0_, taxconfig0_.F_VALTYPE as F4_10_0_, taxconfig0_.F_VAL as F5_10_0_ from tax_config taxconfig0_ where taxconfig0_.F_SYSNAME=?
[sagd] WARN [http-9090-Processor23] DefaultRemoter.warn(67) | Method execution failed: 
org.springframework.orm.hibernate3.HibernateSystemException: More than one row with the given identifier was found: sagd, for class: com.lhsm.core.model.TaxConfig; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: sagd, for class: com.lhsm.core.model.TaxConfig

还有就是如果数据库中的表只有一条记录,不会出现上面的问题,多于一条第二次查询就出错.
而换成了如下写法就没有问题,请问这是怎么回事?
public List find(final String hql, final Object... values) {
		Assert.hasText(hql);
		return super.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session s)
							throws HibernateException, SQLException {
						Query query = s.createQuery(hql);
						query.setCacheable(false);
						for (int i = 0; i < values.length; i++) {
							query.setParameter(i, values[i]);
						}
						List list = query.list();
						return list;
					}
				});
	}
   发表时间:2007-08-20  
把getTaxConfig改成如下方法里面的find()方法实现如上:
public TaxConfig getTaxConfig(String sys, String key)throws RuntimeException {
		// TODO Auto-generated method stub
		Object[] obj = new Object[]{sys,key};
		/*String hql = "from TaxConfig where sysName='"+sys+"' and key='"+key+"'";
		System.out.println("hql is ... "+hql);
		List<TaxConfig> results = getHibernateTemplate().find(hql);*/
		List<TaxConfig> results = find(" from TaxConfig where sysName=? and key=?",obj);
		
		if(results == null || results.isEmpty() ){
			throw new RuntimeException("taxConfig not exist!");
		}else{
			return (TaxConfig)results.get(0);
		}
		
	}
0 请登录后投票
   发表时间:2007-08-21  
如果我把Spring的配置文件中的hibernate缓存设成false就不会出问题.
<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
		<property name="cacheQueries">
			<value>false</value>
		</property>
	</bean>

这是什么原因呀?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics