Hibernate不会在LinkedHashSet集合中保留DB顺序

时间:2017-10-27 12:29:50

标签: java hibernate jpa collections

从数据库中检索值时,hibernate不会在Set类型的集合中保留插入顺序,而是将其保留在List集合中。我试图手动指定LinkedHashSet,但没有帮助。

我有父母实体

@Entity
@Table(name = "radio")
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
public class Radio extends AbstractField {

   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "radio_id")
   private Set<Choice> choices = new LinkedHashSet<>();
}

和子实体:

@Entity
@Table(name = "choice")
public class ChoiceEntity  {

   @Id
   @GeneratedValue
   @Column(name = "id", nullable = false)
   private UUID id;

   @Column(name = "value")
   private String value;
}

当我从数据库中检索值时,hibernate会以随机顺序将它们存储在choices集合中。当我更改集合类型以使用List时,一切正常。

是否可以配置hibernate以保持数据集Set的集合中的数据库顺序,而无需使用任何添加order_row

1 个答案:

答案 0 :(得分:0)

使用@javax.persistence.OrderBy@org.hibernate.annotations.OrderBy。前者需要在HQL / JPQL中定义的片段,而后者需要SQL片段

相关问题