将集合设置为“new ArrayList()”时级联休眠删除

发布于 2024-10-14 09:37:40 字数 1454 浏览 1 评论 0原文

我有一个使用 hibernate 映射的对象,其中包含 id 和集合。在某个时间点,当对象包含其集合中的项目时,我想要执行 obj.setCollection(new ArrayList()) 并让它从数据库中删除以前在集合中的项目。如果有人可以提供帮助,我们将不胜感激。说明问题的代码:

ORM: Foo 1-* Bar 和 Bar 1-1 Foo

class Foo {
    Long id;
    List<Bar> bars;

    //getters & setters
}

<hibernate-mapping>
    <class name="domain.Foo" table="Foos">
        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>
        <list name="bars" lazy="true" cascade="all">
            <key column="FOO_ID" not-null="true" />
            <index column="SEQUENCE" />
            <one-to-many class="domain.Bar" />
        </list>     
    </class>    
</hibernate-mapping>

@Entity
@Table(name="Bars")
public class Bar {
    private Long id;
    private Foo foo;
    private String name;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name="FOO_ID", insertable=false, updatable=false)
    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

    @Column(name="NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I have an object mapped with hibernate that contains an id and a collection. At some point in time when the object contains items in it's collection I want to do obj.setCollection(new ArrayList()) and have it delete the items from the database that were previously in the collection. If anyone could assist it would be much appreciated. Code to illustrate problem:

ORM: Foo 1-* Bar and Bar 1-1 Foo

class Foo {
    Long id;
    List<Bar> bars;

    //getters & setters
}

<hibernate-mapping>
    <class name="domain.Foo" table="Foos">
        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>
        <list name="bars" lazy="true" cascade="all">
            <key column="FOO_ID" not-null="true" />
            <index column="SEQUENCE" />
            <one-to-many class="domain.Bar" />
        </list>     
    </class>    
</hibernate-mapping>

@Entity
@Table(name="Bars")
public class Bar {
    private Long id;
    private Foo foo;
    private String name;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name="FOO_ID", insertable=false, updatable=false)
    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

    @Column(name="NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-10-21 09:37:40

您可能想要做的是:

obj.getCollection().clear();

What you probably want to do is :

obj.getCollection().clear();
Saygoodbye 2024-10-21 09:37:40

您永远不应该用新集合替换由 Hibernate 管理的集合(例如,如果您有一个包含集合的对象,并且该对象由 Hibernate 加载)!

始终使用集合方法(addremove、 clear ...)来操作该方法。

You should never replace a collection manged by Hibernate (for example if you have an object containing a collection, and this object is loaded by Hibernate) by an new collection!

Always use the collection methods (add, remove, clear, ...) to manipulate the method.

安稳善良 2024-10-21 09:37:40

如果您希望 Hibernate 在从集合中删除对象时从数据库中删除对象,则需要为该集合配置“delete-orphan”选项:

<list name="bars" lazy="true" cascade="all-delete-orphan">

尽管在这种情况下您无法替换该集合,但您需要改为使用集合方法(请参阅拉尔夫的答案和 删除集合删除孤儿不适用于空分配?:()

If you want Hibernate to remove objects from the database when you remove them from the collection, you need to configure delete-orphan option for that collection:

<list name="bars" lazy="true" cascade="all-delete-orphan">

Though in this case you cannot replace the collection, you need to use collection methods instead (see Ralph's answer and remove collection with delete-orphan not work with null assignment? :()

琉璃梦幻 2024-10-21 09:37:40

或者类似的东西(我喜欢封装字段):

class Foo {
    Long id;
    List<Bar> bars = new ArrayList<Bar>();

    ...

    public List<Bar> getBars() {
        return new ArrayList<Bar>(bars);
    }

    public void setBars(List<Bar> newBars) {
        bars.clear();
        bars.addAll(newBars);
    }

    public void addBar(Bar bar) {
        bars.add(bar);
    }

    public void removeBar(Bar bar) {
        bars.remove(bar);
    }
}

Or something like this (I like to encapsulate fields):

class Foo {
    Long id;
    List<Bar> bars = new ArrayList<Bar>();

    ...

    public List<Bar> getBars() {
        return new ArrayList<Bar>(bars);
    }

    public void setBars(List<Bar> newBars) {
        bars.clear();
        bars.addAll(newBars);
    }

    public void addBar(Bar bar) {
        bars.add(bar);
    }

    public void removeBar(Bar bar) {
        bars.remove(bar);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文