想知道如何使用SaveOrUpdate
,希望插入數據並在特定列信息相同時再次更新數據。休眠SaveOrUpdate:如何使用
我創建了Abrstract
類,抽象(AbstractDao)類在Dao中擴展並用於服務類。
當我用
beginTransaction
和commit
而SaveOrUpdate
我得到錯誤 「嵌套事務不支持」
Exception in thread "Thread-2" org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at com.my.myProj.mdao.AbstractDao.SaveOrUpdate(AbstractDao.java:26)
at com.my.myProj.mdao.UniqueDataDaoImpl.saveOrUpdateUniqueData(UniqueDataDaoImpl.java:25)
at com.my.myProj.service.UniqueDataServiceImpl.saveOrUpdateUniqueData(UniqueDataServiceImpl.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy33.saveOrUpdateUniqueData(Unknown Source)
at com.my.myProj.mainapp.savedb.StoreIdentityDataPacketDB.saveIdentityData(StoreDataDB.java:126)
at com.my.myProj.mainapp.MainApp.run(MainApp.java:359)
我複製那些來自淨AppConfig
和HibernateConfiguration
。我不確定事務是否在配置中啓用。我想知道以下
- 如何使用
SaveOrUpdate
:插入新時和更新時已在表中 - 提示瘦/瞭解java配置Hibernate的配置和應用程序級的配置
謝謝!
public abstract class AbstractDao {
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public void persist(Object entity) {
getSession().persist(entity);
}
public void delete(Object entity) {
getSession().delete(entity);
}
public void SaveOrUpdate(Object entity) {
Transaction tx3 = getSession().beginTransaction();
getSession().saveOrUpdate(entity);
tx3.commit();
}
}
的休眠配置文件
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.my.testProj.configuration" })
@PropertySource(value = { "classpath:hibernate.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.my.testProj.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
DAO實現
@Repository("UniqueDataDao")
public class UniqueDataDaoImpl extends AbstractDao implements UniqueDataDao {
@Override
public void saveUniqueData(UniqueData UniqData) {
persist(UniqData);
}
@Override
public void saveOrUpdateUniqueData(UniqueData UniqData) {
SaveOrUpdate(UniqData);
}
@SuppressWarnings("unchecked")
@Override
public List<UniqueData> SelectUniqueTable(String sNum,String sSerNum) {
// TODO Auto-generated method stub
Query query = getSession().createQuery("from UniqueData where S_NUMBER = :sModNum and S_SERIAL_NO = :sSerialNum");
query.setParameter("sModNum", sNum);
query.setParameter("sSerialNum", sSerNum);
return query.list();
}
}
服務實現
@Service("UniqueService")
@Transactional
public class UniqueDataServiceImpl implements UniqueDataService {
@Autowired
private UniqueDataDao UniqueDao;
@Override
public void saveUniqueData(UniqueData UniqData) {
UniqueDao.saveUniqueData(UniqData);
}
@Override
public List<UniqueData> SelectUniqueTable(String sNum, String sSerNum) {
return UniqueDao.SelectUniqueTable(sNum, sSerNum);
}
@Override
public List<UniqueData> SelectUniqueTable(String sImeiNum) {
return UniqueDao.SelectUniqueTable(sImeiNum);
}
@Override
public void saveOrUpdateUniqueData(UniqueData UniqData) {
UniqueDao.saveOrUpdateUniqueData(UniqData);
}
}
主要類的代碼片斷:它是一個命令行應用程序。
AbstractApplicationContext appContext = new
AnnotationConfigApplicationContext(AppConfig.class);
UniqueDataService serviceUniq =
(UniqueDataService) appContext.getBean("UniqueService");
UniqueDataTable UniqDataTable = oPacket.getUniqDataTable();
String sNumer = UniqDataTable.getsNumber();
UniqueData UniqData = new UniqueData();
UniqData.setsNumber(sNumer);
UniqData.setsSerialNum(sSerialNum);
// save or update : insert or update
serviceUniq.saveOrUpdateUniqueData(UniqData);
AppConfig.class
@Configuration
@ComponentScan(basePackages = "com.my.testProj")
public class AppConfig {
}
杜爾:我更新的問題。它現在有意義嗎? – SKay