0

我尝试了不同的配置,但没有效果。错误保持不变。这里是从 BoneCPs 网站获取的所需配置:

<?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,我会在几个小时前启动并运行)

现在,我怎样才能让它运行?自动接线如何工作?

编辑:通过“已接受答案”的帮助找到的解决方案:

新的 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>

我不得不取消注释事务管理器部分,因此将数据源配置更改为我使用的配置不起作用(需要数据源)。

我还必须添加

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

到 AbstractHibernateDAO。我正在考虑写一篇博客文章并在这里建立一个链接。对于完全不熟悉 Spring 和 hibernate 的任何人来说,这将非常有用。

4

1 回答 1

1

你的spring xml中有这样的东西吗?

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

这会扫描包含注释的类。

于 2012-10-05T07:21:29.213 回答