2011-02-09 180 views
1

我是Spring Framework(2.5.6)的新手,我在Glassfish服務器(開源版本3)上遇到了Hibernate集成問題。我會補充一點,當我使用Tomcat服務器(v 7.0)時,一切正常。sessionFactory - Spring hibernate集成問題

我有以下代碼(springwebglassfishhibernate-data.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:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd"> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="carRentalPersistenceUnit" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
    factory-bean="entityManagerFactory" factory-method="getSessionFactory"> 
</bean> 

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

但是當我運行服務器,我收到以下錯誤:

SEVERE: Exception while loading the app 
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/configuration_xmls/springwebglassfishhibernate-data.xml]: No matching factory method found: factory bean 'entityManagerFactory'; factory method 'getSessionFactory' 
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:117) 
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126) 
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241) 
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:236) 
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:339) 
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183) 
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235) 
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224) 
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:365) 
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:204) 
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166) 
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:69) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 
at java.lang.Thread.run(Thread.java:662) 

也許,我會貼上我的POM的一部分.xml文件顯示依賴關係:

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring</artifactId> 
     <version>2.5.6</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>2.5.6</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-validator</groupId> 
     <artifactId>commons-validator</artifactId> 
     <version>1.3.1</version> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>commons-digester</groupId> 
     <artifactId>commons-digester</artifactId> 
     <version>2.1</version> 
    </dependency> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
     <version>1.3.146</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-hibernate2</artifactId> 
     <version>2.0.8</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>3.3.2.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-annotations</artifactId> 
     <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>oro</groupId> 
     <artifactId>oro</artifactId> 
     <version>2.0.8</version> 
    </dependency> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>6.0</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>1.5.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.xmlgraphics</groupId> 
     <artifactId>fop</artifactId> 
     <version>1.0</version> 
    </dependency> 
    <dependency> 
     <groupId>xalan</groupId> 
     <artifactId>xalan</artifactId> 
     <version>2.7.1</version> 
    </dependency> 
    <dependency> 
     <groupId>struts</groupId> 
     <artifactId>struts</artifactId> 
     <version>1.2.9</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-struts</artifactId> 
     <version>2.0.8</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.portlet</groupId> 
     <artifactId>portlet-api</artifactId> 
     <version>2.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-api</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-jsp</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-compat</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-core</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-servlet-wildcard</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tiles</groupId> 
     <artifactId>tiles-el</artifactId> 
     <version>2.2.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>jsf-api</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>jsf-impl</artifactId> 
     <version>1.2-b19</version> 
    </dependency> 
    <dependency> 
     <groupId>javassist</groupId> 
     <artifactId>javassist</artifactId> 
     <version>3.1</version> 
    </dependency> 
    <dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>ejb3-persistence</artifactId> 
     <version>3.3.2.Beta1</version> 
    </dependency> 
    <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-c3p0</artifactId> 
     <version>3.3.2.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.maven</groupId> 
     <artifactId>maven-script-ant</artifactId> 
     <version>2.2.1</version> 
    </dependency> 
</dependencies> 

這個問題的最佳解決方案是什麼?

回答

3

我不想讓Hibernate的Session工廠到這一點。 JPA定義了所謂的供應商適配器,因此您可以使用它。下面是從我目前的工作配置一個片段:

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="puName" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter"> 
     <bean 
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="${hibernate.showSql}" /> 
      <property name="databasePlatform" value="${hibernate.dialect}" /> 
      <property name="generateDdl" value="${hibernate.generateDDL}" /> 
     </bean> 
    </property> 
</bean> 

(在${..}屬性可以通過PropertyPlaceholderConfigurer解決你也可以把它們直接出現。)

那麼你當然應該使用JPA事務管理器:

<bean id="jpaTransactionManager" 
    class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

並使用@PersistenceContext private EntityManager entityManager;而不是hibernate會話。

1

將其更改爲

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
    factory-bean="entityManagerFactory" factory-method="getSessionFactory"> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
      p:dataSource-ref="dataSource"> 
     <property name="annotatedClasses"> 
      <list> 
       <value>com.mycompany.app.model.Book</value> 
      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <value> 

       hibernate.dialect org.hibernate.dialect.PostgreSQLDialect 
       REST OF THE PROPS    
      </value> 
     </property> 
    </bean> 

和配置數據源正確