-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to cascade a self-joining OneToMany relationship?
PostPosted: Mon May 16, 2016 4:18 pm 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
In addition, I tried orphanRemoval = true, but deletion attempt was also unsuccessful:
parent was deleted, child - no.
5.1.0.Final.


Code:
@Entity
public class Category implements NamedModel {
    @Id
    @Column(name = "CATEGORY_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
    @JoinTable(name = "CATEGORY_RELATIONS",
            joinColumns = {
                    @JoinColumn(name = "CATEGORY_RELATIONS_CATEGORY_ID", referencedColumnName = "CATEGORY_ID")},
            inverseJoinColumns = {
                    @JoinColumn(name = "CATEGORY_RELATIONS_PARENT_ID", referencedColumnName = "CATEGORY_ID")})
    private Category parent;

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "parent")
    private List<Category> children;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Category)) return false;
        if (!super.equals(o)) return false;

        Category category = (Category) o;

        return !(image != null ? !image.equals(category.image) : category.image != null)
                && !(parent != null ? !parent.equals(category.parent) : category.parent != null)
                && !(children != null ? !children.equals(category.children)
                : category.children != null);

    }
}
//-----------------------------------------
@Entity
@SuppressWarnings("unused")
@Table(name = "CATEGORY_RELATIONS")
@IdClass(CategoryRelations.CategoryRelationsPrimaryKey.class)
public class CategoryRelations implements Serializable {

    @Id
    @Column(name = "CATEGORY_RELATIONS_CATEGORY_ID")
    private long categoryId;

    @Id
    @Column(name = "CATEGORY_RELATIONS_PARENT_ID")
    private long parentId;

    @Entity
    @IdClass(CategoryRelationsPrimaryKey.class)
    public static class CategoryRelationsPrimaryKey implements Serializable {
        private Long categoryId;
        private Long parentId;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            CategoryRelationsPrimaryKey that = (CategoryRelationsPrimaryKey) o;

            return categoryId != null && categoryId.equals(that.categoryId)
                    && parentId != null && parentId.equals(that.parentId);
        }
        public int hashCode() {...}
    }
}


Top
 Profile  
 
 Post subject: Re: OneToManycascade remove - how?
PostPosted: Tue May 17, 2016 1:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to remove the cascade = CascadeType.REMOVE from the @OneToOne association because cascading makes sens for child associations only:

Code:
@OneToOne(fetch = FetchType.EAGER)
@JoinTable(name = "CATEGORY_RELATIONS",
    joinColumns = {
        @JoinColumn(name = "CATEGORY_RELATIONS_CATEGORY_ID", referencedColumnName = "CATEGORY_ID")},
    inverseJoinColumns = {
        @JoinColumn(name = "CATEGORY_RELATIONS_PARENT_ID", referencedColumnName = "CATEGORY_ID")})
private Category parent;


Top
 Profile  
 
 Post subject: Re: How to cascade a self-joining OneToMany relationship?
PostPosted: Tue May 17, 2016 3:49 am 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
Yes, sure!
But without it cascade will not work in my case too.
Why?


Top
 Profile  
 
 Post subject: Re: How to cascade a self-joining OneToMany relationship?
PostPosted: Tue May 17, 2016 5:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Probably you don't synchronize both sides of the bidirectional association.

Also, this statement of your:

Quote:
parent was deleted, child - no.


can never be true because a child cannot exist without a parent entry. The FK constraint should prevent such a scenario.


Top
 Profile  
 
 Post subject: Re: How to cascade a self-joining OneToMany relationship?
PostPosted: Tue May 17, 2016 5:40 am 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
I thought, I have bidirectional relations in my code:
@OneToOne,
@OneToMany, mappedBy.
Or what is wrong?


Top
 Profile  
 
 Post subject: Re: How to cascade a self-joining OneToMany relationship?
PostPosted: Tue May 17, 2016 6:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
First, you need to read this User Guide section and make sure you synchronize both ends of the association whenever you remove a child entity.

Second, you can also share your data access code because that's where the problem is.

Third, your affirmation about the parent being deleted while the child is not is false meaning that you are making wrong assumptions about what might not be working for you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.