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