- Advance
@MappedSuperclass is a annotation of java persistance. You can use to define mappings for an abstract or non-persistent superclass. Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior.
- Explanation
If many of our entities have audit fields we define a class with these fields:
@MappedSuperclass public abstract class AuditDomain implements Serializable { /** * serialVersionUID. */ private static final long serialVersionUID = 6570818726931423004L; /** * Logical Deletion. */ @Column(name = "LOGICAL_DEL") private Boolean logicalDeletion; /** * Date insert. */ @Column(name = "DATE_INSERT") @Temporal(TemporalType.TIMESTAMP) private Date dateInsert; /** * Date update. */ @Column(name = "DATE_UPDATE) @Temporal(TemporalType.TIMESTAMP) private Date dateUpdate; /** * Role insert. */ @Column(name = "ROLE_INSERT") private String roleInsert; /** * Role update. */ @Column(name = "ROLE_UPDATE") private String roleUpdate; /** * User insert. */ @Column(name = "USER_INSERT") private String userInsert; /** * User update. */ @Column(name = "USER_UPDATE") private String userUpdate; //getters and setters }
To every entity with audit fields, we only have to make them to extend from our class AuditDomain, and all these fields will be added by inheritance.
We saved encrypt all audit fields in all institutions. Now our code will be more readable and robust.
public class UserOnLine extends AuditDomain { private static final long serialVersionUID = 4892071387935963546L; @Id @Column(name = "USER_ON_LINE_ID") private Long id; @Column(name = "NAME") private String name; @Column(name = "EMAIL") private String email; @Column(name = "TELEPHONE") private String telephone; //getters and setters