“躲在 Hibernate 背后”在没有关联实体的情况下更新外键值
更新:我最终通过做相反的事情“解决”了问题!我现在将实体引用字段设置为只读(insertable=false updatable=false),并将外键字段设置为读写。这意味着我在保存新实体时需要特别小心,但在查询时,实体属性会为我解析。
我的域模型中有一个双向一对多关联,其中我使用 JPA 注释和 Hibernate 作为持久性提供程序。这几乎是您的沼泽标准父/子配置,其中一个区别是我希望将父级的外键公开为子级的单独属性以及对父级实例的引用,如下所示:
@Entity
public class Child {
@Id @GeneratedValue
Long id;
@Column(name="parent_id", insertable=false, updatable=false)
private Long parentId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_id")
private Parent parent;
private long timestamp;
}
@Entity
public class Parent {
@Id @GeneratedValue
Long id;
@OrderBy("timestamp")
@OneToMany(mappedBy="parent", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Child> children;
}
这在大多数情况下都可以正常工作时间,但有很多(遗留)情况,我想在parent_id列中放入无效值,而不必先创建一个虚假的父项。
不幸的是,由于 insertable=false, updatable=false
,Hibernate 不会保存分配给 parentId
字段的值,当同一列映射到多个属性时,它需要这样做。有没有什么好的方法可以“躲在 Hibernate 背后”并将值偷偷地放入该字段,而不必下降到 JDBC 或实现拦截器?
谢谢!
Updated: I wound up "solving" the problem by doing the opposite! I now have the entity reference field set as read-only (insertable=false updatable=false), and the foreign key field read-write. This means I need to take special care when saving new entities, but on querying, the entity properties get resolved for me.
I have a bidirectional one-to-many association in my domain model, where I'm using JPA annotations and Hibernate as the persistence provider. It's pretty much your bog-standard parent/child configuration, with one difference being that I want to expose the parent's foreign key as a separate property of the child alongside the reference to a parent instance, like so:
@Entity
public class Child {
@Id @GeneratedValue
Long id;
@Column(name="parent_id", insertable=false, updatable=false)
private Long parentId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_id")
private Parent parent;
private long timestamp;
}
@Entity
public class Parent {
@Id @GeneratedValue
Long id;
@OrderBy("timestamp")
@OneToMany(mappedBy="parent", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Child> children;
}
This works just fine most of the time, but there are many (legacy) cases when I'd like to put an invalid value in the parent_id column without having to create a bogus Parent first.
Unfortunately, Hibernate won't save values assigned to the parentId
field due to insertable=false, updatable=false
, which it requires when the same column is mapped to multiple properties. Is there any nice way to "go behind Hibernate's back" and sneak values into that field without having to drop down to JDBC or implement an interceptor?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假父母有什么问题吗?有一种巧妙的方法可以在一个地方完成此操作:
Whats wrong about a bogus Parent? There is a neat way to do it in one place:
您是否考虑过在子项上设置 ManyToOne inverse=true,而不是告诉属性值不可插入/可更新?如果 inverse=true 执行它以前的操作,它将使子实体“不是关系的真相来源”。它仍然会读取该列,但不会写入它。我认为。我已经有一段时间没有遇到这种情况了。
Have you looked into setting the ManyToOne on the child inverse=true, instead of telling the property value to be un-insertable/updatable? If the inverse=true does what it used to, it'll make the Child entity "not the source of truth" for the relationship.. It'll still read the column, but not write it.. I think. It's been a while since I've been in this situation.