【问题标题】:Save order of set in hibernate在休眠中保存集合的顺序
【发布时间】:2020-06-05 22:37:59
【问题描述】:

我有两个集合,默认情况下由休眠设置为 PersistentSet。问题是在引擎盖下休眠将它们转换为 HashSet,但我希望将它们设置为 LinkedHashSet,因为我想保留添加元素的顺序。

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> responsibilities;

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> requirements;

我尝试了几件事,但没有成功

public void setResponsibilities(LinkedHashSet<String> responsibilities) {
    this.responsibilities = responsibilities;
}

public void setRequirements(LinkedHashSet<String> requirements) {
    this.requirements = requirements;
}

我尝试的另一件事是:

@ElementCollection(fetch = FetchType.LAZY)
private Set<String> requirements = new LinkedHashSet<>();

【问题讨论】:

  • 你用的是什么休眠版本?
  • hibernate 5 spring boot 2+

标签: hibernate nhibernate-mapping linkedhashset persistent-set


【解决方案1】:

请看这里https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#collections-sorted 似乎这可以使用 SortedSet 来完成,或者只是尝试使用以下解决方案:

@OrderBy("created DESC")
private Set<MyEntity> entities;

【讨论】:

  • 我已经尝试过您的解决方案,但它没有完成这项工作,还请注意我的集合是字符串,而不是自定义对象
猜你喜欢
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 2011-12-16
  • 2011-03-26
  • 1970-01-01
相关资源
最近更新 更多