我有Hibernate依賴一個Spring MVC的項目:的EntityManager是空Spring MVC中控制器
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.0.Final</version>
</dependency>
<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.0.Final</version>
</dependency>
<!-- optional -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.0.Final</version>
</dependency>
這是我簡單的控制器:
@EnableTransactionManagement
@Controller
public class HomeController {
@PersistenceContext
private EntityManager em;
@Transactional
@RequestMapping("/home")
public String goHome(){
System.out.println(em);
return "home";
}
}
的applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="someDataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.
InstrumentationLoadTimeWeaver"/>
</property>
</bean>
林不知道我應該把這裏:
<property name="dataSource" ref="someDataSource"/>
什麼是休眠的情況下,數據源引用? 這一塊配置的來自spring dokumentation
你能確定你沒有NEW這個類的實例嗎?這經常是值得提問的錯誤。 – NawaMan
你能顯示上下文文件嗎?你是否初始化entitymanager bean? – pezetem
@NawaMan - 這就是我所擁有的,只有這個單一的控制器類。 – jarosik