When we develop a project, the same city defines the entity, there are some abstractions, that is, some object-oriented ideas. 1 For example, a data entity or other type of entity will have an ID field of 2. For data entities there will always be creator, creation time, update person, update time, notes, and so on.
At this time we must be thinking of these public abstractions into the public Vo, and then the specific different VO to inherit these public vo.
In JPA, because each entity corresponds to a table, it is possible to have inherited, but not to scan the fields in the public entities, when we can use the annotation @mappedsuperclass, which is the role of this entity as a base class entity, These fields are scanned automatically when the entity that inherits it is scanned.
BaseEntity:
Package Uap.web.persistence;import Java.io.serializable;import javax.persistence.id;import Javax.persistence.MappedSuperclass;/** * * * * @author HOTUSM * @since 2016-03-18 * Entity support class*/@MappedSuperclass Public classBaseEntity implements serializable{/** * */ Private StaticFinalLongSerialversionuid =1L; @IdprotectedString ID; PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } }
Dataentity:
Package Uap.web.persistence;import java.io.serializable;import java.sql.date;import javax.persistence.Id; Import Javax.persistence.mappedsuperclass;import Com.fasterxml.jackson.annotation.JsonFormat;/** * * * * @author HOTUSM * @since 2016-03-18 * Because the relationship between the creator and the updated person is more complex, there is no definition in it*/@MappedSuperclass Public classDataentity extends baseentity{/** * */ Private StaticFinalLongSerialversionuid =-3203004068002303110L; @IdprotectedString ID; /** * Creation time*/@JsonFormatprotectedDate CreateDate; /** * Update Time*/@JsonFormatprotectedDate updatedate; /** * Remarks*/ protectedString Remark; PublicDate getcreatedate () {returnCreateDate; } Public voidsetcreatedate (Date createdate) { This. CreateDate =CreateDate; } PublicDate getupdatedate () {returnupdatedate; } Public voidsetupdatedate (Date updatedate) { This. updatedate =updatedate; } PublicString Getremark () {returnremark; } Public voidSetremark (String remark) { This. Remark =remark; } PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } }
Then we can use the JPA annotations in our specific VO, but these public fields are not needed.
JPA entity Vo use of inherited entities @mappedsuperclass annotations