开发者

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 anyon开发者_运维问答e successfully implemented a model like this, or know any workaround for this problem?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜