2015-12-24 113 views
0

我有一個問題與使用Spring(4.2.4版本)和JPA我的項目(2.1)春JPA「類不是實體」

簡短的問題是在代碼文件中的一部分「 Book.java「:

@Table(name = "book") 
@NamedQueries({ 
     @NamedQuery(name = "Book.getBooks", query="select b from com.jpaProSpring.Book b ") 
}) 
@Entity(name = "Book") 
public class Book implements Serializable { 
    private int id; 
    private String title; 
    private String description; 

    public Book() { 

    } 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id") 
    public int getId() { 
     return this.id; 
    } 

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

    @Column 
    public String getTitle() { 
     return this.title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    @Column 
    public String getDescription() { 
     return this.description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 
} 

和格外在

查詢= 「選擇從com.jpaProSpring.Book b b」)

我的IntelliJ突出顯示Book並且表示該類不是en實體。我不知道,爲什麼這麼認爲我正在從這本書中做一個例子。

鏈接到我的項目https://github.com/yuraguz/LearnORM.git

我的春天-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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:jpa="http://www.springframework.org/schema/data/jpa" 
     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/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd 
     "> 


     <tx:annotation-driven transaction-manager="transactionManager" /> 

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

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

     <bean id="emf" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
       <property name="dataSource" ref="dataSource" /> 
       <property name="jpaVendorAdapter"> 
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
       </property> 
       <property name="packagesToScan" value="com.jpaProSpring" /> 
       <property name="jpaProperties"> 
        <props> 
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
         <prop key="hibernate.max_fetch_depth">3</prop> 
         <prop key="hibernate.jdbc.fetch_size">50</prop> 
         <prop key="hibernate.jdbc.batch_size">10</prop> 
         <prop key="hibernate.show_sql">true</prop> 
        </props> 
       </property> 
     </bean> 

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
     <context:annotation-config /> 
     <context:component-scan base-package="com.jpaProSpring" /> 
</beans> 

PS:我知道實體必須的persistence.xmlMETA-INF/聲明來源。但是,如果我理解正確,Spring4可以在不使用persistance.xml的情況下配置項目。所以,我不使用它。

+0

發佈該類的整個文件。 – chrylis

+0

它在你的測試中運行良好嗎?因爲IDE試圖提供幫助,但有時它們不對,或者它們配置不正確,無法提供正確的消息。 –

+0

我想這可能是有幫助的,http://stackoverflow.com/questions/21381943/how-to-configure-spring-without-persistence-xml –

回答

2

如果您使用InellyJ Idea,可能您沒有在項目中添加JPA構面。嘗試打開菜單「文件」 - >「項目結構」,然後在名爲「小平面」的項目設置部分列表中進行選擇。檢查是否已在facet列表中配置JPA構面。如果你沒有看到JPA facet,只需添加它(通過按「+」按鈕)。或者,您可以指定persistence.xml(如果有的話)和Default JPA提供程序。希望這會有所幫助。

+0

是的,它的工作原理! 非常感謝大家!當一切正常時,這是一種難以置信的感覺:) yeeeah – Yury