近日老领导让我看看之前的系统不能登陆了,我查了查卡在了hibernate查询Session.CreateQuery(hql)那里,直接跳到finally内,连catch都没进去,感觉很奇怪,于是问了问度娘,发现一则是在配置文件内添加一个属性节点。
我这项目是SSH搭建的,spring和hibernate配合着。经断点测试当走到 Begin后面的那句createQuery时直接跳入finally,并没有到End那句也没有被catch捕获。
如下代码示例:
try {
session = getSession();
System.out.println("Begin");
query = session.createQuery(hql.toString());
System.out.println("End");
query.setInteger(0, tuser.getUserId());
query.setInteger(1, tuser.getUserId());
list = query.list();
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
session.close();
}
解决方案:Spring+Hibernate开发
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>
加入<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> 节点
如果只是hibernate配置文件需要加入
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
最后补充一下,还有一种情况就是如果你用的IDE是MyEclipse8.6 请检查一下lib下是否多出来个antlr-2.7.2.jar 包,如果有了它也会出现此情况所以要把它消灭掉。