我使用相同的 batchNumber 调用下面的函数,并且它没有问题15次并且从数据库中获取记录没有问题但是在16时,应用程序在查询时冻结。调用list()行。而不是写“查询后”(参见System.out行)它只是失去了调试焦点而没有给出任何异常。这个问题可能不是关于hql,因为我在不同的hql和我使用标准而不是hql之前已经看到了这个问题,我得到了传递这个问题。但是当我在条件(setProjection ....)中使用“ group by ”时,它不会返回结果,因为hibernate model(object)只返回一个列表。但我需要结果作为模型。
注意:大约15次仅用于测试。这是一个Web应用程序,用户可以多次单击该按钮调用此函数以查看从数据库中获取的记录。
SiteAddressDaoImpl :
public class SiteAddressDaoImpl<T, Id extends Serializable> extends
GenericDaoHibernateImpl implements SiteAddressDao {
public List<SiteAddressModel> getSitesByBatch(String batchNumber) {
try {
List<SiteAddressModel> siteList;
MigrationPlanDao migrationPlanDao = ServiceFactory
.getO2SiteService().getMigrationPlanDao();
Query query = this.getSession().createQuery(
"from " + persistentClass.getName() + " where "
+ "siteType =:" + "type and siteName in "
+ "(select distinct exchange from "
+ migrationPlanDao.getPersistentClass().getName()
+ " where migrationBatchNumber =:" + "batchNumber"
+ ")");
query.setString("batchNumber", batchNumber);
query.setString("type", "LLU/ASN");
System.out.println("before query");
siteList = query.list();
System.out.println("after query");
return siteList;
} catch (Exception e) {
e.printStackTrace();
}
}
...
}
将GenericDaoHibernateImpl :
public class GenericDaoHibernateImpl<T, Id extends Serializable> extends HibernateDaoSupport implements GenericDao<T, Id> {..........}
休眠属性
<!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
depends-on="annotatedClassRegistrar">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses" ref="annotatedClassList"/>
<property name="hibernateProperties">
<props>
<prop key="c3p0.acquire_increment">1</prop>
<prop key="c3p0.idle_test_period">120</prop>
<prop key="c3p0.max_size">50</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.min_size">20</prop>
<prop key="c3p0.timeout">0</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
<!-- Spring Data Access Exception Translator Defintion -->
<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="exceptionTranslator" ref="jdbcExceptionTranslator"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Hibernate Template Defintion -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="jdbcExceptionTranslator" ref="jdbcExceptionTranslator"/>
</bean>
daos的豆子
<bean name="migrationPlanDao"
class="com.alcatel.lucent.tr.o2ccm.middleware.dao.impl.MigrationPlanDaoImpl">
<constructor-arg value="com.alcatel.lucent.tr.o2ccm.middleware.model.hibernate.MigrationPlanModel"/>
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean name="siteAddressDao" class="com.alcatel.lucent.tr.o2ccm.middleware.dao.impl.SiteAddressDaoImpl">
<constructor-arg value="com.alcatel.lucent.tr.o2ccm.middleware.model.hibernate.SiteAddressModel"/>
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
我简单地制作了hql
Query query = this.getSession().createQuery("from " + persistentClass.getName() + " where " + "siteName='siteName'");
它在第16次查询中无效。
我使用了hql的标准实例,它与hql做了相同的工作。它起作用的是什么......
DetachedCriteria criteria = DetachedCriteria.forClass(persistentClass);
criteria.add(Property.forName("siteName").eq("siteName"));
siteList = getHibernateTemplate().findByCriteria(criteria);
Hibernate版本3.2.0.ga
已更新:我添加了一些细节。
答案 0 :(得分:1)
我通过HibernateCallBack
解决了问题。
请参阅评论,了解哪些实施正常或无效。特别是通过查看findByHql()
和findByHqlQuery()
函数,可以更容易地看到差异。
findByHql()
是工作实施。区别似乎是会话,因为在getSitesByBatch()
函数中,当我调用findByHqlQuery()
时我正在通过this.getSession
创建查询,但是当我调用findByHql()
时我不需要创建查询,因为此函数已经通过从doInHibernate(Session session)
函数的Session
参数获取的会话创建了查询。直接拨打query.list()
也无效。
参见代码:
public class SiteAddressDaoImpl<T, Id extends Serializable> extends
GenericDaoHibernateImpl implements SiteAddressDao {
public List<SiteAddressModel> getSitesByBatch(String batchNumber) {
List<SiteAddressModel> siteList;
MigrationPlanDao migrationPlanDao = ServiceFactory.getO2SiteService()
.getMigrationPlanDao();
// working
String hql = "from " + persistentClass.getName() + " where "
+ "siteType ='LLU/ASN' and siteName in "
+ "(select distinct exchange from "
+ migrationPlanDao.getPersistentClass().getName()
+ " where migrationBatchNumber =" + "'" + batchNumber + "'"
+ ")";
siteList = findbyHQL(hql);
/*
* // notWorking Query query = this.getSession().createQuery("from " +
* persistentClass.getName() + " where " + "siteType =:" +
* "type and siteName in " + "(select distinct exchange from " +
* migrationPlanDao.getPersistentClass().getName() +
* " where migrationBatchNumber =:" + "batchNumber" + ")" );
*
* query.setString("batchNumber", batchNumber); query.setString("type",
* "LLU/ASN"); siteList = query.list();
*/
/*
* //working DetachedCriteria criteria =
* DetachedCriteria.forClass(persistentClass);
* criteria.add(Property.forName("siteName").eq("Barnet")); siteList =
* getHibernateTemplate().findByCriteria(criteria);
*/
/*
* //notWorking Query query = this.getSession().createQuery("from " +
* persistentClass.getName() + " where " + "siteName='Barnet'");
* siteList = findbyHQLQuery(query);
*/
/*
* //working String hql = "from " + getPersistentClass().getName() +
* " where " + "siteName='Barnet'" ; siteList = findbyHQL(hql);
*/
return siteList;
}
}
public class GenericDaoHibernateImpl<T, Id extends Serializable> extends
HibernateDaoSupport implements GenericDao<T, Id> {
public List<T> findbyHQL(final String hql) throws DaoException {
getHibernateTemplate().setAlwaysUseNewSession(true);
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// String str = "from " + persistentClass.getName() + " o";
Query query = session.createQuery(hql);
List<T> list = query.list();
logger.debug("Find " + list.size() + " records.");
return list;
}
});
}
public List<T> findbyHQLQuery(final Query hqlQuery) throws DaoException {
getHibernateTemplate().setAlwaysUseNewSession(true);
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List<T> list = hqlQuery.list();
logger.debug("Find " + list.size() + " records.");
return list;
}
});
}
}
答案 1 :(得分:0)
你给我们留下了极少的惊喜。数字15看起来太圆了,人们无法忽视。你如何管理交易和连接?你使用连接池吗?你回到游泳池的连接?您的连接池的大小恰好是15个连接吗?当您执行第16次代码时,池中是否有可用的连接?这是我建议你先检查的东西。