I've done something similar to this with hbm files but so far I haven't found an equivalent using annotations. What I'd like to be able to do is create an association across a join table where the join table contains an order colum for the association (sorry for any incorrect terminology here). At the very simplest, the tables might look like this:
Table: Menus
-----
menu_id
Table: Items
-----
item_id
Table: Menus_Items
----
menu_id
item_id
order_by
If I didn't require that menu items be ordered (this the order_by column) I could do this with no problem; however, I'd like to be able to programmatically order items in the Menus_Items table either in unit tests or in an admin tool. So for that, I think, I'll also need a MenuItems class, correct?
My question is, what annotations are needed on the corresponding classes to make this happen? The classes might look something like this (this is as far as I've got):
public class Menu {
private List<MenuItem> items;
// ...
@OneToMany
@JoinTable(name = "Menus_Items",
joinColumns = @JoinColumn(name = "menu_id"),
inverseJoinColumns = @JoinColumn(name = "item_id"))
@Cascade(value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
@IndexColumn(name = "order_by")
public List<MenuItem> getItems() { // ...
}
} // end class Menu
Is @IndexColumn correct? Programmatically, how do I specify the order of the MenuItems collection? What might I be doing wrong? I have a nagging feeling that I'm missing something obvious.
Any help much appreciated.