我有一個問題與使用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.xml在META-INF/聲明來源。但是,如果我理解正確,Spring4可以在不使用persistance.xml的情況下配置項目。所以,我不使用它。
發佈該類的整個文件。 – chrylis
它在你的測試中運行良好嗎?因爲IDE試圖提供幫助,但有時它們不對,或者它們配置不正確,無法提供正確的消息。 –
我想這可能是有幫助的,http://stackoverflow.com/questions/21381943/how-to-configure-spring-without-persistence-xml –