Hibernate:映射@OneToMany关系的最后一行
我在元素和它的名称之间有关系。所有历史名称以及当前名称都位于具有“created”字段的表“element_name”中。最后创建的行是元素的当前名称。
如何将元素的当前名称映射为元素的属性?
class Element implements Serializable {
@OneToMany(fetch = FetchType.LAZY)
private List<ElementName> historyOfElementNames;
// What annotations should be used here?
private ElementName currentElementName;
...
}
提前致谢!
I have a relation between element and its names. All historical names as well as the current one are located in table "element_name" that has field "created". The row last created is the current name of the element.
How could I map the current name of the element as the property of the element?
class Element implements Serializable {
@OneToMany(fetch = FetchType.LAZY)
private List<ElementName> historyOfElementNames;
// What annotations should be used here?
private ElementName currentElementName;
...
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案的替代方法是映射列表中的所有名称,对该列表进行排序,然后以
elementNames.get(elementNames.size() - 1)
的形式获取当前名称。要启用此功能,请添加
@IndexColumn
注释以及实际索引列和索引。这样您还可以获得名称更改的顺序。编辑:从 Hibernate 3.5 开始,
@IndexColumn
似乎已重命名为@OrderColumn
。An alternative to your solution would be to map all names in a list, sort that list and then get the current name as
elementNames.get(elementNames.size() - 1)
.To enable this, add the
@IndexColumn
annotation as well as the actual index column and indices. That way you also get the order of name changes.Edit: as of Hibernate 3.5
@IndexColumn
seems to have been renamed to@OrderColumn
.