【问题标题】:Can't create an instance of TypedQuery for executing a criteria query无法创建 TypedQuery 的实例来执行条件查询
【发布时间】:2016-08-14 12:03:23
【问题描述】:

我是 JPA 的初学者。 我只想返回实体method getAll 的所有实例。但是当我在 WAR 中收集我的项目时,我得到了这个错误error。 我不知道是什么问题。也许在我的实体或上下文中存在问题。 典型实体:

@Entity
public class Currency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;



public Currency() {

}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config/>
<tx:annotation-driven/>

<context:component-scan base-package="ru.stasdev"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mydbtest"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    <property name="initialSize" value="3" />
    <property name="maxActive" value="10" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="ru.stasdev.domain"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>

</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="ru.stasdev"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="database" value="MYSQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    </property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="validation"/>
</bean>

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

【问题讨论】:

    标签: hibernate jpa criteria entitymanager javax.persistence


    【解决方案1】:

    您似乎正在针对JPA 1.0 进行编译,并且调用的方法位于JPA 2.0+(即EntityManager.createQuery(CriteriaQuery))中。因此,您应该将您正在编译的 JPA API jar 更正为 v2.0 或 v2.1。

    PS1。不知道为什么你不只是在你的帖子中发布带有编译器错误的代码和错误本身(而不是你发布的代码,这显然与编译器错误无关)。

    PS2。为什么你的配置中有一些 Hibernate 特定的东西,我不知道。你创建了一个 EMF,所以使用它而不是一些 Hibernate SessionFactory ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 2019-08-01
      • 2011-12-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      相关资源
      最近更新 更多