I have SuperClass where I have defined my properties and I want to apply one more annotation on one of the SuperClass Property.
@MappedSuperclass
public class CartBaseEntity {
private String cartName;
@Column(name = "cart_name")
public String getCartName() {
return cartName;
}
public void setCartName(String cartName) {
this.cartName = cartName;
}
}
And my Sub Class is in below:
@Entity
@Table(name = "CART2")
public class CartEntity extends CartBaseEntity implements Serializable {
private Set<Items> items;
@Basic(fetch = FetchType.LAZY)
@Override
public String getCartName() {
return super.getCartName();
}
}
I was trying to override the 'cartName' column and want to add @Basic annotation on it. Is it possible or is there any workarround? TIA
Yes, it's possible with @AttributeOverride annotation applied to a subclass:
@Entity
@Table(name = "CART2")
@AttributeOverride(name = "cartName", column = @Column(name="CART_NAME"))
public class CartEntity extends CartBaseEntity implements Serializable {
...
}
UPDATE: here's what JPA 2.1 Specification tells about overriding a column in a mapped superclass:
11.1.4 AttributeOverride Annotation
The
AttributeOverrideannotation is used to override the mapping of aBasic(whether explicit or default) property or field orIdproperty or field.The
AttributeOverrideannotation may be applied to an entity that extends a mapped superclass or to an embedded field or property to override aBasicmapping orIdmapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With