Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA-override annotation on SuperClass property

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

like image 708
pijushcse Avatar asked Dec 24 '22 12:12

pijushcse


1 Answers

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 AttributeOverride annotation is used to override the mapping of a Basic (whether explicit or default) property or field or Id property or field.

The AttributeOverride annotation may be applied to an entity that extends a mapped superclass or to an embedded field or property to override a Basic mapping or Id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).

like image 54
wypieprz Avatar answered Jan 04 '23 05:01

wypieprz