For JPA-Entities in a project I work on, properties of type List or Map are always initialized to the synchronized implementations Vector and Hashtable.
(Unsynchronized ArrayList and HashMap are the standard implementations in Java, except if synchronization is really needed.)
Does anyone know a reason why synchronized Collections would be needed? We use EclipseLink.
When I asked about it, nobody knew why it was done like that. It seems it was always done like this. Maybe this was needed for an old version of EclipseLink?
I'm asking for two reasons:
- I would prefer to use the standard implementations ArrayList and HashMap like anywhere else. If that's safe.
- There's no matching synchronized Set implementation in the JDK. At least not a serializable one as EclipseLink expects.
Example Entity:
@Entity
public class Person {
...
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable( ... )
private List<Role> accessRoles;
@ElementCollection
@CollectionTable( ... )
@MapKeyColumn(name="KEY")
@Column(name="VALUE")
private Map<String, String> attrs;
public Person() {
// Why Vector/Hashtable instead of ArrayList/HashMap?
accessRoles = new Vector<Role>();
attrs = new Hashtable<String, String>();
}
public List<Role> getAccessRoles() {
return accessRoles;
}
public void setAccessRoles(List<Role> accessRoles) {
this.accessRoles = accessRoles;
}
public Map<String, String> getAttrs() {
return attrs;
}
public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs;
}
}