2016-08-14 28 views
0

我是JPA的初學者。 我只想返回實體method getAll的所有實例。但是當我在WAR中收集我的項目時,我得到這個錯誤error。 我不知道什麼是問題。也許問題在我的實體或上下文中。 典型的實體:無法創建用於執行條件查詢的TypedQuery實例

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

回答

-1

你似乎對編譯1.0 JPA和調用的方法是在JPA 2.0+(即EntityManager.createQuery(CriteriaQuery))。因此,您應該將您正在編譯的JPA API jar更正爲v2.0或v2.1。

PS1。不知道爲什麼你不只是在你的文章中發佈編譯器錯誤和錯誤本身的代碼(而不是你發佈的代碼,這顯然與編譯器錯誤無關)。

PS2。爲什麼你的配置中有一些Hibernate特定的東西,我不知道。你創建一個EMF,所以使用它而不是一些Hibernate SessionFactory ...