Search code examples
hibernatecriteriaentitymanagerjpa

Can't create an instance of TypedQuery for executing a criteria query


I'm beginner in JPA. I just want to return all instances of the entity method getAll. But when I collect my project in WAR I get this Error error. I don't know what is the problrem. Maybe problem in my entity or context. Typical entity:

@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;
}

}

Context:

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


Solution

  • You're seemingly compiling against JPA 1.0 and the method invoked is in JPA 2.0+ (i.e EntityManager.createQuery(CriteriaQuery)). Consequently you should correct the JPA API jar you are compiling against to be v2.0 or v2.1.

    PS1. No idea why you don't just POST the code with the compiler error and the error itself in your post (instead of what code you did post, which clearly has nothing to do with a compiler error).

    PS2. Why you have some Hibernate specific stuff in your config, I've no idea. You create an EMF, so use it rather than some Hibernate SessionFactory ...