使用 DiscriminatorOptions 和 orphanremoval 具有继承实体的 JPA OneToMany

发布于 2024-12-10 14:49:58 字数 1703 浏览 0 评论 0原文

我有一个问题,几天了都无法解决。我阅读了很多文档,搜索了很多论坛,但没有找到解决方案。 我继承了类,如下代码所示:

@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 {

我想在 OneToMany 现实的多方面使用这些特定实体:

@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>();

当我保留此时事通讯实体时,所有记录都会很好地创建。 如果我修改列表的内容,似乎 orphanRemoval = true 属性不起作用,因为旧记录保留在表中,并且创建了新记录。 如果我从基本实体中删除 @DiscriminatorOptions 注释,旧记录将被删除,但继承鉴别器不再工作,因此 JPA 提供程序(hibernate)尝试将每个子记录加载到每个集合中,这会导致一个组织.hibernate.loader.Loader.instanceAlreadyLoaded 异常。

有谁成功地实现了这样的模型,或者知道这个问题的任何解决方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-12-17 14:49:58

您可以尝试使用 Hibernate 的原生 @Cascade 注释和级联删除孤儿,而不是 JPA 的孤儿删除。
有可能它会起作用。

让我知道它是否对你有用。

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.

月朦胧 2024-12-17 14:49:58

今天我在这篇文章中遇到了类似的问题。

我的场景是

使用 @Inheritance 策略连接的父实体类。
该父类有一个 OneToMany Set 子级,其级联 All 和孤儿移除为 true。

子类使用公共 id 扩展父类以进行歧视连接,

其目的是干净地保存子类实体而不会出现问题。

好吧,断言失败了,错误是 hibernate orphanremoval null 等等。

在对 junit 进行各种测试之后,我意识到只有当父类上的子级列表必须用空列表初始化时,保存才有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文