简体   繁体   English

休眠一对多从父关系中分离子对象

[英]hibernate one-to-many detach child object from parent relationship

I have 2 entities film (Parent) and episode (Child) with one-to-many relationship, whenever I create a film object with collection of episode objects, everything works normal, parent and related child objects are created. 我有两个具有一对多关系的实体film (父母)和episode (孩子),每当我创建具有episode对象集合的film对象时,一切正常,父对象和相关的子对象都会创建。 But when I try to update a `film' by removing bunch of 'episode' objects nothing seems to work out 但是,当我尝试通过删除一堆“片段”对象来更新“电影”时,似乎没有任何效果

Here what I am trying to do, When i remove a bunch of episode objects from film , those removed objects should not be deleted but kept as episode without linked to any parent object . 这是我要尝试做的,当我从film删除一堆episode对象时,那些删除的对象不应被删除, 保留为情节而未链接到任何父对象 Is this possible? 这可能吗?

Film entity 电影实体

    import org.hibernate.annotations.*;

    import javax.persistence.*;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.Table;
    import java.util.Set;

    @Entity
    @Table(name = "FILM")
    @Getter
    @Setter
    public class Film {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;

        @Column(name = "ORIGINAL_TITLE", nullable = false)
        private String originalTitle;


        @OneToMany(targetEntity=FilmEpisode.class, mappedBy="film", cascade=CascadeType.ALL,fetch=FetchType.EAGER, orphanRemoval = true)
        private Set<FilmEpisode> filmEpisodes;

        @Column(name = "SHOW_ON_LANDING_PAGE")
        private Boolean showOnLandingPage;
    }

FilmEpisode entity FilmEpisode实体

    import javax.persistence.*;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.Table;
    import java.util.Set;

    @Entity
    @Table(name = "FILM_EPISODE")
    @Getter
    @Setter
    public class FilmEpisode {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;

        @Column(name = "ORIGINAL_TITLE", nullable = false)
        private String originalTitle;

        @ManyToOne
        @JoinColumn(name = "FILM_ID")
        private Film film;
    }

In DAO I am just saving film with saveOrUpdate method like below 在DAO中,我只是使用如下的saveOrUpdate方法保存film

session.saveOrUpdate(film);

Am I missing something..?? 我在想什么吗?

orphanRemoval = true is making unreferenced entities from parent entity being deleted. orphanRemoval = true从父实体中删除未引用的实体。 Change it to orphanRemoval = false and child entities will not be removed from database. 将其更改为orphanRemoval = false ,子实体将不会从数据库中删除。 You can delete it too as it defaults to false : 您也可以删除它,因为它默认为false

In .NET I had the same problem to exclude a child. 在.NET中,我有同样的问题要排除孩子。 I decided carrying a session from parent and then setting a merge in session . 我决定从父级进行一次会话,然后在session中设置一个合并。

Using: 使用方法:

Map Parent

HasMany(x => x.Child) .KeyColumn("id") .AllDeleteOrphan();

On Update method: 关于更新方法:

public void Update(Parent model)
{
   var session = _sessionFactory.GetCurrentSession();

   using (var transaction = session.BeginTransaction())
   {
       session.Load<Parent>(model.Id);
       session.Merge(model);
       transaction.Commit();
   }
}

To load the list in .load in the database, returned three rows, and the last object in the method has only 2 rows. 要在数据库的.load中加载列表,请返回三行,而该方法中的最后一个对象只有2行。 Making the .Commit, the line I removed the object was deleted from the database. 进行.Commit时,我删除对象的行已从数据库中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM