Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using hibernate with annotations, i want a one-many relationship to be sorted

Using hibernate with annotations, i want a one-many relationship to be sorted by the 'created' field on the 'many' table.

So far i've got this, which always ends up in a random order:

// The notes 
@OneToMany
@JoinColumn(name="task_id")
Set<TaskNote> notes;
public Set<TaskNote> getNotes() {return notes;}
public void setNotes(Set<TaskNote> notes) {this.notes = notes;} 
like image 262
Chris Avatar asked Nov 26 '25 21:11

Chris


1 Answers

since neither answer gave you the full solution :

@OneToMany
@JoinColumn(name="task_id")
@OrderBy("created")
List<TaskNote> notes;
public List<TaskNote> getNotes() {return notes;}
public void setNotes(List<TaskNote> notes) {this.notes = notes;}

Set is unordered, so use List instead, and you need the @OrderBy annotation too.

like image 126
chris Avatar answered Nov 28 '25 12:11

chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!