想要在单个hibernate事务中从文件夹中删除数据库值和文档

时间:2015-12-01 11:44:45

标签: java hibernate postgresql file transactions

我想知道是否适用于数据库查询执行的文件删除事务?我是hibernate查询事务的新手。我想从数据库表中删除文档详细信息,同时必须从文档中删除特定文档。如果文件删除中没有发生删除,则不会发生数据库值删除反之亦然。我认为这可以使用transaction来完成。这是否适用?或者我的问题是否有任何解决方案?我尝试使用事务代码。但它显示事务未成功启动异常。

谢谢

在我的DAOHibernateImplimentation类

getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)throws HibernateException, java.sql.SQLException {

Transaction tx = null;
int deleteStatus=0;
try {
    tx = session.beginTransaction();
    final String deleteQuery = "delete from tablename where name='"+name+"'";                                       
    deleteStatus = (Integer)getHibernateTemplate().execute(new HibernateCallback(){
    public Object doInHibernate(final Session session) throws HibernateException, java.sql.SQLException
    {
    Query query = session.createSQLQuery(deleteQuery);                                              
    return query.executeUpdate();
    }
      });
    if(deleteStatus>0){
        file.delete();
        }   
     tx.commit();                                                                        
    } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
                }       
                return deleteStatus;    
    }

});

例外是:

17:09:30,447 ERROR [STDERR] org.springframework.transaction.TransactionSystemException: Could not commit      Hibernate transaction; nested exception is org.hibernate.TransactionException:    Transaction not successfully started
17:09:30,447 ERROR [STDERR]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)
17:09:30,447 ERROR [STDERR]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
17:09:30,447 ERROR [STDERR]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632) 

2 个答案:

答案 0 :(得分:2)

尝试使用TransactionTemplate

TransactionTemplate transactionTemplate=new TransactionTemplate();
transactionTemplate.execute(new TransactionCallback() {
    @Override
    public Object doInTransaction(TransactionStatus status) {
    int deleteStatus = 0;
        try {
   final String deleteQuery = "delete from tablename where name='" + name + "'";
   deleteStatus = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(final Session session) throws HibernateException, java.sql.SQLException {
   Query query = session.createSQLQuery(deleteQuery);
   return query.executeUpdate();
         }
     });
 if (deleteStatus > 0) {
       file.delete();
        }
 } catch (Exception e) {
    e.printStackTrace();
       }
     return deleteStatus;
  }
});

答案 1 :(得分:0)

似乎在事务中没有执行getHibernateTemplate().execute ...。就像这里描述的Hibernate transaction not successfully started一样,可能没有什么可以提交的。我建议您删除HibernateCallback并直接执行查询。

编辑。喜欢这个!

getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session)throws HibernateException, java.sql.SQLException {

  Transaction tx = null;
  int deleteStatus=0;
  try {
      tx = session.beginTransaction();
      final String deleteQuery = "delete from tablename where name='"+name+"'";                                       

      Query query = session.createSQLQuery(deleteQuery);                                              
      deleteStatus = query.executeUpdate();

      if(deleteStatus>0){
          boolean deleted = file.delete();
          if(!deleted)
             throw new IOException("Error deleting");
       }   
       tx.commit();                                                                        
  } catch (Exception e) {
      tx.rollback();
      e.printStackTrace();
  }       
  return deleteStatus;    


});

您需要检查file.delete()调用的返回值。 (参见JavaDoc)