Hibernate Session.delete()一个对象是否存在 [英] Hibernate Session.delete() an object if exists

查看:276
本文介绍了Hibernate Session.delete()一个对象是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Session类的JavaDoc中,描述 delete 方法是:


持久性实例中移除数据存储。参数可能是一个与接收Session相关的实例,或者是一个带有与现有持久状态关联的标识符的临时实例。

我的问题是:


  1. 我想删除一个detach对象,我可以使用这个方法吗,AFAIK会话首先让一个对象持久分离,然后执行它操作。我对吗?

  2. 如果我不确定数据库中是否存在对象,是否应该使用Session.get()来检查它是否为null,然后执行删除操作,或者我可以使用直接删除操作?

以下是一段代码片段:

  public void removeUnallocatedUserIfExits(final long itemId){
getHibernateTemplate()。execute(new HibernateCallback(){
$ b $ public Object doInHibernate(Session session)throws HibernateException,SQLException {$ b $ ();(b)session.flush();
session.setCacheMode(CacheMode.IGNORE);
UnallocatedUser unallocatedUser;
if((unallocatedUser =(UnallocatedUser)session.get(UnallocatedUser.class,itemId)) (
session.delete(unallocatedUser);
}
session.flush();
return null;
}
});
}

好吗?

解决方案


或带有
标识符的临时实例与现有
关联持久状态

这意味着您可以直接将实体传递给 session.delete(),以删除该对象。此外,您不需要检查实体是否存在。如果数据库中没有记录,应该有一个例外。事实上,我们通常并没有真正了解这种情况。我们总是删除一个现有的实体,我的意思是通常的逻辑就是这样;所以,不需要那样做。您可以简单地做到这一点,

  SomeEntity ent = session.load(SomeEntity.class,'1234'); 
session.delete(ent);

或者您可以这样做,

  SomeEntity ent = new SomeEntity('1234'); //为简洁而使用构造函数
session.delete(ent);

顺便说一句,你也可以使用这个版本 session.delete(String query)

  sess.delete(from Employee e where e.id ='1234') ; //发现它已被弃用


In JavaDoc of Session class the description of delete method is:

Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state.

My questions are:

  1. I want to delete a detach object, can I use this method, AFAIK session first makes an object persistent from detach and then perform its operation. Am I right?
  2. If I am not sure about the existence of the object in database, should I use Session.get() to check the whether or not it is null and then perform delete operation or I can use directly delete operation?

Here is a code snippet:

public void removeUnallocatedUserIfExits(final long itemId) {
    getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            session.flush();
            session.setCacheMode(CacheMode.IGNORE);
            UnallocatedUser unallocatedUser;
            if ((unallocatedUser = (UnallocatedUser) session.get(UnallocatedUser.class, itemId)) != null) {
                session.delete(unallocatedUser);
            }
            session.flush();
            return null;
        }
    });
}

Is the okay?

解决方案

or a transient instance with an identifier associated with existing persistent state

This means you can directly pass your entity to session.delete(), in order to delete that object. Further, you need not check whether the entity exist or not. There should be an exception if no record found in the database. In fact, we usually don't really get this case. We always delete an existing entity, I mean usual logic is like that; so, no need to do that. You can simply do this,

SomeEntity ent = session.load(SomeEntity.class, '1234');
session.delete(ent);

or you can do this instead,

SomeEntity ent = new SomeEntity('1234'); // used constructor for brevity
session.delete(ent);

Btw, you can also use this version session.delete(String query),

sess.delete("from Employee e where e.id = '1234'"); // Just found it is deprecated

这篇关于Hibernate Session.delete()一个对象是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆