Continuing with this I decided to take an approach of making a different entity and make a OneToOne relationship (I hope this is OK). Bet let me explain what I have been doing:
I have a Juez (judge) entity with these relevant fields:
@Entity
@Table(name = "jueces")
public class Juez implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id")
private Integer id;
@Version
@Column(name = "opt_lock")
private Integer version;
@Basic
@Column(name = "nombres")
private String nombres;
// Getters, setters, other properties, etc.
}
And I want another entity called RolDeJueces (judge roll) to hold a java.util.Map<Integer, Juez> in which I want the keys to be sorted (1,2,3…) and mapped to a judge so I can have a roll to make some calculations. Here is what I have until now:
@Entity
@Table(name = "rol_jueces")
public class RolDeJueces {
@Id
private Integer id;
@Version
@Column(name = "opt_lock")
Integer version;
@OneToMany //Is this ok?
@MapKeyColumn(name = "orden_juez", updatable = true)
private Map<Integer, Juez> rol;
// Getters, setters, other properties, etc.
}
How can I sort the map key and hold a reference to all my judges but only one time (a la @OneToOne)?
Also, I must be capable of reordering my judges and make the changes persistent e.g.
rolDeJueces.getRol().clear(); // Delete current order
int order = 0;
// Add judges according to new order to the roll
for(Juez juez : getNewOrder()){
RolDeJueces.getRol().put(++order, juez);
}
// save my new roll and stuff...
Is this possible?
Ok, I figured that I do not need a map for insertion order nor the stuff I wanted.
Here is the impl:
@OrderColumn will generate an extra column in the table used for the relationship.