GWT/RequestFactory/JPA 外键未自动合并
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是如何找到新的品牌实体并将其与您的汽车关联起来的?确保您已查找并更改了引用,而不仅仅是修改了旧品牌对象的 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.