JPA manytomany keep list order while using @JoinTable

I am using a class to hold a list of persisted objects, and the order of those objects really matter to me.

The class looks like this:

public class Class implements Serializable {

private static final long serialVersionUID = -8772078256979276783L;

@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.DETACH,CascadeType.REFRESH})
@JoinTable(name = "join_table",
        joinColumns = {
        @JoinColumn(name="joinColumn") 
},
inverseJoinColumns = {
        @JoinColumn(name="inverseJoinColumn")
}
)
private List<AbstractVariant> variablesList;
}

Is there a way to add to that JoinTable an index to specify the position on the list, therefore,keeping the order safe?

Upvotes: 7

Views: 3780

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

That's the goal of the @OrderColumn annotation.

Upvotes: 8

Related Questions