简体   繁体   中英

how to use @OrderBy with Embeddable in spring boot jpa

I have 3 entity Unit, Off, Position. in my Unit have an constrain one to many with list offs, off have position. I want to Order by code in position.code , how can I make it

@Entity
@Table(name = "unit")
public class Unit{

 @Id
 @Column(name = "id")
 private Long id;

 @OneToMany(fetch = FetchType.LAZY)
 @JoinClolumn(name = "unit")
@ElementCollection
 @OrderBy("position.code")
 private List<Off> offs;
}

this is my Entity Off

@Entity
@Table(name = "off")
@Embeddable
public class Off{

 @Id
 @Column(name = "id")
 private Long id;

 @Column(name = "cate_position")
 private Long catePosition;

 @ManyToOne
 @NotFound(action = NotFoundAction.IGNORE)
 @JoinClolumn(name = "cate_position")
 @Embedded
 private Position position;
}

this is my Entity Position

@Entity
@Table(name = "position")
@Embeddable
public class Position{

 @Id
 @Column(name = "id")
 private Long id;

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

How can I sort List offs in Unit entity by 'position.code', it always throws invalid column name 'position'. Many thanks!

Annotate the entity class Off with @Embeddable and now include the following code above the List

@ElementCollection
@OrderBy("position.code DESC")
private List<Off> offs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM