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

查看:69
本文介绍了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;

检索数据时,一切正常.但是我无法使其重新排序列表中的元素并将新的订单保存到数据库中.

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访问一侧",请使用

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天全站免登陆