tocsa
tocsa

Reputation: 63

JPA OneToMany with inherited entities using DiscriminatorOptions and orphanremoval

I've a problem, that I can't solve for days now. I have read many docs, searched many forums, but found no solution. I've inherited class as the code shows below:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "nc_linktype", discriminatorType = DiscriminatorType.STRING)
@Table(name = "mk_newsletter_coupons")
@DiscriminatorOptions(force = true)
public class BaseNewsletterCoupon extends BaseEntity {...}

@Entity
@DiscriminatorValue("expire")
public class NewsletterCouponsExpire extends BaseNewsletterCoupon {

@Entity
@DiscriminatorValue("region")
public class NewsletterCouponsRegion extends BaseNewsletterCoupon {

I would like to use these specific entities in a OneToMany realions' many side:

@Entity(name = "newsletter")
@Table(name = "mk_newsletters")
@Configurable
public class NewsLetter extends BasePictureEntity {

    @OneToMany(mappedBy = "newsletter", cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
    @IndexColumn(name = "nc_index")
    @OrderColumn(name = "nc_index")
    @OrderBy("index")
    List<NewsletterCouponsExpire> expireCoupons = new ArrayList<NewsletterCouponsExpire>();

    @OneToMany(mappedBy = "newsletter", cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
    @IndexColumn(name = "nc_index")
    @OrderColumn(name = "nc_index")
    @OrderBy("index")
    List<NewsletterCouponsRegion> regionCoupons = new ArrayList<NewsletterCouponsRegion>();

When I persist this newsletter entity all records are created nicely. If I modify the content of the list, it seems the orphanRemoval = true attribute has no effect, because the old records remains in the table, and new ones gets created. If I remove the @DiscriminatorOptions annotation from the base entity, old records gets deleted, but the inheritance discriminator doesn't work anymore, so JPA provider (hibernate) tries to load every child record to into every collection, and that leads to an org.hibernate.loader.Loader.instanceAlreadyLoaded exception.

Does anyone successfully implemented a model like this, or know any workaround for this problem?

Upvotes: 5

Views: 2582

Answers (2)

Alessandro Modica
Alessandro Modica

Reputation: 91

Today I have a issue similar at this post.

My scenario was

A parent entity class with @Inheritance strategy Joined. This parent class have got a OneToMany Set children with cascade All and orphanremoval at true.

The child class extends parent class with a common id to discrimination join

The objective is to save a child class entity cleanly without problem.

Well, the assert is a fail, the error was an hibernate orphanremoval null etc etc.

After various tests on junit I realize that the save work it only if the list of children on parent class must been initialized with an empty list.

Upvotes: 0

Doron Manor
Doron Manor

Reputation: 596

You can try using Hibernate's native @Cascade annotation with cascade delete orphan, instead of JPA's orphan removal. There is a chance it might work.

Let me know if it worked for you.

Upvotes: 0

Related Questions