GWT/RequestFactory/JPA 外键未自动合并

发布于 2025-01-04 15:09:56 字数 1812 浏览 0 评论 0原文

我有以下 2 个实体:

@Entity
@Table(name = "brand")
public class Brand {

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
}

基本上,

@Entity
@Table(name = "car")
public class Car {

    @Id
    @Column(name = "id")
    private Integer id;

    @Version
    @Column(name = "version")
    private Integer version;

    @ManyToOne(optional=false)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    private Brand brand;

    @Column(name = "otherproperty")
    private String otherProperty;
}

品牌将是一个永远不应该更改的预定义列表,在 GUI 中从下拉列表中选择。 但是,我希望能够像更改任何其他财产一样更改我的汽车的品牌。

这是我的 DAO 中的方法:

public void updateCar(Car update) throws Exception {

    EntityManager em = entityManager();
    try {
        em.getTransaction().begin();
        em.merge(update);
        em.getTransaction().commit();

    } finally {
        em.close();
    }
}

DAO 可以很好地更新 otherProperty,但如果我提供另一个品牌作为 Car update 参数的子项,它不会更新新品牌的外键。

我尝试使用 CascadeType 属性,但它想要更新品牌表,这是我不想要的。

任何帮助将不胜感激。谢谢!


编辑:好的,我找到了解决方案,但对我来说它看起来过于复杂。

如果我在 DAO 代码中添加 2 行以便显式查找要更新的品牌,它会起作用:

public void updateCar(Car update) throws Exception {

    EntityManager em = entityManager();
    try {
        em.getTransaction().begin();

        Brand lookedUp = findBrandById(update.getBrand().getId());
        update.setBrand(lookedUp);

        em.merge(update);
        em.getTransaction().commit();

    } finally {
        em.close();
    }
}

但是我正在使用 GWT/RequestFactory,并且已经为我的 实现了一个 Locator品牌 和我的汽车 的一个,所以我认为定位器 会为我处理这项业务。

我错过了什么吗?

I have the 2 following entities:

@Entity
@Table(name = "brand")
public class Brand {

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;
}

and

@Entity
@Table(name = "car")
public class Car {

    @Id
    @Column(name = "id")
    private Integer id;

    @Version
    @Column(name = "version")
    private Integer version;

    @ManyToOne(optional=false)
    @JoinColumn(name = "brand_id", referencedColumnName = "id")
    private Brand brand;

    @Column(name = "otherproperty")
    private String otherProperty;
}

Basically the brand will be a predefined list that should never change, chosen in the GUI from a drop-down list.
However, I'd like to be able to change the brand of my car as any other property.

Here is the method in my DAO:

public void updateCar(Car update) throws Exception {

    EntityManager em = entityManager();
    try {
        em.getTransaction().begin();
        em.merge(update);
        em.getTransaction().commit();

    } finally {
        em.close();
    }
}

The DAO works fine to update otherProperty, but if I provide another brand as child of the Car update parameter, it won't update the foreign key to the new brand.

I've tried using the CascadeType properties, but it wants to update the brand table, which I don't want.

Any help would be appreciated. Thanks!


EDIT: OK I've found a solution, but it looks over complicated to me.

If I add 2 lines in the DAO code in order to explicitly look for the brand to update, it works:

public void updateCar(Car update) throws Exception {

    EntityManager em = entityManager();
    try {
        em.getTransaction().begin();

        Brand lookedUp = findBrandById(update.getBrand().getId());
        update.setBrand(lookedUp);

        em.merge(update);
        em.getTransaction().commit();

    } finally {
        em.close();
    }
}

However I'm working with GWT/RequestFactory, and have implemented a Locator for my Brand and one for my Car, so I would assume the Locator would take care of that business for me.

Am I missing something?

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

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

发布评论

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

评论(1

我的影子我的梦 2025-01-11 15:09:56

您是如何找到新的品牌实体并将其与您的汽车关联起来的?确保您已查找并更改了引用,而不仅仅是修改了旧品牌对象的 ID 和名称。

How did you find the new brand entity in order to associate it to your car? Make sure that you have looked it up and changed the reference and not just modified the old brand object's id and name.

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