Hi,
i use hibernate-annotations and i can store to DB java.util.Map (java.util.HashMap), but there is problem with column in DB for map-key, this column is still empty, but all other columns are ok.
House.java:
Code:
@OneToMany(mappedBy = "house", fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = com.xxx.model.HousePropertyValue.class)
@MapKey(name = "valueKey")
private Map<String, HousePropertyValue> housePropertyValues = new HashMap<String, HousePropertyValue>();
//get/set housePropertyValues
HousePropertyValue.java abstract class, DB table house_property_value, and value_key column for map-key, and value_type for discriminator
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public abstract class HousePropertyValue implements Serializable {
@Column(name = "value_key")
private String valueKey;
@ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "fk_house_id", referencedColumnName = "id")
private House house;
...
public String getValueKey() {
return valueKey;
}
public void setValueKey(String valueKey) {
this.valueKey = valueKey;
}
@Transient
public abstract Object getValue();
public abstract void setValue(Object value);
}
HousePropertyIntegerValue - implementation of HousePropertyValue :
Code:
@Entity
@DiscriminatorValue("INTEGER")
@javax.persistence.SequenceGenerator(
name="seq_house_property_value",
sequenceName="s_house_property_value",
allocationSize=1
)
public class HousePropertyIntegerValue extends HousePropertyValue {
@Column(name = "value_integer", nullable = false)
private Integer value;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public void setValue(Object value) {
this.value = (Integer) value;
}
....
}
but value_key column in house_property_value table si still empty.
for example :
Code:
house.getHousePropertyValues().put("description", new HousePropertyIntegerValue(10));
please, where is the problem ?
hibernate configuration:
Code:
<hibernate-configuration>
<session-factory>
class="com.xxx.model.HousePropertyValue"/>
<mapping class="com.xxx.model.HousePropertyIntegerValue"/>
</session-factory>
</hibernate-configuration>
thanks!
Ivan