休眠如何正确删除@OneToMany中的子项? [英] Hibernate How to correctly delete children in @OneToMany?

查看:128
本文介绍了休眠如何正确删除@OneToMany中的子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Parent对象到CascadeType.ALL儿童列表的非常简单的单向@OneToMany。如何正确地删除和删除其中一个子项?



只要在List上调用remove(child),然后调用session.saveOrUpdate(parent)当然不起作用,除非我指定删除孤儿,否则孩子不会被删除。



作为孤儿删除的替代方法,如果我删除session.delete(child)它在数据库中,然后从列表中删除(孩子),然后我必须session.refresh(父母),所以我的父母对象在内存中有正确的状态?



我如何正确地删除孩子,并删除数据库中删除孤儿?



我目前正在考虑在我的ParentDao:

  public void removeChild(Parent parent,Child child){
Session session = HibernateUtil.getSessionFactory()。openSession();
Transaction tx = null;
尝试{
session.beginTransaction();

session.delete(child);

session.getTransaction()。commit();

parent.getChildren()。remove(child);
session.refresh(parent);
} catch(RuntimeException e){
if(tx!= null){
tx.rollback();
}
throw e;
} finally {
session.close();



解决方案

你需要手动执行@Cascade(DELETE_ORPHAN)(从Hibernate),这里是执行几乎相同行为的代码。



实体:

 类库{

@Id
private Integer id;

@OneToMany
私人列表< Book>图书;

// getters and setters
}

class Book {

@Id
private Integer id;

@ManyToOne
私人Libraby图书馆;

// getters and setters
}

例如:

  session.beginTransaction(); 

//载入ID = 1的库
库库= session.get(Library.class,1);

//假设你的图书馆有两本书
Book book = library.getBooks()。get(0); //获取第一本书
library.getBooks()。remove(0); //从图书馆删除这本书
session.delete(book);

session.getTransaction()。commit();

并且您的图书将从数据库中删除,并且其父母的更新也将被更新。 / p>

I have a very simple unidirectional @OneToMany from a Parent object to a List of children with CascadeType.ALL. How would I correctly remove and delete one of the children?

Simply calling remove(child) on the List and then session.saveOrUpdate(parent) of course does not work and the child is not deleted in the database unless I specify orphan removal.

As an alternative to orphan removal, would it be correct if I session.delete(child) to delete it in the DB, then remove(child) from the List and do I then have to session.refresh(parent) so my parent object in memory has the right state?

How would I correctly remove the child and have it deleted in the database without orphan removal?

I am currently thinking about this in my ParentDao:

public void removeChild(Parent parent, Child child) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try {
        session.beginTransaction();

        session.delete(child);

        session.getTransaction().commit();

        parent.getChildren().remove(child);
        session.refresh(parent);
    } catch (RuntimeException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        session.close();
    }
}

解决方案

As you area going to do the @Cascade(DELETE_ORPHAN) (from Hibernate) manually, here is the code that does pretty much the same behavior.

Entities:

class Library {

  @Id
  private Integer id;

  @OneToMany
  private List<Book> books;

  // getters and setters    
}

class Book {

  @Id
  private Integer id; 

  @ManyToOne
  private Libraby library;

  // getters and setters
}

And a simple example:

session.beginTransaction();

// load a library with ID = 1
Library library = session.get(Library.class, 1);

// assuming that your library has two books
Book book = library.getBooks().get(0); //gets the first book
library.getBooks().remove(0); // Remove this book from the library
session.delete(book);

session.getTransaction().commit();

And your Book will be deleted from the database and the lisf othe parent will be updated as well.

这篇关于休眠如何正确删除@OneToMany中的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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