Hibernate 問題,在執行Query session.createQuery(hql) 報錯誤直接跳到finally

近日老領導讓我看看之前的系統不能登陸了,我查了查卡在了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 包,如果有了它也會出現此情況所以要把它消滅掉。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章