Hibernate承诺但不保存

时间:2014-09-28 06:54:22

标签: java xml spring hibernate postgresql

我正在使用Hibernate 4和Postgresql数据库。选择查询执行得很好,但是当涉及更新查询时(插入)根本不起作用。

日志没有说出任何关于错误的信息,一切似乎都没问题。我试图使用Dao图层保存对象:

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

服务层:

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

}

映射角色对象,注入sessionfactory没有任何问题。以下是日志所说的内容:

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);

}

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

可能是什么原因?

1 个答案:

答案 0 :(得分:3)

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

Session session = sessionfactory.getCurrentSession();