Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Can Hibernate delete orphan record instead of nulling the FK
PostPosted: Thu Sep 07, 2017 5:59 pm 
Newbie

Joined: Fri Sep 01, 2017 2:40 am
Posts: 17
I have 3 tables. `post`, `tag` and a join table `post_tag`. `post` to `post_tag` is one to many and `post_tag` to `tag` is one to one.

three entities are as follow:
Code:
@Entity
@Table(name = "post")
public class Post extends BaseEntity {
    @Setter
    @Getter
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "post_id")
    private Set<PostTag> tags;
}

@Entity
@Table(name = "post_tag")
public class PostTag extends BaseEntity {
    @Setter
    @Getter
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    private Tag tag;
}

@Entity
@Table(name = "tag")
public class Tag extends BaseEntity {
    @Getter
    @Setter
    @Column(name = "tag_text")
    private String text;
}

and My BaseEntity has just one variable
Code:
public class BaseEntity {
    @Id
    @Setter
    @Getter
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
}


The above works great on update and insert, but it nulls the post_id in post_tag table when I wanted hibernate to delete the row instead.

What happened was for example client HAD 2 records in post_tag table. When doing a PUT request to update post, it also passes tags object with just 1 record. Hibernate is smart enough to keep the one in object and null the other, but what I really want hibernate to do is to delete the orphan record. I've tried to set orphanRemoval = true) like this to tags
Code:
@Setter
@Getter
@OneToMany(orphanRemoval = true)
@JoinColumn(name = "user_id")
private Set<UserSubscribeTag> tags;

but got
Code:
A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance

I don't know if this is possible since I think hibernate is doing the right thing. I should've called delete instead of save if I really want to get rid of that record.


Top
 Profile  
 
 Post subject: Re: Can hibernate delete orphan record instead of nulling fk
PostPosted: Fri Sep 08, 2017 12:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
When you get:

Quote:
A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance


It means that you have assigned a new collection Object reference instead of adding/removing elements from the Hibernate managed collection.

For more details of how this association should look like, check out this article.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Can hibernate delete orphan record instead of nulling fk
PostPosted: Fri Sep 08, 2017 3:17 pm 
Newbie

Joined: Fri Sep 01, 2017 2:40 am
Posts: 17
vlad wrote:
When you get:

Quote:
A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance


It means that you have assigned a new collection Object reference instead of adding/removing elements from the Hibernate managed collection.

For more details of how this association should look like, check out this article.


I think I've carefully studied and followed the tutorial at least 5 times. I don't think I've missed anything, but I'm getting
Code:
2017-09-08T19:07:49.879110+00:00 app[web.1]: Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
2017-09-08T19:07:49.879114+00:00 app[web.1]:    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452) ~[hibernate-core-5.2.10.Final.jar!/:5.2.10.Final]
2017-09-08T19:07:49.879115+00:00 app[web.1]:    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889) ~[hibernate-core-5.2.10.Final.jar!/:5.2.10.Final]

after googling, suggestion was to set use_second_level_cache to false which I think totally defeat the purpose.

I also found suggestion that I think is more appropriate by setting
Code:
spring.jpa.properties.hibernate.cache.region.factory_class="net.sf.ehcache.hibernate.EhCacheRegionFactory"

but got another exception says
Code:
org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name ["net.sf.ehcache.hibernate.EhCacheRegionFactory"] as strategy [org.hibernate.cache.spi.RegionFactory]

I also tried org.hibernate.cache.ehcache.EhCacheRegionFactory and getting the same exception except class name
I do have hibernate-ehcache in my pom
I'm sorry, but I really can't find other solution. Thank you


Top
 Profile  
 
 Post subject: Re: Can hibernate delete orphan record instead of nulling fk
PostPosted: Fri Sep 08, 2017 5:46 pm 
Newbie

Joined: Fri Sep 01, 2017 2:40 am
Posts: 17
Hi vlad,
after reading your another tutorial here, I think I understand now. Thank you so much!


Top
 Profile  
 
 Post subject: Re: Can hibernate delete orphan record instead of nulling fk
PostPosted: Sat Sep 09, 2017 3:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
You're welcome. There are more (140 and counting) you might want to read.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.