Hi
I wish to map some custom attributes of an entity, stored in a Map<String,String>.
The entity is Customer and the map property is customAttributes. They should be stored in a second table:
customerid INTEGER,
name VARCHAR,
value VARCHAR,
Primary Key (customerid, name)
Is this possible with Hibernate Annotations Extentions or do I have to define a second Entity for the customAttributes?
I tried with a second Entity, but I cannot get it to work:
@Entity
public class Customer {
private Long id;
private String lastName;
private Map<String, CustomAttribute> customAttributes;
@OneToMany (cascade=CascadeType.ALL)
@JoinColumn (name="customer_id")
@MapKey (name="name")
public Map<String,CustomAttribute> getCustomAttributes() {
return customAttributes;
}
...
}
@Entity
public class CustomAttribute {
private String name;
private String value;
@Id
public String getName() {
return name;
}
...
}
I get an
IllegalArgumentException occurred calling getter of CustomAttribute.name
caused by:
java.lang.IllegalArgumentException: object is not an instance of declaring class
The second thing is that I don't know how to define the @Id in a correct way (should be composite). I would prefer a simple value mapping without the need for a second entity...
Thanks a lot.
|