2015-10-01 84 views
15

所以我試圖用Spring來第一次管理hibernate事務,並且出現了問題。我不知道是什麼。我在這個網站上看過一堆類似的答案,我看到的東西似乎都是正確的。Hibernate&Spring:嘗試創建事務時出現異常

所以,我要複製一些我的代碼並粘貼一些解釋,並在這裏尋求幫助。

這是我得到的異常的堆棧跟蹤。本質上,它似乎試圖找到org.hibernate.engine.transaction.spi.transactioncontext,而不能。

異常堆棧跟蹤

EXCEPTION: Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext 
org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:544) 
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) 
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427) 
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276) 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
com.sun.proxy.$Proxy42.getSavedPortfolios(Unknown Source) 
io.craigmiller160.stockmarket.controller.StockMarketController.showOpenPortfolioDialog(StockMarketController.java:994) 
io.craigmiller160.stockmarket.controller.StockMarketController.parseEvent(StockMarketController.java:431) 
io.craigmiller160.stockmarket.controller.StockMarketController.processEvent(StockMarketController.java:336) 
io.craigmiller160.mvp.concurrent.AbstractConcurrentListenerController$1.run(AbstractConcurrentListenerController.java:209) 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
java.lang.Thread.run(Thread.java:745) 

現在,我搜索這個網站,我看到的大的事情是,這意味着我有依賴錯誤的,我的pom.xml。問題是,我的pom中有最新版本的hibernate-core依賴項。從我讀過的內容來看,這就是我爲這門課所需要的。

的pom.xml依賴

<dependencies> 
<!-- JUnit Testing --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 
<!-- MVP Framework --> 
    <dependency> 
     <groupId>io.craigmiller160.mvp</groupId> 
     <artifactId>mvp-framework</artifactId> 
     <version>2.1.1</version> 
    </dependency> 
<!-- MigLayout --> 
    <dependency> 
     <groupId>com.miglayout</groupId> 
     <artifactId>miglayout-swing</artifactId> 
     <version>5.0</version> 
    </dependency> 
<!-- JFreeChart --> 
    <dependency> 
     <groupId>org.jfree</groupId> 
     <artifactId>jfreechart</artifactId> 
     <version>1.0.19</version> 
    </dependency> 
<!-- Java Concurrency In Practice Annotations --> 
    <dependency> 
     <groupId>net.jcip</groupId> 
     <artifactId>jcip-annotations</artifactId> 
     <version>1.0</version> 
    </dependency> 
<!-- Joda Time --> 
    <dependency> 
     <groupId>joda-time</groupId> 
     <artifactId>joda-time</artifactId> 
     <version>2.8.2</version> 
    </dependency> 
<!-- MySQL ConnectorJ --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.36</version> 
    </dependency> 
<!-- Spring Framework Core --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
<!-- Spring Framework Beans --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
<!-- Spring Framework Context --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
<!-- Hibernate Core --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.0.1.Final</version> 
    </dependency> 
<!-- XML Framework --> 
    <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
    </dependency> 
<!-- Code Generation library --> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>3.1</version> 
    </dependency> 
<!-- Apache Commons Logging --> 
    <dependency> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging</artifactId> 
     <version>1.2</version> 
    </dependency> 
<!-- LOG4J API --> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>${log4j.version}</version> 
    </dependency> 
<!-- LOG4J Core --> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>${log4j.version}</version> 
    </dependency> 
<!-- SLF4J/LOG4J Binding --> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-slf4j-impl</artifactId> 
     <version>${log4j.version}</version> 
    </dependency> 
<!-- LOG4J/Commons Logging Binding --> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-jcl</artifactId> 
     <version>${log4j.version}</version> 
    </dependency> 
<!-- SLF4J API --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.7.12</version> 
    </dependency> 
<!-- Spring ORM --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
<!-- AspectJ Runtime --> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>${aspectj.version}</version> 
    </dependency> 
<!-- AspectJ Weaver --> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>${aspectj.version}</version> 
    </dependency> 
<!-- Apache Database Connection Pooling --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-dbcp2</artifactId> 
     <version>2.1.1</version> 
    </dependency> 
</dependencies> 

而且,我加入我的DAO實際的方法我打電話。這種方法是在拋出異常時試圖運行的。

DAO方法:

@Transactional 
@Override 
@SuppressWarnings("unchecked") //hibernate list() method doesn't support generics 
public List<String> getSavedPortfolios() throws HibernateException { 
    List<String> portfolioNames = new ArrayList<>(); 

    List<SQLPortfolioModel> portfolioList = sessionFactory.getCurrentSession() 
           .createCriteria(PortfolioModel.class) 
           .list(); 

    for(SQLPortfolioModel portfolio : portfolioList){ 
     int id = portfolio.getUserID(); 
     String name = portfolio.getPortfolioName(); 
     BigDecimal netWorth = portfolio.getNetWorth(); 
     Calendar timestamp = portfolio.getTimestamp(); 

     String fileName = String.format("%1$d-%2$s-%3$s-" 
       +"%4$s", id, name, moneyFormat.format(netWorth), 
       timestampFormat.format(timestamp.getTime())); 
     portfolioNames.add(fileName); 
    } 

    return portfolioNames; 
} 

最後,這裏是我的春天上下文data​​.xml中。它包含了我的數據豆春季所有的配置,再加上交易的東西:

彈簧上下文data​​.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<!-- Sets annotation-driven transactions --> 
<tx:annotation-driven transaction-manager="transactionManager"/> 

<!-- DataSource object for providing database connections --> 
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost/stockmarket"/> 
    <property name="username" value="stockmarket"/> 
    <property name="password" value="stockmarket"/> 
</bean> 

<!-- SessionFactory object for creating sessions for database access --> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"/>--> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="connection.pool_size">1</prop> 
      <prop key="show_sql">false</prop> 
      <!-- Might need this one below for transactions, not sure yet --> 
      <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
     </props> 
    </property> 
    <property name="annotatedClasses"> 
     <list> 
      <value>io.craigmiller160.stockmarket.stock.AbstractStock</value> 
      <value>io.craigmiller160.stockmarket.stock.OwnedStock</value> 
      <value>io.craigmiller160.stockmarket.stock.DefaultStock</value> 
      <value>io.craigmiller160.stockmarket.stock.DefaultOwnedStock</value> 
      <value>io.craigmiller160.stockmarket.model.PortfolioModel</value> 
      <value>io.craigmiller160.stockmarket.model.SQLPortfolioModel</value> 
     </list> 
    </property> 
</bean> 

<!-- Hibernate Transaction Manager --> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<!-- HibernateDAO class for performing database operations --> 
<bean id="hibernateDao" class="io.craigmiller160.stockmarket.controller.HibernatePortfolioDAO" 
    destroy-method="closeFactory"> 
    <constructor-arg ref="sessionFactory"/> 
</bean> 

</beans> 

所以,我只是不知道爲什麼會這樣。我已經雙倍和三倍檢查了我所做的與我在網上看到的情況,並且我看不到這個錯誤。這是我第一次嘗試使用Spring事務管理。任何幫助將不勝感激。

PS。我一起使用Spring 4 & Hibernate 5,如果這有所作爲。

+0

您正在使用hibernate4根據春天,但你必須hibernate5依賴性。這是行不通的,要麼切換到正確的'LocalSessionFactoryBean'或切換到hibernate4 –

+0

您確切地知道哪個版本的Spring 4?只有Spring 4.2和更高版本支持Hibernate 5.如果你有Spring 4.1,那麼你可以使用Hibernate 4.3作爲最高版本。 – dunni

回答

0

不知道這是否是問題,但由於您認爲是與pom.xml相關的東西,請嘗試添加hibernate的實體管理器依賴項。我將你的pom.xml與我的相比較,關於hibernate的依賴關係,這是缺乏的。

+0

你能指定我缺少的依賴嗎? – user2223059

+0

在這裏http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager/5.0.0.Final –

5

它尋找的是指,只有在休眠4.x的 http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/engine/transaction/spi/package-summary.html

存在一類 http://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/engine/transaction/spi/package-summary.html

所以我想你可能會混合休眠的類,但沒有更多的5.x的4和5依賴關係。我不認爲你可以在不檢查所有依賴關係的情況下切換休眠版本。

+0

那麼我應該只是恢復到休眠4呢?我所有的問題都會解決嗎? – user2223059

+0

尤里卡!這工作,非常感謝。所以現在只剩下的bug是,一些hibernate的日誌記錄在控制檯中打印出來,而不是被路由到log4j2中的fileAppender。當我使用版本5時,並沒有發生這種情況。我的POM具有依賴關係,我必須確保所有slf4j日誌都能正確路由,不知道發生了什麼? (小細節,我知道,但我非常注重細節) – user2223059

+0

「一些hibernate的日誌記錄」究竟是什麼?我寧願在stackoverflow上打開一個新的問題,以便它得到正確的關注,並且可以發佈一些示例配置。 –

45

在你的POM你依賴於Hibernate的5,但在你的事務管理器,你正在使用Hibernate 4.

更改transacation經理,以配合您的POM(即從hibernate4到hibernate5):

<bean id="transactionManager" 
     class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

如果導致類沒有發現錯誤,升級你的Spring框架4.2.2

+3

謝謝!對我來說就像一個魅力。 – Deoxyseia

+0

看起來這是正確的答案,但你可以將它轉換成@annotation模式,你的答案會更加完美和有用......並且謝謝。 –

+0

每天給我保存:) –

-1

我得到了同樣的問題,我的問題得到通過更新

解決
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory"/> 

+1

你沒看見這個確切的方法是由某人發佈的?作爲回答,你最好發表評論。 – larrytech

2

您正在使用休眠-5,但你問春季使用Hibernate-4 SessionFactory的

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

和事務管理

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
相關問題