java - spring 应用程序的 hibernate 配置错误?

标签 java spring hibernate hibernate-session

我的 Spring 应用程序中的 hibernate 有问题。我想肯定是配置有问题。

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cloud_app?autoReconnect=true</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.release_mode">after_transaction</property>
        <property name="show_sql">true</property>

    ...

persistanceContext.xml

<bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cloud_app" />
        <property name="user" value="root" />
        <property name="password" value="root" />
        <property name="debugUnreturnedConnectionStackTraces" value="true" />
        <property name="unreturnedConnectionTimeout" value="20" />
        <property name="minPoolSize" value="5" />
        <property name="initialPoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="maxStatements" value="50" />
        <property name="idleConnectionTestPeriod" value="120" />
        <property name="maxIdleTime" value="1200" />
    </bean>

    <!-- Hibernate -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>

当我在应用程序中使用sessionFactory.getCurrentSession()时,它仍然抛出异常

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

但是当我使用sessionFactory.openSession()时,它解决了问题,但又产生了另一个问题。所以我的问题是,我怎样才能达到一种状态——整个应用程序一次 session 。当我调用 sessionFactory.getCurrentSession() 时, session 将存在。 我很困惑,我读了很多帖子如何解决它,但没有成功。

更新 当我得到一些对象然后我尝试改变它

Opportunity opportunity = opportunityDao.get(idOpportunity);
opportunity.setOrder(order);
opportunityDao.edit(opportunity);

来自 get 方法的代码

public T get(Integer id) {
    T object = (T) sessionFactory.getCurrentSession().get(clazz, id);
    return object;
}

来自编辑方法的代码

public void edit(T object) {
    this.sessionFactory.getCurrentSession().update(object);
}

它抛出了我org.hibernate.HibernateException:非法尝试将集合与两个打开的 session 关联

最佳答案

有几件事

  1. 您的hibernate.connection.*当您注入(inject) DataSource 时,属性是无用的在你的 Spring 应用程序中
  2. dialect 和 show_sql 的其他属性已在 spring 配置中设置,因此简而言之(由于 1 和 2)您可以删除 hibernate.cfg.xml文件。
  3. 使用 AnnotationSessionFactoryBean便利类。

留给你这个

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.bytecode.use_reflection_optimize">false</prop>
        </props>
    </property>
    <property name="packagesToScan" value="your.package.with.entities.here" />
</bean>

这至少清理了您的配置(并将所有内容移至单个文件中)。

您遇到的问题是由于事务配置不正确造成的。你有如何,HibernateTransactionManager但不是配置的时间/地点。我期望 <tx:annotation-driven /><tx:advice /><aop:config />启用交易。

我建议阅读transaction chapter在 Spring 引用指南中,有关 hibernate 的更多信息请阅读 the hibernate section .

关于java - spring 应用程序的 hibernate 配置错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20370014/

相关文章:

java - 如何在没有 FXML 的情况下创建自定义复合控件?

java - 从 NSIS 检测已经运行的 Java 应用程序

使用 Arraylist 进行快速排序的 Java 实现疑难解答

java - 原型(prototype) spring bean 只给我一个类的实例

java - 构造函数中的 Spring Data JPA JPQL 列表

java - JBoss Picketlink - 自定义用户模型身份验证失败

java - Spring Rest 重复检查在同时运行 100 个用户的服务时跳过一些时间

java - 当 Camel 从 XML 文件加载路由时,在注册表中找不到 Bean

hibernate - 带有参数的JPQL ORDER BY子句

java - Hibernate 一对一条件获取