hibernate是否保留LinkedHashSet的顺序,如果是的话,怎么办? [英] Does hibernate preserve the order of a LinkedHashSet and if so, how?
问题描述
背景:
我知道什么是LinkedHashSet,我之所以问这个问题是因为我将一些执行的函数的名称记录到一个'logError'表中,该表有多个与某些'functionName'表的关系。
我需要这些函数保持与执行它们时的顺序相同,所以首先找到相应的'functionName'对象,将它们放入LinkedHashSet(每个失败的函数之后),然后坚持'logError 'object。
现在,当我再次从数据库中获得'logError'对象时,它仍然会被排序吗?如果是这样的话,我很好奇Hibernate是如何完成这项工作的。
解决方案第一:我假设你正在谈论两个实体。就像
@Entity
public class A {
@ManyToMany
@JoinTable(name =A_B,joinColumns = {@JoinColumn(name =A_fk)},inverseJoinColumns = {@JoinColumn(name =B_fk)})
private Set< B> bSet = new LinkedHashSet< B>();
Hibernate不会自行保存订单!
如果您查看从数据库加载实体 即使Hibernate使用 但是您可以使用 2012年9月1日由ŁukaszRzeszotarski添加: 但我们必须记住,使用 @see:在sql中定制hibernate Does hibernate preserve the order of a LinkedHashSet and if so, how? In case this depends on the type of database, I'd like to know this for PostgreSQL. Background: I know what a LinkedHashSet is for, and the reason I'm asking this is because I'm logging the names of some functions I execute to a 'logError' table that has a many-to-many relation to some 'functionName' table.
I need these functions to remain in the same order as when I executed them, so first I find the corresponding 'functionName' objects, put them in a LinkedHashSet (after each function that failed) and then I persist the 'logError' object. Now when I get the 'logError' object from the database again, will it still be ordered? And if so, I was curious how this is done by Hibernate. First: I assume you are talking about a relationship between two entities. Something like Hibernate does not preserve the order by itself! If you have a look at the classes used when entity Even if Hibernate used But you can use the @see: Ordering return of Child objects in JPA query Added by Łukasz Rzeszotarski on 1th Sep 2012: But we have to remember that using @see: Hibernate ordering in sql 这篇关于hibernate是否保留LinkedHashSet的顺序,如果是的话,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! A
时使用的类,那么 Set
bSet
类型为 PersistentSet
,它是另一个包装设置
,这是(在我的情况下)正常 HashSet
。 ( HashSet
不会保留它的元素顺序。)
List
或 LinkedHashSet
,但仍然不推荐将实现建立在自然数据库(不保证)数据库的顺序上。对于MySQL,它是某种反模式。
@Sort
注释( org.hibernate.annotations.Sort
)来使你的排序清晰。例如:
@OneToMany(mappedBy =as)
@Sort(type = SortType.COMPARATOR,comparator = MyBComparator.class);
public SortedSet< C> CS;
$ b $ - 返回子对象在jpa-query b
@Sort
批注会导致内存中的对象(jvm)而不是sql中的对象。相反,我们可以使用 @OrderBy
注释来导致sql服务器端的排序。这两个注释在我的(ŁukaszRzeszotarski)意见中有一个缺点,即默认设置了排序。 I(ŁukaszRzeszotarski)宁愿hibernate使用自己的LinkedHashSet实现,当它'看到'使用了order by子句时。
@Entity
public class A {
@ManyToMany
@JoinTable(name = "A_B", joinColumns = { @JoinColumn(name = "A_fk") }, inverseJoinColumns = { @JoinColumn(name = "B_fk") })
private Set<B> bSet = new LinkedHashSet<B>();
}
A
is loaded from the database, then the Set
bSet
is of type PersistentSet
, which is a wrapper around another Set
, and this is (in my case) a normal HashSet
. (HashSet
does not preserve the order of its elements.)List
or LinkedHashSet
, it is still inadvisable to base the implementation on the natural (not guaranteed) database order. For MySQL it is some kind of anti-pattern.@Sort
annotation (org.hibernate.annotations.Sort
) to make your sorting explicit. For example:@OneToMany(mappedBy = "as")
@Sort(type = SortType.COMPARATOR, comparator = MyBComparator.class);
public SortedSet<C> cs;
@Sort
annotation causes sorting objects in memory (jvm) and not in sql. Instead we can use @OrderBy
annotation that causes sorting on the sql server side. Both of these annotations have in my (Łukasz Rzeszotarski ) opinion one weakness that set up ordering by default. I (Łukasz Rzeszotarski ) would rather hibernate uses own LinkedHashSet implementation when it 'sees' that order by clause is used.