2012-10-05 36 views
0

我嘗試了不同的配置,但無效。錯誤保持不變。這裏從BoneCPs網站採取所需的配置:Spring 3.1 + hibernate 4:無法運行

<?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:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.2.RELEASE.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.2.RELEASE.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
">  

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire-candidate="" autowire="autodetect"> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
       <prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop> 
       <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop> 
       <prop key="hibernate.connection.url">jdbc:postgresql:MyDB</prop> 
       <prop key="hibernate.connection.username">postgres</prop> 
       <prop key="hibernate.connection.password">123456</prop> 
       <prop key="bonecp.idleMaxAge">240</prop> 
       <prop key="bonecp.idleConnectionTestPeriod">60</prop> 
       <prop key="bonecp.partitionCount">1</prop> 
       <prop key="bonecp.acquireIncrement">5</prop> 
       <prop key="bonecp.maxConnectionsPerPartition">60</prop> 
       <prop key="bonecp.minConnectionsPerPartition">5</prop> 
       <prop key="bonecp.statementsCacheSize">50</prop> 
       <prop key="bonecp.releaseHelperThreads">2</prop> 
      </props> 
     </property> 
    </bean> 
    <!--<bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" /> --> 

    <bean id="AbstractHibernateDAO" abstract="true" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO"/> 

    <bean id="ChemicalStructureDAO" extends="AbstractHibernateDAO" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalStructureDAO"/> 
    <bean id="ChemicalCompoundDAO" extends="AbstractHibernateDAO" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalCompoundDAO"/> 
</beans> 

和代碼包含自動連接會話工廠:

@Repository 
public abstract class AbstractHibernateDAO< T extends Serializable> { 

    private final Class< T> clazz; 
    @Autowired 
    SessionFactory sessionFactory; 

    public AbstractHibernateDAO(final Class< T> clazzToSet){ 
     this.clazz = clazzToSet; 
    } 

    public T getById(final Long id) { 
     Preconditions.checkArgument(id != null); 
     return (T) this.getCurrentSession().get(this.clazz, id); 
    } 

    public List< T> getAll() { 
     return this.getCurrentSession() 
       .createQuery("from " + this.clazz.getName()).list(); 
    } 

    public void create(final T entity) { 
     Preconditions.checkNotNull(entity); 
     this.getCurrentSession().persist(entity); 
    } 

    public void update(final T entity) { 
     Preconditions.checkNotNull(entity); 
     this.getCurrentSession().merge(entity); 
    } 

    public void delete(final T entity) { 
     Preconditions.checkNotNull(entity); 
     this.getCurrentSession().delete(entity); 
    } 

    public void deleteById(final Long entityId) { 
     final T entity = this.getById(entityId); 
     Preconditions.checkState(entity != null); 
     this.delete(entity); 
    } 

    protected final Session getCurrentSession() { 
     return this.sessionFactory.getCurrentSession(); 
    } 
} 

當試圖創建一個新的實體(片段的最後一行),我得到一個錯誤:

ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 

ChemicalStructureDAO structureDAO = (ChemicalStructureDAO) context.getBean("ChemicalStructureDAO"); 

ChemicalStructure structure1 = new ChemicalStructure(); 
structure1.setStructureKey("c1ccccc1"); 
structure1.setStructureData("c1ccccc1"); 

structureDAO.create(structure1); 

我得到一個NullPointerException:

java.lang.NullPointerException 
    at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.getCurrentSession(AbstractHibernateDAO.java:78) 
    at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.create(AbstractHibernateDAO.java:54) 
    at org.bitbucket.myName.moleculedatabaseframework.App.main(App.java:32) 
------------------------------------------------------------------------ 

也許我誤解了autowired意味着什麼?我認爲那個屬性會自動設置。所以我嘗試以下操作:

ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 
SessionFactory sessionfactory = (SessionFactory)context.getBean("sessionFactory"); 

ChemicalStructureDAO structureDAO = (ChemicalStructureDAO) context.getBean("ChemicalStructureDAO"); 
structureDAO.setSessionFactory(sessionfactory); 

ChemicalStructure structure1 = new ChemicalStructure(); 
structure1.setStructureKey("c1ccccc1"); 
structure1.setStructureData("c1ccccc1"); 

structureDAO.create(structure1); 

這將導致以下錯誤:

Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread 
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) 
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941) 
at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.getCurrentSession(AbstractHibernateDAO.java:78) 
at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.create(AbstractHibernateDAO.java:54) 

我看着噸的教程,但他們都忽略了什麼似乎是基本的東西,得到的東西運行,例如。 ApplicationContext context = new ClassPathXmlApplicationContext(「ApplicationContext.xml」);沒有出現在任何spring + hibernate教程中。有人能給我一個完整的教程,假設我完全愚蠢,並告訴我每一步都需要,並且有一個實際運行時重複代碼的應用程序? (是的,現在變得非常沮喪了,說實話,如果我明白了jdbc,我會在幾個小時前開始運行)

現在,我該如何運行?自動裝配工作如何?

編輯: AS FOUND通過 「接受的答案」 的幫助下,解決方案:

新的Spring配置文件:

<?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:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx" 

     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
">  


    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="autodetect"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalStructure</value> 
       <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompound</value> 
       <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompoundComposition</value> 
      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
      </props> 
     </property> 
    </bean>  

    <!-- Spring bean configuration. Tell Spring to bounce off BoneCP --> 
    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> 
     <property name="targetDataSource"> 
      <ref local="mainDataSource" /> 
     </property> 
    </bean> 

    <!-- BoneCP configuration --> 
    <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> 
     <property name="driverClass" value="org.postgresql.Driver" /> 
     <property name="jdbcUrl" value="jdbc:postgresql:MolDB" /> 
     <property name="username" value="postgres"/> 
     <property name="password" value="123456"/> 
     <property name="idleConnectionTestPeriod" value="60"/> 
     <property name="idleMaxAge" value="240"/>  
     <property name="maxConnectionsPerPartition" value="60"/> 
     <property name="minConnectionsPerPartition" value="20"/> 
     <property name="partitionCount" value="3"/> 
     <property name="acquireIncrement" value="10"/>        
     <property name="statementsCacheSize" value="50"/> 
     <property name="releaseHelperThreads" value="3"/> 
    </bean> 

    <bean id="txManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" /> 

    <context:annotation-config /> 

    <bean id="AbstractHibernateDAO" abstract="true" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO"/> 

    <bean id="ChemicalStructureDAO" parent="AbstractHibernateDAO" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalStructureDAO"/> 
    <bean id="ChemicalCompoundDAO" parent="AbstractHibernateDAO" 
      class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalCompoundDAO"/> 
</beans> 

我不得不添加

<context:annotation-config /> 

到文件並在sessionFactory配置中聲明已註釋的實體類:

<property name="annotatedClasses"> 
    <list> 
     <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalStructure</value> 
     <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompound</value> 
     <value>org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompoundComposition</value> 
    </list> 
</property> 

我不得不取消對事務管理器部分的註釋,並且因爲更改了數據源配置,因爲我使用的配置不起作用(需要DataSource)。

我也不得不

@Repository 
@Transactional 
public abstract class AbstractHibernateDAO< T extends Serializable> { 
    //code... 
} 

添加到AbstractHibernateDAO。我正在考慮編寫一篇博客文章並在此處建立鏈接。對於任何人來說,新的Spring和hibernate將非常有用。

+0

你可以給你的applicationContext.xml或者一個xml在spring初始化的時候...... – sgpalit

回答

1

在spring xml中你有這樣的東西嗎?

<context:annotation-config /> 
<context:component-scan base-package="base.package" /> 

這將掃描包含註釋的類。

+0

用自動裝配解決了這個問題。我仍然得到:異常在線程「main」org.hibernate.HibernateException:沒有發現當前線程的會話 –

+1

您取消註釋transactionManager Bean和tx:註釋驅動? txManager應該與TransactionManager的id相同。 – sgpalit

+0

是的。謝謝。這也有幫助。之後,我遇到了其他幾個問題,但現在我得到了它的工作!我正在考慮創建一篇博文。完整的答案太大而不能放在這裏。我會將yoru問題標記爲答案,並將更新後的配置添加到我的初始帖子中。 –

相關問題