java @OrderColumn、@OneToMany 和用于集合的空索引列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6763329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@OrderColumn, @OneToMany & null index column for collection
提问by Mark
I am trying to create parent child tables where the order is preserved. The example 7.8 from Hibernate documentation shows how to do this:
我正在尝试创建保留订单的父子表。Hibernate 文档中的示例 7.8 显示了如何执行此操作:
@Entity
public class Customer {
@Id @GeneratedValue public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
@OneToMany(mappedBy="customer")
@OrderColumn(name="orders_index")
public List<Order> getOrders() { return orders; }
public void setOrders(List<Order> orders) { this.orders = orders; }
private List<Order> orders;
}
@Entity
public class Order {
@Id @GeneratedValue public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
private String number;
@ManyToOne
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
private Customer number;
}
from http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-indexed
来自http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-indexed
When I try this I get an error: null index column for collections
当我尝试这个时,我收到一个错误:集合的空索引列
There is Hibernate issue that describes the problem and gives an invalid example, but it specifically says that the example I gave above from the docs IS valid.
有一个 Hibernate 问题描述了这个问题并给出了一个无效的例子,但它特别说明我上面从文档中给出的例子是有效的。
@Entity
public class Parent {
@OneToMany(mappedBy="parent")
@OrderColumn(name="order")
private List<Child> children;
}
@Entity
public class Child {
@ManyToOne
private Parent parent;
}
from: https://hibernate.onjira.com/browse/HHH-5390
来自:https: //hibernate.onjira.com/browse/HHH-5390
Maybe I'm being dense, but I don't see the difference between these two examples. One is:
也许我很密集,但我看不出这两个例子之间的区别。一种是:
@OneToMany(mappedBy="customer")
@OrderColumn(name="orders_index")
The other is:
另一个是:
@OneToMany(mappedBy="parent")
@OrderColumn(name="order")
And of course, I haven't figured out how to the get OrderColumn to work. Does anyone have any insight into why one of these examples is valid and the other is not?
当然,我还没有弄清楚如何让 OrderColumn 工作。有没有人对为什么这些例子中的一个有效而另一个无效有任何见解?
回答by Alex Gitelman
The bug refers to Hibernate 3.5.3 while documentation refers to Hibernate 3.6. It is my understanding from comments that the issue HHH-5390has been resolved. Which version of Hibernate do you use?
Note that you must have a column with exact specified name in @OrderCoulumn
.
该错误指的是 Hibernate 3.5.3,而文档指的是 Hibernate 3.6。我从评论中了解到问题HHH-5390已解决。您使用哪个版本的 Hibernate?请注意,您必须在@OrderCoulumn
.
Also see this discussionabout that same issue and a workaround in case of 3.5.
另请参阅有关同一问题的讨论以及 3.5 的解决方法。
Update
更新
Apparently it remains unsupported and there is a documentation bug as described by HHH-5732. I thought from HHH-5390that the person who it was assigned (same who owns HHH-5390) has agreed to fix it. But it's not clear whether and when it is going to happen.
显然它仍然不受支持,并且存在HHH-5732描述的文档错误 。我从HHH-5390 中认为分配给它的人(拥有HHH-5390 的人)已同意修复它。但目前尚不清楚它是否以及何时会发生。
回答by elektronika
For me the point was to set the column declared in @OrderColumn to the NOT NULL and with default value 0
对我来说,重点是将@OrderColumn 中声明的列设置为 NOT NULL 并使用默认值 0
回答by Daniel De León
Do something like this:
做这样的事情:
@Entity
class Parent {
@OneToMany
@IndexColumn(name = "index_column")
List<Child> children;
}
@Entity
class Child {
@ManyToOne
Parent parent;
@Column(name = "index_column")
Integer index;
@PrePersist
@PreUpdate
private void prepareIndex() {
if (parent != null) {
index = parent.children.indexOf(this);
}
}
}
回答by Tommy Müller
Moin,
莫恩,
the same problem occurs on hibernate.core 5.1.4 final. Using Set
and HashSet
on tag @OrderColumn
(like Augustin said) fix the problem.
同样的问题发生在hibernate.core 5.1.4 final 上。使用Set
和HashSet
标记@OrderColumn
(如奥古斯丁所说)可以解决问题。
回答by Agustin Silva Albistur
Maybe these can help you:
也许这些可以帮助你:
I have the same problem with an old version of hibernate (3.5.6)with tag @IndexColumn
and find one good non-invasive workaround: try to change your List<Message>
to Set<Message>
Object and use HashSet
instead of ArrayList
.
It's seems that old Hibernate versions work better with Sets.
我有同样的问题与旧版本的休眠(3.5.6)与标签@IndexColumn
,找到一个良好的非侵入性的解决办法:试着改变你List<Message>
对Set<Message>
对象,并使用HashSet
替代ArrayList
。似乎旧的 Hibernate 版本与 Sets 一起工作得更好。
Good luck!
祝你好运!