我正在使用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[[email protected] [email protected] [email protected] [email protected] [email protected]580 [email protected]b34 [email protected]0e [email protected]efd8 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[[email protected] [email protected] [email protected] [email protected] [email protected]580 [email protected]b34 [email protected]0e [email protected]efd8 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 [[email protected]]
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[[email protected] [email protected] [email protected] [email protected] [email protected]580 [email protected]b34 [email protected]0e [email protected]efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
2014-09-28 10:44:52 DEBUG HibernateTransactionManager:636 - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[[email protected] [email protected] [email protected] [email protected] [email protected]580 [email protected]b34 [email protected]0e [email protected]efd8 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction
彈簧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);
}
的對象不保存在物理數據庫呢?我檢查了數據庫用戶的權限,但它似乎擁有對所有操作的完全超級訪問權限!
可能是什麼原因?
如果您嘗試在沒有休眠的情況下運行類似的插入,它是否工作?你是否尊重所有非空,外鍵等約束? – 2014-09-28 07:34:29
正常的插入正在工作,所有的字段都被提交,除了主鍵以外沒有約束的普通表! – Hatem 2014-09-28 07:38:24
你可以測試你的休眠命令並在主鍵上添加一個值嗎?我沒有進入休眠狀態,我只是給出一些一般的想法。 – 2014-09-28 07:42:13