使用Spring 3,maven,JPA,c3p0在hibernate中“java.lang.NoSuchFieldError:NONE”
问题:
Hibernate没有正确执行查询。 它出现了与slf4j相关的问题,但使用任何推荐的修复程序似乎都不起作用。
我已经为createQuery调用尝试了各种变量名组合,希望到目前为止我还没做好运,但是没有运气。 这个问题真让我难过,有没有人遇到过类似的东西?
堆栈跟踪:
Exception in thread "main" java.lang.NoSuchFieldError: NONE at org.hibernate.ejb.QueryImpl.(QueryImpl.java:604) at org.hibernate.ejb.QueryImpl.(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 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 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 listEvent) { for(Event event : listEvent) { getJpaTemplate().persist(event); } } }
persistence.xml中:
com.package.mvcfromscratch.entities.Event
最后,applicationcontext.xml:
你包含了旧版本的hibernate-commons-annotations
,其中包含了JPA 1.0,它与Hibernate 3.5所需的JPA 2.0冲突。
此外,从Hibernate 3.5开始,您不再需要包含多个Hibernate工件。 你只需要在pom.xml
声明hibernate-entitymanager
,它应该足够你的设置。
也可以看看:
- Hibernate兼容性矩阵
它在org.hibernate.ejb.QueryImpl
抱怨这个代码行:
private javax.persistence.LockModeType jpaLockMode = javax.persistence.LockModeType.NONE;
由于javax.persistence.LockModeType.NONE
在JPA2中是新的,我怀疑你的类路径上也有JPA1。 查找并删除它。
- 如何将用户创建的连接传递给hibernate
- JPA没有生成“on delete set null”FK限制
- 如何在struts2上应用Spring安全性
- Hibernate @Where子句
- 如何使用Java删除JSONArray元素
- Hibernate – PropertyNotFoundException:无法找到getter
- getSingleResult在hibernate中返回本机标量查询的代理
- “在注释ConcurrentHashMap时,在hibernate中非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements”
- 代理如何加载Hibernate / JPA中的惰性属性