Ni cordes ni liste est une entité, vous devez créer une classe d'emballage qui encapsule votre liste
Ne pas oublier de poseur
@Entity
public class Foo {
private MutableInt id = new MutableInt();
private Map<String, CustomList> customListMap = new HashSet<String, CustomList>();
@Id
@GeneratedValue
public Integer getId() {
return this.id.intValue();
}
public void setId(Integer id) {
return this.id.setValue(id);
}
@OneToMany
@MapKey(name="key")
public Map<String, CustomList> getCustomListMap() {
return customListMap;
}
// add convenience method
public void addBar(String key, String bar) {
if(customListMap.get(key) == null)
customListMap.put(key, new CustomList(new CustomListId(id, key)));
customListMap.get(key).getBar().add(bar);
}
}
Et votre CustomList personnalisé (Ne pas oublier poseur de)
@Entity
public class CustomList {
private CustomListId customListId;
private List<String> bar;
private String key;
@EmbeddedId
public CustomListId getCustomListId() {
return customListId;
}
@Column(insertable=false, updatable=false)
public String getKey() {
return this.key;
}
@CollectionOfElements
@JoinTable(name="BAR")
public List<String> getBar() {
return this.bar;
}
@Embeddable
public static class CustomListId implements Serializable {
private MutableInt fooId = new MutableInt();
private String key;
// required no-arg construtor
public CustomList() {}
public CustomList(MutableInt fooId, String key) {
this.fooId = fooId;
this.key = key;
}
public Integer getFooId() {
return fooId.intValue();
}
public void setFooId(Integer fooId) {
this.fooId.setValue(fooId);
}
// getter's and setter's
public boolean equals(Object o) {
if(!(o instanceof CustomListId))
return false;
CustomListId other = (CustomList) o;
return new EqualsBuilder()
.append(getFooId(), other.getFooId())
.append(getKey(), other.getKey())
.isEquals();
}
// implements hashCode
}
}
Vous pouvez même créer une méthode personnalisée appelée getBar qui en encapsule votre customListMap de manière transparente comme suit
@Entity
public class Foo {
...
public Map<String, List<String>> getBar() {
Map<String, List<String>> bar = new HashMap<String, List<String>>();
for(Entry<String, CustomList> e: customListMap())
bar.put(e.getKey(), e.getValue().getBar());
return bar;
}
}
Et quelle serait la représentation de la base de données? –