JPA OneToMany with inherited objects using DiscriminatorOptions and orphanremoval

I have a problem that I canโ€™t solve in a few days. I read a lot of documents, searched many forums, but did not find a solution. I inherited the class as shown 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 objects in OneToMany realities with many:

 @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 save this newsletter object, all entries are created beautifully. If I change the contents of the list, it seems that the orphanRemoval = true attribute has no effect, because old records remain in the table, and new ones are created. If I remove the @DiscriminatorOptions annotation from the base object, the old records are deleted, but the inheritance discriminator no longer works, so the JPA provider (hibernate) tries to load each child record in each collection, and this leads to the organization .hibernate.loader.Loader.instanceAlreadyLoaded exception .

Has anyone successfully implemented such a model or was aware of a workaround?

+4
source share
1 answer

You can try using the @Cascade annotation for hibernation using the cascade delete syntax instead of removing orphans from the JPA. This could probably work.

Let me know if this works for you.

0
source

All Articles