Friday, May 15, 2009

JPA self join table

In my project, we have some properties which have a bunch of sub properties. For example

CA=Canada
    |—AB= Alberta
    |—BC= British Columbia
I generated a self join table tproperty.

image

Here is the entity.
public class PropertyImpl extends AbstractEntityImpl implements Property {     private long uidPk; 

    private String groupName;

    private String code;

    private Property parentProperty;

    private Set<Property> subProperties;

    private Collection<PropertyValue> propertyValues;    
@OneToMany(targetEntity = PropertyImpl.class, fetch = FetchType.EAGER, cascade = { CascadeType.ALL },
mappedBy = "parentProperty")

    public Set<Property> getSubProperties() {
        return subProperties;

    }    
public void setSubProperties(Set<Property> subProperties) {

        this.subProperties = subProperties;

        if (subProperties != null) {

            for (Property property : subProperties) {

                property.setParentProperty(this);

            }

        }

    }   

@ManyToOne(targetEntity = PropertyImpl.class)
@JoinColumn(name = "PARENT_PROPERTY_UID", nullable = true)


    public Property getParentProperty() {

        return parentProperty;

    }    
public void setParentProperty(Property parentProperty) {

        this.parentProperty = parentProperty;

    }

}

No comments:

Post a Comment