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

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

问题描述

我试图在Hibernate 3.5中使用@OrderColumn注解

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

检索数据时,一切正常。但我不能让它重新排列列表中的元素,并将新订单保存到数据库。 OneToMany(mappedBy =...)和@OrderColumn不被Hibernate支持。这个JIRA问题跟踪一个请求,当使用这个无效组合时,抛出一个更明显的错误信息: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5390



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



Hibernate Annotations文档描述这种情况有一些细节:

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



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

这种变化的净效应是现在关系的多一方决定了关系如何持续下去。你的java代码需要确保List被正确更新,因为当冲洗实体时,Hibernate将忽略one一方。



如果你仍然想要在Java中可以访问的one方面,将它映射为

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


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.

解决方案

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.

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-indexbidir

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.

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.

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中的JPA 2.0 @OrderColumn注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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