Hi, is there a way to define a compund key as the keyMap for a Hashmap?
Here is my test code.
User Object
Code:
@Entity
@Table(name="TEST_USER")
public class User {
private int id;
private String name;
private UserEnum type;
private Map<AttributeInstance,UserAttribute> attributes=new HashMap<AttributeInstance,UserAttribute>();
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="USER_GENERATOR")
@SequenceGenerator(name="USER_GENERATOR", sequenceName="USER_SEQUENCE")
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Enumerated(EnumType.STRING)
@Column(name="type")
public UserEnum getType() {
return type;
}
public void setType(UserEnum type) {
this.type = type;
}
/**
* @return the attributes
*/
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="USER_ID")
@MapKey(name="attributeInstance")
public Map<AttributeInstance, UserAttribute> getAttributes() {
return attributes;
}
/**
* @param attributes the attributes to set
*/
public void setAttributes(Map<AttributeInstance, UserAttribute> attributes) {
this.attributes = attributes;
}
public Object getAttribute(String name, int index) {
return attributes.get(new AttributeInstance(name, index));
}
}
UserAttribute Class
Code:
@Entity
@Table(name="USER_ATTRIBUTE")
public class UserAttribute {
private Integer id;
private String value;
private AttributeInstance attributeInstance=new AttributeInstance();
/**
* @return the id
*/
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.AUTO, generator="USERATT_GENERATOR")
@SequenceGenerator(name="USERATT_GENERATOR", sequenceName="USERATT_SEQUENCE")
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return attributeInstance.name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.attributeInstance.name = name;
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
public AttributeInstance getAttributeInstance() {
return attributeInstance;
}
/**
* @param attributeInstance the attributeInstance to set
*/
public void setAttributeInstance(AttributeInstance attributeInstance) {
this.attributeInstance = attributeInstance;
}
/**
* @return the index
*/
public int getIdx() {
return attributeInstance.index;
}
/**
* @param index the index to set
*/
public void setIdx(int index) {
this.attributeInstance.index = index;
}
}
AtrributeInstance class contains name and index key. This properties identify an attribute.
I need to get a HashMap for user attributes indexed by pair (name,index). The idea is to have in one attribute name several values referenced by index property.
Thanks in advance.
AgustÃn Almonte