2011-04-20 43 views
1

問題:「java.lang.NoSuchFieldError的:NONE」 在Hibernate和Spring 3,行家,JPA,C3P0

休眠未正確執行查詢。它看起來與slf4j有關的問題出錯,但使用任何推薦的修復程序似乎都不起作用。

我已經嘗試了createQuery調用的變量名稱的各種組合,希望我做錯了(TM)但目前還沒有運氣。這個問題真的讓我難堪,有沒有人遇到過類似的東西?

堆棧跟蹤:

Exception in thread "main" java.lang.NoSuchFieldError: NONE 
    at org.hibernate.ejb.QueryImpl.<init>(QueryImpl.java:604) 
    at org.hibernate.ejb.QueryImpl.<init>(QueryImpl.java:79) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:268) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:341) 
    at $Proxy12.createQuery(Unknown Source) 
    at com.package.mvcfromscratch.dao.PostgresEventDao.numberOfEvents(PostgresEventDao.java:17) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196) 
    at $Proxy11.numberOfEvents(Unknown Source) 
    at com.package.mvcfromscratch.main.UserInterface.main(UserInterface.java:27) 

Maven的依賴關係樹:

[INFO] com.package:mvcFromScratch:jar:0.0.1-SNAPSHOT 
[INFO] +- org.springframework:spring-context:jar:3.0.3.RELEASE:compile 
[INFO] | +- org.springframework:spring-expression:jar:3.0.3.RELEASE:compile 
[INFO] | \- org.springframework:spring-asm:jar:3.0.3.RELEASE:compile 
[INFO] +- org.springframework:spring-beans:jar:3.0.3.RELEASE:compile 
[INFO] +- org.springframework:spring-aop:jar:3.0.3.RELEASE:compile 
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile 
[INFO] +- org.springframework:spring-web:jar:3.0.3.RELEASE:compile 
[INFO] +- org.springframework:spring-core:jar:3.0.3.RELEASE:compile 
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile 
[INFO] +- org.springframework:spring-test:jar:3.0.3.RELEASE:compile 
[INFO] +- org.springframework:spring-jpa:jar:2.0.8:compile 
[INFO] | +- org.springframework:spring-dao:jar:2.0.8:compile 
[INFO] | \- org.springframework:spring-jdbc:jar:2.0.8:compile 
[INFO] +- org.springframework:spring-tx:jar:3.0.3.RELEASE:compile 
[INFO] +- org.hibernate:hibernate:jar:3.5.3-Final:compile 
[INFO] | \- org.slf4j:slf4j-api:jar:1.5.8:compile 
[INFO] +- org.hibernate:hibernate-core:jar:3.5.3-Final:compile 
[INFO] | +- antlr:antlr:jar:2.7.6:compile 
[INFO] | +- commons-collections:commons-collections:jar:3.1:compile 
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile 
[INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile 
[INFO] | \- javax.transaction:jta:jar:1.1:compile 
[INFO] +- org.hibernate:hibernate-annotations:jar:3.5.3-Final:compile 
[INFO] +- org.hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile 
[INFO] | \- javax.persistence:persistence-api:jar:1.0:compile 
[INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile 
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.5.3-Final:compile 
[INFO] | +- cglib:cglib:jar:2.2:compile 
[INFO] | | \- asm:asm:jar:3.1:compile 
[INFO] | \- javassist:javassist:jar:3.9.0.GA:compile 
[INFO] +- postgresql:postgresql:jar:8.4-702.jdbc4:compile 
[INFO] \- org.slf4j:slf4j-simple:jar:1.5.8:compile 

PostgreSQL數據庫:

diarmaid=# \d Event; 
             Table "public.event" 
    Column  |   Type   |      Modifiers       
---------------+------------------------+--------------------------------------------------------- 
eventid  | integer    | not null default nextval('event_eventid_seq'::regclass) 
eventname  | character varying(255) | 
eventdate  | character varying(255) | 
eventcapacity | bigint     | 
Indexes: 
    "event_pkey" PRIMARY KEY, btree (eventid) 

的UserInterface類:

public class UserInterface { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); 
     EventDao eventDao = applicationContext.getBean("eventDao", EventDao.class); 

     // Are there any events? 
     System.out.println("Events: " + eventDao.numberOfEvents()); 
     // Get all events 
     List<Event> listEvents = eventDao.getAllEvents(); 
     // & Print them all out. 
     for(Event event : listEvents) { 
      System.out.println(event); 
     } 
     System.out.println(eventDao.getEvent(2)); 
    } 

} 

PostgresEventDao類:

public class PostgresEventDao extends JpaDaoSupport implements EventDao { 

    @Override 
    public String numberOfEvents() { 
     return (String) getJpaTemplate() 
      .getEntityManagerFactory() 
      .createEntityManager() 
      .createQuery("select count(ev.id) from Event ev") 
      .getSingleResult(); 
    } 

    @Override 
    public List<Event> getAllEvents() { 
     return getJpaTemplate().find("from Event"); 
    } 

    @Override 
    public Event getEvent(long id) { 
     return (Event) getJpaTemplate() 
      .getEntityManagerFactory() 
      .createEntityManager() 
      .createQuery("select d from Event d where d.id = " + id) 
      .getSingleResult(); 
    } 

    @Override 
    @Transactional 
    public void setAllEvents(List<Event> listEvent) { 
     for(Event event : listEvent) { 
      getJpaTemplate().persist(event); 
     } 

    } 

} 

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 

    <persistence-unit name="Events"> 
     <class>com.package.mvcfromscratch.entities.Event</class> 
     <properties> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="true" /> 
      <property name="hibernate.connection.driver_class" 
       value="org.postgresql.Driver" /> 
      <property name="hibernate.connection.url" 
       value="jdbc:postgresql://localhost/diarmaid" /> 
      <property name="hibernate.connection.username" 
       value="diarmaid" /> 
      <property name="hibernate.connection.password" 
       value="sqlol" /> 

      <property name="hibernate.c3p0.min_size" value="5" /> 
      <property name="hibernate.c3p0.max_size" value="20" /> 
      <property name="hibernate.c3p0.timeout" value="300" /> 
      <property name="hibernate.c3p0.max_statements" value="50" /> 
      <property name="hibernate.c3p0.idle_test_period" value="3000" /> 
     </properties> 
    </persistence-unit> 

</persistence> 

最後,applicationContext.xml中:

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

    <bean id="eventDao" class="com.package.mvcfromscratch.dao.PostgresEventDao" 
     autowire="byType"> 
     <property name="jpaTemplate" ref="jpaTemplate" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="Events" /> 
    </bean> 

    <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="myTransactionManager" /> 

    <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 
</beans> 
+0

另外,我在eclipse中以Run-> As Java Application的形式運行它。 – Diarmaid 2011-04-20 08:39:49

回答

3

你包括老版hibernate-commons-annotations,其中包括及物動詞JPA 1.0 ,這與Hibernate 3.5所需的JPA 2.0衝突。另外,由於Hibernate 3.5不再需要包含多個Hibernate構件,因此,您只需在pom.xml中聲明hibernate-entitymanager,對於您的設置就足夠了。

參見:

+0

謝謝 - 馬上解決它。我刪除了Hibernate的所有依賴關係,並將它運行。現在不同的錯誤信息,但完全不相關 - 只是我的錯誤編碼;) – Diarmaid 2011-04-20 08:45:30

2

它抱怨這行代碼中org.hibernate.ejb.QueryImpl

private javax.persistence.LockModeType jpaLockMode 
    = javax.persistence.LockModeType.NONE; 

由於javax.persistence.LockModeType.NONE是JPA2新的,我懷疑你也有JPA1在你的類路徑的某處。找到並移除它。

+0

正如axtavt建議的,我包括一箇舊版本的hibernate-commons-annotations。這確實包括JPA1,這是我的問題。感謝您的輸入=) – Diarmaid 2011-04-20 08:47:27