hibernate中的java继承插入示例

k10s72fa  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(270)

我想在产品类和笔记本产品类之间建立连接

@Entity
@Table(name = "products")
@Inheritance(strategy = InheritanceType.JOINED)
public class Product {
    @Id
    @Column(unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(nullable = false, unique = true)
    private String name;
    @Column(nullable = false)
    private BigDecimal cost;
    @Column(nullable = false)
    private int count;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "productStatus", nullable = false)
    private ProductStatus productStatus;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "productManufacture", nullable = false)
    private ProductManufacture productManufacture;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "productCategory", nullable = false)
    private ProductCategory productCategory;

笔记本电脑类

@Entity
@Table(name = "laptops")
public class Laptop extends Product {
    @Id
    @Column(unique = true, nullable = false)
    private int id;
    @Column(nullable = false)
    private String processor;
    private int videoMemory;
    @Column(nullable = false)
    private int ram;

但是我的变量不起作用……(我怎么做才能正确,我怎么能在数据库中插入一些数据?(请举例)

mv1qrgav

mv1qrgav1#

laptop正在扩展product类并从父类继承所有字段,包括id。您正在laptop类上再次定义id。继承策略也被加入,这意味着它处理父类和子类的id生成。删除id字段及其注解。

相关问题