簡體   English   中英

Spring JPA“類不是實體”

[英]Spring-JPA “Class is not entity”

我的項目使用Spring(4.2.4 Release)和JPA(2.1)時遇到問題

簡而言之,問題在於文件“ 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,並指出該類不是實體。 而且我不知道,為什么如此,以至於我只是從書中做一個例子。

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

我的spring-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:我知道必須在META-INF /源代碼的persistence.xml中聲明實體。 但是,如果我正確理解Spring4,則可以在沒有persistance.xml的情況下配置項目。 所以,我不使用它。

如果使用InellyJ Idea,可能沒有在項目中添加JPA facet。 嘗試打開菜單“文件”->“項目結構” ,然后在“項目設置”部分的列表中選擇“ Facets” 檢查是否在構面列表中配置了JPA構面。 如果看不到JPA構面,只需添加它(按“ +”按鈕)。 (可選)您可以指定persistence.xml(如果有)和默認JPA提供程序。 希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM