" java.lang.NoSuchFieldError:NONE"在与Spring 3,Maven,JPA,c3p0的休眠 [英] "java.lang.NoSuchFieldError: NONE" in hibernate with Spring 3, maven, JPA, c3p0

查看:212
本文介绍了" java.lang.NoSuchFieldError:NONE"在与Spring 3,Maven,JPA,c3p0的休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

Hibernate没有正确执行查询。它看起来与slf4j有关的问题的错误,但使用任何推荐的修补程序似乎并不奏效。



我尝试过各种变量名称组合createQuery调用,希望我做得不对(TM),但目前还没有运气。这个问题真的让我难住了,有没有人遇到过类似的东西?



堆栈跟踪:

 线程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。在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)上的
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)处
在java中为
。 lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:341)
at $ Proxy12.createQuery(Unknown源)
在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。
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
在$ Proxy11.numberOfEvents(未知来源)
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:编译
[INFO] + - org.springframework:spring-web:jar:3.0.3.RELEASE:compile
[INFO] + - org.springframework :spring-core:jar:3.0.3.RELEASE:编译
[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:编译
[INFO] | \- org.slf4j:slf4j-api:jar:1.5.8:编译
[INFO] + - org.hibernate:hibernate-core:jar:3.5.3-Final:编译
[INFO ] | + - antlr:antlr:jar:2.7.6:编译
[INFO] | + - commons-collections:commons-collections:jar:3.1:编译
[INFO] | + - dom4j:dom4j:jar:1.6.1:编译
[INFO] | | \ -xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | \- javax.transaction:jta:jar:1.1:编译
[INFO] + - org.hibernate:hibernate-annotations:jar:3.5.3-Final:编译
[信息] + - org .hibernate:hibernate-commons-annotations:jar:3.3.0.ga:compile
[INFO] | \ javax.persistence:persistence-api:jar:1.0:编译
[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:编译
[INFO] | + - cglib:cglib:jar:2.2:编译
[INFO] | | \ - asm:asm:jar:3.1:编译
[INFO] | \ javassist:javassist:jar:3.9.0.GA:编译
[INFO] + - postgresql:postgresql:jar:8.4-702.jdbc4:编译
[INFO] \- org。 slf4j:slf4j-simple:jar:1.5.8:编译

PostgreSql数据库:

  diarmaid =#\d事件; 
表public.event
Column |类型|修饰符
--------------- + ------------------------ + ---- -------------------------------------------------- ---
eventid |整数| not null default nextval('event_eventid_seq':: regclass)
eventname |字符变化(255)|
eventdate |字符变化(255)|
eventcapacity | bigint |
索引:
event_pkeyPRIMARY KEY,btree(eventid)

UserInterface class:

  public class UserInterface {

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

//是否有任何事件?
System.out.println(Events:+ eventDao.numberOfEvents());
//获取所有事件
列表< Event> listEvents = eventDao.getAllEvents();
//&全部打印出来。
for(Event event:listEvents){
System.out.println(event);
}
System.out.println(eventDao.getEvent(2));


$ b $ / code
$ b PostgresEventDao类:

  public class PostgresEventDao extends JpaDaoSupport implements EventDao {

@Override
public String numberOfEvents(){
return(String)getJpaTemplate()
.getEntityManagerFactory()
.createEntityManager()
.createQuery(事件ev中的select count(ev.id))
。 getSingleResult();
}

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

$ b @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.0encoding =UTF-8?> 
< persistence version =2.0
xmlns =http://java.sun.com/xml/ns/persistencexmlns: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>
<属性>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =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_sizevalue =5/>
< property name =hibernate.c3p0.max_sizevalue =20/>
< property name =hibernate.c3p0.timeoutvalue =300/>
< property name =hibernate.c3p0.max_statementsvalue =50/>
< property name =hibernate.c3p0.idle_test_periodvalue =3000/>
< / properties>
< / persistence-unit>

< /持久性>

最后,applicationcontext.xml:

 <?xml version =1.0encoding =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/模式/ TX /弹簧-TX-2.0.XSD>

< bean id =eventDaoclass =com.package.mvcfromscratch.dao.PostgresEventDao
autowire =byType>
< property name =jpaTemplateref =jpaTemplate/>
< / bean>

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

< bean id =jpaTemplateclass =org.springframework.orm.jpa.JpaTemplate>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

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

< bean id =myTransactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< / beans>


解决方案

您包含旧版本的 hibernate-commons-annotations ,它传递地包含JPA 1.0,它与Hibernate 3.5所要求的JPA 2.0冲突。

另外,从Hibernate 3.5开始不再需要包含多个Hibernate构件。你只需要在 pom.xml 中声明 hibernate-entitymanager ,它对你的设置就足够了。



另见: ://community.jboss.org/wiki/HibernateCompatibilityMatrixrel =noreferrer> Hibernate Compatibility Matrix

Problem:

Hibernate is not executing queries correctly. It errors with a problem that appears to be related to slf4j but using any recommended fixes don't seem to work.

I've tried various combinations of variable names for the createQuery call, in hopes I was Doing It Wrong(TM) but no luck so far. This problem really has me stumped, has anyone come across something similar before?

Stack Trace:

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 dependency tree:

[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 database:

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 class:

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 class:

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>

And finally, 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>

解决方案

You included old version of hibernate-commons-annotations, which transitively included JPA 1.0, which conflicts with JPA 2.0 required by Hibernate 3.5.

Moreover, since Hibernate 3.5 you don't need to include multiple Hibernate artifacts anymore. You only need to declare hibernate-entitymanager in pom.xml, it should be enough for your setup.

See also:

这篇关于&quot; java.lang.NoSuchFieldError:NONE&quot;在与Spring 3,Maven,JPA,c3p0的休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆