我正在尝试在数据库中保存TestEntity
对象。正在执行代码并创建实体的Id,但实体未持久保存到数据库(未插入行):
@Repository
public class DBConnectionConfig {
@Autowired
SessionFactory sessionFactory;
DBConnectionConfig(){
//this.sessionFactory=getSessionFactory();
}
/*private SessionFactory getSessionFactory() {
return sessionFactory;
}
private void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}*/
public Session getSession(){
return sessionFactory.openSession();
}
}
package com.homecare.persistance.repository;
@Repository
@Transactional
public class HomeCareDaoImp extends DBConnectionConfig implements HomeCareDao{
Logger LOGGER=Logger.getLogger("Connection.class");
Session session=null;
Transaction trnx=null;
@Autowired
ApplicationContext ctx;
@Autowired
DBConnectionConfig dBConnectionConfig;
public void doaProcess(TestEntity testEntity){
LOGGER.info(dBConnectionConfig.toString());
session=getSession();
// trnx=session.beginTransaction();
session.save(testEntity);
// session.flush();
// trnx.commit();
LOGGER.info("connection established");
}
}
从3行(beginTransation,flush,commit)中删除注释可以解决问题,但为什么@Transactional
注释在这里不起作用?
Spring XML:
<!-- Our service layer components -->
<context:component-scan base-package="com.homecare" />
<!-- <context:component-scan base-package="com.homecare.persistance.repository" /> -->
<!-- enable @Transactional Annotation -->
<!-- data source with c3p0 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5433/My_DB" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</bean>
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.homecare.persistance.resource"></property> <!-- yet to defined place where to scan> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>
POM xml文件:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- spring framwork -->
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
答案 0 :(得分:0)
用于保存的Hibernate会话对象与spring config xml中定义的事务管理器无关。
使用autowired
定义sessionfactory对象@Autowired
Private SessionFactory sessionFactory;
将会话对象提取为
Session session = sessionFactory.getCurrentSession();
现在执行session.save(object);
根据代码中的spring配置,sessionFactory与transactionManager相关联。因此,当事务成功完成时,所有执行的更改会话(与事务管理器关联)将在db中提交。
答案 1 :(得分:0)
尝试重写getSession()方法:
@Repository
@Transactional
public class HomeCareDaoImp extends DBConnectionConfig implements HomeCareDao{
Logger LOGGER=Logger.getLogger("Connection.class");
Session session=null;
Transaction trnx=null;
@Autowired
ApplicationContext ctx;
public void doaProcess(TestEntity testEntity){
LOGGER.info(dBConnectionConfig.toString());
session=getSession();
// trnx=session.beginTransaction();
session.save(testEntity);
// session.flush();
// trnx.commit();
LOGGER.info("connection established");
}
@Override
public void getSession() {
super.getSession();
}
}