【发布时间】:2016-03-30 22:33:52
【问题描述】:
我的项目使用 Spring(4.2.4 版本)和 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;
}
}
特别是
query = "从 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>
P.S:我知道实体必须在 META-INF/ 源的 persistence.xml 中声明。但如果我理解正确,Spring4 可以在没有persistance.xml 的情况下配置项目。所以,我不使用它。
【问题讨论】:
-
为班级发布整个文件。
-
它在您的测试中运行良好吗?因为 IDE 试图提供帮助,但有时它们并不正确,或者它们的配置不够正确,无法提供正确的消息。
-
我想这可能会有所帮助,stackoverflow.com/questions/21381943/…
-
@chrylis 我已经完成了)
-
@JBNizet 事实上我不为这个项目使用测试:)
标签: java spring jpa intellij-idea