背景
Entity 是数据库 ORM 框架表结构的映射, BaseEntity 用来设置每张表的共同部分,然后其他表继承它即可.
定义
必须使用 MappedSuperclass 装饰器.
@Getter
@Setter
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
使用
@Entity
@Table(name = "cart_items")
@Data
@NoArgsConstructor
public class CartItem extends BaseEntity {
@Column(length = 40, nullable = false, unique = true)
private String name;
@Column(length = 150, nullable = false)
private String description;
}