将实体与 onetomany 映射和 @version 字段合并会导致删除先前的映射
你好!所有,
我有两个实体的映射问题。通过 @OneToMany
单向关系映射。我有一个实体Artifact
,它可以有多个Revision
。这是我映射它们的方式
@Entity
@Table(name = "artifact")
public class Artifact implements Serializable {
private static final long serialVersionUID = 248298400283358441L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Version
private Integer version;
...
@OneToMany(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE })
@JoinTable(name = "artifact_revisions", joinColumns = @JoinColumn(name = "artifact_id"), inverseJoinColumns = @JoinColumn(name = "revision_id"))
private Set<Revision> revisions;
和修订实体
@Entity
@Table(name = "revision")
public class Revision implements Serializable {
private static final long serialVersionUID = -1823230375873326645L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
@Column(name = "date_created", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
修订表保存更新的文件名;旧值和新值等。
我面临的问题是,当我更新工件时;最后一个映射被删除,然后插入一个新映射,因此,如果有效,我只有最后一个可用修订,而不是整个修订历史记录。
Hibernate:
update
artifact
set
description=?,
estimate=?,
name=?,
rank=?,
status=?,
sysId=?,
version=?
where
id=?
and version=?
Hibernate:
delete
from
artifact_revisions
where
artifact_id=?
and revision_id=?
Hibernate:
insert
into
artifact_revisions
(artifact_id, revision_id)
values
(?, ?)
如果我从工件中删除 @version
注释,它就可以正常工作。
- 是因为我以错误的方式映射关系吗?是否应该将该关系映射为元素集合?
- 还有另一个实体
Task
需要与Revision
实体进行映射。那么这里最好的方法是什么?
Hi! All,
I have a mapping issue with two entities. mapped through a @OneToMany
unidirectional relation. I have an entity Artifact
which can have multiple Revision
. Here's how I have mapped them
@Entity
@Table(name = "artifact")
public class Artifact implements Serializable {
private static final long serialVersionUID = 248298400283358441L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Version
private Integer version;
...
@OneToMany(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE })
@JoinTable(name = "artifact_revisions", joinColumns = @JoinColumn(name = "artifact_id"), inverseJoinColumns = @JoinColumn(name = "revision_id"))
private Set<Revision> revisions;
And the revisions entity
@Entity
@Table(name = "revision")
public class Revision implements Serializable {
private static final long serialVersionUID = -1823230375873326645L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
@Column(name = "date_created", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
The revision table saves the filed name that was updated; old value and new value etc.
The problem I face is that when I update the artifact; the last mapping gets deleted and then it inserts a new one, so if effect I only have the last but one revision available not the entire revision history.
Hibernate:
update
artifact
set
description=?,
estimate=?,
name=?,
rank=?,
status=?,
sysId=?,
version=?
where
id=?
and version=?
Hibernate:
delete
from
artifact_revisions
where
artifact_id=?
and revision_id=?
Hibernate:
insert
into
artifact_revisions
(artifact_id, revision_id)
values
(?, ?)
If I remove @version
annotation from the artifact it works fine.
- Is it because I am mapping the relation in a wrong manner? Should this relation be mapped as an element collection instead?
- There is another Entity
Task
which is to be mapped with theRevision
entity. So what will be the best approach here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这不是您问题的直接答案,但我认为您应该研究一下休眠状态。我认为它正在做非常相似的事情。 (envers 代表实体版本控制)。您只需使用 @Audited 注释实体,将一些侦听器放入配置中,剩下的魔法就为您完成了。
Maybe this is not a straight answer to your question but I think you should look into hibernate envers. I think it's doing pretty similar thing. (envers stands for entity versioning). You just annotate entity with @Audited put some listeners into config and the rest magic is done for you.