Hibernate 3.5 中的@OrderColumn 注释 [英] @OrderColumn annotation in Hibernate 3.5

查看:38
本文介绍了Hibernate 3.5 中的@OrderColumn 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Hibernate 3.5 中使用 @OrderColumn 注释

I'm trying to use the @OrderColumn annotation with Hibernate 3.5

@OneToMany(mappedBy = "parent",fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@OrderColumn(name = "pos")
private List<Children> childrenCollection;

检索数据时,一切正常.但是我不能让它重新排列List中的元素并将新的顺序保存到数据库中.

When retrieving data, everything works fine. But I can't make it reorder elements in the List and save the new order to the database.

推荐答案

Hibernate 不支持@OneToMany(mappedBy="...") 和@OrderColumn 的组合.当使用此无效组合时,此 JIRA 问题会跟踪抛出更明显错误消息的请求:http://opensource.atlassian.com/projects/hibernate/browse/HHH-5390

The combination of @OneToMany(mappedBy="...") and @OrderColumn is not supported by Hibernate. This JIRA issue tracks a request to throw a more obvious error message when this invalid combination is used: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5390

我认为这不受支持,主要是因为它是一种奇怪的关系模式.上面的注释表明关系的一个"端决定了如何将关系刷新到数据库,但订单/位置仅在多"端通过检查列表可用.多"方拥有关系更有意义,因为该方知道元素的成员资格和顺序.

I think that this isn't supported mainly because it is an odd relational pattern. The annotations above indicate that the "one" side of the relationship determines how the relationship will be flushed to the database, but the order/position is only available on the "many" side by examining the List. It makes more sense for the "many" side to own the relationship, since that side knows about both the membership and the ordering of the elements.

Hibernate Annotations 文档详细描述了这种情况:

The Hibernate Annotations docs describe this situation in some detail:

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype-in​​dexbidir

解决方法是删除mappedBy"属性,这将导致关联使用默认连接表策略而不是目标表上的列.您可以使用@JoinTable 批注指定连接表的名称.

The workaround is to remove the "mappedBy" attribute, which will cause the association to use the default join table strategy instead of a column on the target table. You can specify the name of the join table using the @JoinTable annotation.

此更改的最终效果是关系的多"方面现在决定了关系的持久性.您的 java 代码需要确保 List 正确更新,因为 Hibernate 现在将在刷新实体时忽略一"侧.

The net effect of this change is that the "many" side of the relationship now determines how the relationship is persisted. Your java code needs to ensure that the List is updated properly, because Hibernate will now disregard the "one" side when flushing the entities.

如果您仍然希望在 Java 中可以访问one"端,请使用

If you still want to have the "one" side accessible in Java, map it with

@ManyToOne
@JoinColumn(name="...", insertable=false, updatable=false, nullable=false)

这篇关于Hibernate 3.5 中的@OrderColumn 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆