Java Hibernate正在提交但未保存

Java Hibernate正在提交但未保存,java,xml,spring,hibernate,postgresql,Java,Xml,Spring,Hibernate,Postgresql,我正在使用Hibernate4和Postgresql数据库。选择查询执行得很好,但在更新查询(插入)时根本不起作用 日志中没有任何关于错误的信息,所有的事情看起来都很正常。我正在尝试使用Dao层保存对象: public void add(Role role) { sessionFactory.openSession().save(role); } 服务层: @Transactional public void addNewRole(Role role) { roleDao.ad

我正在使用Hibernate4和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>
对象根本没有保存在物理数据库中?我检查了数据库用户的权限,但它似乎拥有对所有操作的完全超级访问权限


原因可能是什么

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

Session session = sessionfactory.getCurrentSession();

您正在DAO中打开一个新会话,而不是链接到当前的Spring事务。不要那样做。而是获取链接到当前事务的当前会话:

Session session = sessionfactory.getCurrentSession();

您正在DAO中打开一个新会话,而不是链接到当前的Spring事务。不要那样做。而是获取链接到当前事务的当前会话:

Session session = sessionfactory.getCurrentSession();

您正在DAO中打开一个新会话,而不是链接到当前的Spring事务。不要那样做。而是获取链接到当前事务的当前会话:

Session session = sessionfactory.getCurrentSession();


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