Question

I have two entities, Books and Wishlist. My wish list contains books. I want to get the collection of books that are in my wish list ordered in the same order they were added to the wish list.

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "WISHLISTS_BOOKS", joinColumns = { @JoinColumn(name = "BOOK_ID") }, inverseJoinColumns = { @JoinColumn(name = "WISHLIST_ID") })
private Set<Book> books;

This is where I get stuck. There's an @OrderBy annotation but from what I understand it refers to a field in the book to use to order by. In my case, since a book can belong to multiple wish lists, I can't do that. Instead, I tried to create a added_ts timestamp field in the WISHLISTS_BOOKS join table but I'm not sure how to refer to it.

Can anyone help me sort this out?

Was it helpful?

Solution

You can use @OrderColumn(name = "added_ts") to achieve ordering.

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "WISHLISTS_BOOKS", joinColumns = { @JoinColumn(name = "BOOK_ID") }, inverseJoinColumns = { @JoinColumn(name = "WISHLIST_ID") })
@OrderColumn(name = "added_ts")
private Set<Book> books;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top