Friday, May 15, 2009

JPA: use of Map collections for OneToMany

Here is the tables

image

Data sample:

UIDPK GROUP_NAME CODE PARENT_PROPERTY_UID
1 “COUNTRY” CA NULL

 

UIDPK PROPERTY_UID VALUE LOCALE
1 1 CANADA en
2 1 KANTANA fr

The property value would be a collection in the object Property. We want to use Map here and the key will be “LOCALE”

public class PropertyImpl {
…    
@OneToMany(targetEntity = PropertyValueImpl.class, fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
    @MapKey(name = "locale")
@ElementJoinColumn(name = "PROPERTY_UID", nullable = false)
    public Map<String, PropertyValue> getPropertyValues() {
        return propertyValues;

    }    
public void setPropertyValues(Map<String, PropertyValue> propertyValues) {
        this.propertyValues = propertyValues;

    }

The “locale” in @MapKey should be one property name in PropertyValueImpl.class.

No comments:

Post a Comment