【问题标题】:Hibernate is committing but not savingHibernate 正在提交但不保存
【发布时间】:2014-09-28 06:54:21
【问题描述】:

我正在使用带有 Postgresql 数据库的 Hibernate 4。选择查询执行良好,但在更新查询(插入)时根本不起作用。

日志没有说明任何错误,一切似乎都正常并已提交。我正在尝试使用 Dao 层保存对象:

public void add(Role role) {
    sessionFactory.openSession().save(role);
}

服务层:

@Transactional
public void addNewRole(Role role) {
    roleDao.add(role);      

}

角色对象被映射,会话工厂被注入没有任何问题。这是日志所说的:

2014-09-28 10:44:52 DEBUG HibernateTransactionManager:367 - Creating new transaction with  name [com.x.y.base.services.imp.RoleServiceImp.addNewRole]:  PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:420 - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@66f9104a updates=org.hibernate.engine.spi.ExecutableList@699c9f16 deletions=org.hibernate.engine.spi.ExecutableList@29909385 orphanRemovals=org.hibernate.engine.spi.ExecutableList@52c51614 collectionCreations=org.hibernate.engine.spi.ExecutableList@92ca580 collectionRemovals=org.hibernate.engine.spi.ExecutableList@52257b34 collectionUpdates=org.hibernate.engine.spi.ExecutableList@1abbbd0e collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1b78efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:430 - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@66f9104a updates=org.hibernate.engine.spi.ExecutableList@699c9f16 deletions=org.hibernate.engine.spi.ExecutableList@29909385 orphanRemovals=org.hibernate.engine.spi.ExecutableList@52c51614 collectionCreations=org.hibernate.engine.spi.ExecutableList@92ca580 collectionRemovals=org.hibernate.engine.spi.ExecutableList@52257b34 collectionUpdates=org.hibernate.engine.spi.ExecutableList@1abbbd0e collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1b78efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
2014-09-28 10:44:52 DEBUG DriverManagerDataSource:142 - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/moe]
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:491 - Exposing Hibernate transaction as JDBC transaction [org.postgresql.jdbc4.Jdbc4Connection@11fb24d3]
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:755 - Initiating transaction commit
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:554 - Committing Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@66f9104a updates=org.hibernate.engine.spi.ExecutableList@699c9f16 deletions=org.hibernate.engine.spi.ExecutableList@29909385 orphanRemovals=org.hibernate.engine.spi.ExecutableList@52c51614 collectionCreations=org.hibernate.engine.spi.ExecutableList@92ca580 collectionRemovals=org.hibernate.engine.spi.ExecutableList@52257b34 collectionUpdates=org.hibernate.engine.spi.ExecutableList@1abbbd0e collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1b78efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:636 - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@66f9104a updates=org.hibernate.engine.spi.ExecutableList@699c9f16 deletions=org.hibernate.engine.spi.ExecutableList@29909385 orphanRemovals=org.hibernate.engine.spi.ExecutableList@52c51614 collectionCreations=org.hibernate.engine.spi.ExecutableList@92ca580 collectionRemovals=org.hibernate.engine.spi.ExecutableList@52257b34 collectionUpdates=org.hibernate.engine.spi.ExecutableList@1abbbd0e collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1b78efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction

spring-db.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:properties/db.properties</value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driverClassName}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>mapping/User.hbm.xml</value>
            <value>mapping/Role.hbm.xml</value>
            <value>mapping/Permission.hbm.xml</value>
            <value>mapping/Representation.hbm.xml</value>
            <value>mapping/Right.hbm.xml</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.connection.autocommit">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
        </props>
    </property>
</bean>

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

测试应用:

public static void main(String[] args) {

    ApplicationContext cx = new ClassPathXmlApplicationContext("spring/spring-all.xml");
    RoleService roleService = (RoleService) cx.getBean("roleService");

    Role role = new Role();
    role.setCode("TEST");
    role.setName("Test");
    role.setDescription("Test Role");

    roleService.addNewRole(role);

}

对象根本没有保存在物理数据库中?我检查了数据库用户的权限,但似乎对所有操作都有完全的超级访问权限!

可能是什么原因?

【问题讨论】:

  • 如果您尝试在没有休眠的情况下运行类似的插入,它是否有效?您是否尊重所有非空、外键等约束?
  • 正常插入工作,所有字段都提交,正常表,除主键外无约束!
  • 您可以通过向主键添加值来测试您的休眠命令吗?我不喜欢休眠,我只是给出一些一般性的想法。
  • 主键不是(自动),所以我必须在表单中手动输入。身份证已提交!还是不行!
  • 不确定是否相关,但在休眠属性中有一个错字:你写了hibernate.conntection .autocommit (with t)...

标签: java xml spring hibernate postgresql


【解决方案1】:

您正在 DAO 中打开一个新会话,该会话未链接到当前 Spring 事务。不要那样做。相反,获取与当前事务相关联的当前会话:

Session session = sessionfactory.getCurrentSession();

【讨论】:

  • 非常感谢,这是正确答案,你拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 2021-07-03
  • 2013-12-20
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2012-07-16
  • 2011-12-14
相关资源
最近更新 更多