[@MappedSuperclass]
指定其映射信息应用于从其继承的实体的类。映射的超类没有为其定义单独的表。
使用MappedSuperclass 注释指定的类可以按照与实体相同的方式进行映射,
但映射仅适用于其子类,因为映射的超类本身不存在任何表。
当应用于子类时,继承的映射将应用于子类表的上下文中。
通过使用AttributeOverride和 AssociationOverride注解或相应的XML元素,
映射信息可以在这些子类中被覆盖。
注意:
An entity cannot be annotated with both @Entity and @MappedSuperclass
@MappedSuperclass
@Entity
不能同时使用
package sun.rain.amazing.javax.anno.domain.mapped;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
/**
* @author sunRainAmazing
*/
@MappedSuperclass
@Data
public class BaseMappedSuperclass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Version
private Integer version;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
private Date createdAt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
private Date updatedAt;
/**
* Callback methods annotated on the bean class must return void and take no arguments
* 不能有参数 和 返回值
* @return
*/
@PrePersist
public void onCreate() {
createdAt = updatedAt = new Date();
}
@PreUpdate
public void onUpdate() {
this.updatedAt = new Date();
}}