1.@MappedSuperclass 含义
1.1)基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclass注解将实体类的多个属性分别封装到不同的非实体类中。例如,数据库表中都需要id来表示编号,id是这些映射实体类的通用的属性,交给jpa统一生成主键id编号,那么使用一个父类来封装这些通用属性,并用@MappedSuperclas标识。
1.2)注意:
1.2.1)标注为@MappedSuperclass的类将不是一个完整的实体类,他将不会映射到数据库表,但是他的属性都将映射到其子类的数据库字段中。
1.2.2)标注为@MappedSuperclass的类不能再标注@Entity或@Table注解,也无需实现序列化接口。
@MappedSuperclasspublic abstract class Test extends Entitlement
{
private static final long serialVersionUID = 858491978870675469L;
@Column(name = HanaDbColumnConstants.CREATED_BY, nullable = false)
private String createdBy;
@Column(name = HanaDbColumnConstants.CREATED_ON, nullable = false)
private Timestamp createdOn;
@Column(name = HanaDbColumnConstants.CHANGED_BY, nullable = false)
private String changedBy;
@Column(name = HanaDbColumnConstants.CHANGED_ON, nullable = false)
private Timestamp changedOn;
}
2.子类重写父类字段名
2.1)In some cases, we don't want to use some fields which they are extending from super class. So ,in this case, we have to reuse these field. Here is the way:
eg: If you want to rename "createdBy" field of super class "Test" to "HanaDbColumnConstants.GROUP_CODE"
@Entity(name = HanaDbEntityConstants.ENTITY_ENTITLEMENT_REPOSITORY_INFO)
@Table(name = HanaDbViewConstants.VIEW_ENTITLEMENT_REPOSITORY_INFO)
@AttributeOverride(name = "createdBy", column = @Column(name = HanaDbColumnConstants.GROUP_CODE))
public class EntitlementRepositoryInfoView extends Test
{
}
Ok, now,you can have a try.