当前位置: 首页 > 面试题库 >

在Spring 3,maven,JPA,c3p0中处于休眠状态的“ java.lang.NoSuchFieldError:NONE”

太叔英卫
2023-03-14
问题内容

问题:

Hibernate未正确执行查询。它因似乎与slf4j相关的问题而出错,但使用任何建议的修复程序似乎均无效。

我已经为createQuery调用尝试了各种变量名组合,希望我做的是Doing It
Wrong(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>

问题答案:

您包括的旧版本hibernate-commons-annotations,该版本可传递地包括JPA 1.0,该版本与Hibernate
3.5所需的JPA 2.0冲突。

而且,由于Hibernate 3.5,您不再需要包含多个Hibernate构件。你只需要声明hibernate- entitymanagerpom.xml,它应该在你的系统是不够的。

也可以看看:

  • hibernate兼容性矩阵


 类似资料:
  • 问题内容: 问题: Hibernate未正确执行查询。它因似乎与slf4j有关的问题而出错,但使用任何建议的修复程序似乎均无效。 我已经为createQuery调用尝试了各种变量名组合,希望我做的是Doing It Wrong(TM),但到目前为止还没有碰到运气。这个问题真的让我感到难过,有人遇到过类似的事情吗? 堆栈跟踪: Maven依赖树: PostgreSQL数据库: UserInterfa

  • 地址 address_id INT PK AutoIncr 城市Varchar 国家varchar 员工 null

  • 问题内容: 我想执行类似的查询 如果我使用neProperty()函数,它将返回记录为 如何使用hibernate条件实施? 谢谢 问题答案: 创建一个全选条件: 然后,您可以对其添加限制,即第1列= 8等,如下所示: 最后,您可以提供not in子句,如下所示:

  • 问题内容: 我正在将c3p0和hibernate 3用于程序,该程序不断从某些源中提取数据并将其写入数据库。现在的问题是,由于某些原因,数据库可能变得不可用(在最简单的情况下:我只是将其关闭)。 如果有什么要写入数据库,则不应有任何异常- 查询应等待所有永恒,直到数据库再次可用。如果我没记错的话,这就是连接池可以为我做的事情之一:如果数据库有问题,只需重试连接-在最坏的情况下,无限即可。 但是相反

  • 问题内容: 有人可以透视一下JPA和Hibernate之间的区别吗?还是将这些互补的概念一起使用? 问题答案: 大致来说,JPA是java社区的一个标准,这里是specs,它是由Hibernate家伙实现(并扩展)的(此处提供一些信息)。作为规范,您将不会直接使用JPA,而是使用JPA实现。 请注意,如果要使用hibernateJPA扩展,将破坏与其他JPA实现的兼容性(尽管有些人会说“为什么要使

  • 问题内容: 我需要在hibernate状态下禁用ONLY_FULL_GROUP_BY。这是我当前的会话工厂。我不确定如何在其中指定sql_mode =’‘。 问题答案: 我认为您可以在JDBC连接字符串中进行设置,例如