2015-09-01 96 views
0

我有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

+0

你能確定你沒有NEW這個類的實例嗎?這經常是值得提問的錯誤。 – NawaMan

+0

你能顯示上下文文件嗎?你是否初始化entitymanager bean? – pezetem

+0

@NawaMan - 這就是我所擁有的,只有這個單一的控制器類。 – jarosik

回答

0

試試這個應用程序上下文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> 

    <context:component-scan base-package="your.package.for.controller" /> 

<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> 
+0

我不會使用ref =「someDataSource」 – jarosik

+0

@jarosik唯一的區別在於xml中的這一行: Atul

0

看看下面的Spring配置(數據源,組件掃描目錄和休眠特性應調整到您的需要):

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" 

    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/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 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd"> 

// will enable jpa repositories if you use one 
    <jpa:repositories base-package="com.xyz.database.repository"/> 
// allow usage of transactional annotation 
    <tx:annotation-driven transaction-manager="transactionManager"/> 
//scans for spring components 
    <context:component-scan base-package="com.xyz"/> 
// dataSource creates connection to database 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}"/> 
    <property name="url" value="${jdbc.url}"/> 
    <property name="password" value="${jdbc.password}"/> 
    <property name="username" value="${jdbc.username}"/> 
</bean> 

// create entity manager 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> 
     <property name="packagesToScan" value="com.xyz"/> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.jdbc.fetch_size">10000</prop> 
       <prop key="hibernate.connection.autocommit">true</prop> 
       <prop key="hibernate.jdbc.batch_size">100</prop> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.format_sql">false</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 

// create transaction manager 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
     <property name="dataSource" ref="dataSource"/> 
     </bean> 
</beans> 

後,您可以使用:

@PersistenceContext 
private EntityManager em; 
+0

我有「元素*的前綴*沒有綁定所有三個: ​​ – jarosik

+0

哦,我忘了你還應該添加schemaLocation到beans - 我更新了 – pezetem

+0

錯誤:cvc-complex-type.2.3:元素'beans'不能包含character [children],因爲這個類型的內容類型是element-only。 – jarosik