I am mapping a Map of embeddable objects, where the (compound) map key constitutes a property of the corresponding value
Here is the mapping:
Map ownerCode:
@Entity
@Access(value = AccessType.FIELD)
@Table(name = "map_owner")
public class MapOwner implements Identifiable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "map_owner_values", joinColumns = @JoinColumn(name = "owner", nullable = false, insertable = true, updatable = false))
@Fetch(FetchMode.JOIN)
@ForeignKey(name = "fk_map_owner")
private Map<Key, ValueObject> map = new HashMap<Key, ValueObject>();
public Long getId() {
return this.id;
}
public Map<Key, ValueObject> getMap() {
return this.map;
}
}
KeyCode:
@Embeddable
@Access(AccessType.FIELD)
public class Key implements Comparable<Key> {
@Column(name = "key_id")
private Integer id;
@Column(name = "key_name")
private String name;
public Key() {
// for hibernate
}
public Key(Integer traderId, String articleNo) {
this.id = traderId;
this.name = articleNo;
}
@Column(name = "key_id", nullable = false, updatable = false)
public Integer getId() {
return id;
}
@Column(name = "key_name", nullable = false, updatable = false)
public String getName() {
return name;
}
// compareTo, equals and hashCode omitted
}
ValueCode:
@Embeddable
@Access(AccessType.FIELD)
public class ValueObject {
@Transient
private Key key;
@Column(name = "value")
private Double value;
public ValueObject() {
// Hibernate
}
public ValueObject(Integer keyId, String keyName, Double value) {
this(new Key(keyId, keyName), value);
}
public ValueObject(Key key, Double value) {
this.key = key;
this.value = value;
}
public Key getKey() {
return this.key;
}
public Double getValue() {
return this.value;
}
}
This mapping works.
Is there a possibility (a workaround, maybe) to retain a (non-transient) key property on the value object? With the given mapping, having a non-transient key property results in an exception (repeated column mapping).
Thank you in advance.