首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-10-29 
getHibernateTemplate().find()和executeFind()的问题如下方法:public TaxConfig getTaxConfig(String sys

getHibernateTemplate().find()和executeFind()的问题
如下方法:

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;}});}
public TaxConfig getTaxConfig(String sys, String key)throws RuntimeException {// TODO Auto-generated method stubObject[] 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);}}<bean id="hibernateTemplate"/></property><property name="cacheQueries"><value>false</value></property></bean>
这是什么原因呀?

热点排行