Hibernate JPA @AttributeOverride Example

The @AttributeOverride annotation is used to override an attribute mapping inherited from a mapped superclass or an embeddable.
When AttributeOverride is applied to a map, "key." or "value." must be used to prefix the name of the attribute that is being overridden in order to specify it as part of the map key or map value.

To override mappings at multiple levels of embedding, a dot (".") notation form must be used in the name element to indicate an attribute within an embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

If AttributeOverride is not specified, the column is mapped the same as in the original mapping.

Example 1:

    @MappedSuperclass
    public class Employee {
        @Id protected Integer id;
        @Version protected Integer version;
        protected String address;
        public Integer getId() { ... }
        public void setId(Integer id) { ... }
        public String getAddress() { ... }
        public void setAddress(String address) { ... }
    }

    @Entity
    @AttributeOverride(name="address", column=@Column(name="ADDR"))
    public class PartTimeEmployee extends Employee {
        // address field mapping overridden to ADDR
        protected Float wage();
        public Float getHourlyWage() { ... }
        public void setHourlyWage(Float wage) { ... }
    }


    Example 2:

    @Embeddable public class Address {
        protected String street;
        protected String city;
        protected String state;
        @Embedded protected Zipcode zipcode;
    }

    @Embeddable public class Zipcode {
        protected String zip;
        protected String plusFour;
    }

    @Entity public class Customer {
        @Id protected Integer id;
        protected String name;
        @AttributeOverrides({
            @AttributeOverride(name="state",
                               column=@Column(name="ADDR_STATE")),
            @AttributeOverride(name="zipcode.zip",
                               column=@Column(name="ADDR_ZIP"))
        })
        @Embedded protected Address address;
        ...
    }


    Example 3:

    @Entity public class PropertyRecord {
        @EmbeddedId PropertyOwner owner;
        @AttributeOverrides({
            @AttributeOverride(name="key.street",
                               column=@Column(name="STREET_NAME")),
            @AttributeOverride(name="value.size",
                               column=@Column(name="SQUARE_FEET")),
            @AttributeOverride(name="value.tax",
                               column=@Column(name="ASSESSMENT"))
        })
       @ElementCollection
       Map<Address, PropertyInfo> parcels;
    }

    @Embeddable public class PropertyInfo {
        Integer parcelNumber;
        Integer size;
        BigDecimal tax;
    }

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation