【问题标题】:spring jpa - inheriting entity does not get its own id columnspring jpa - 继承实体没有自己的id列
【发布时间】:2018-08-03 10:23:48
【问题描述】:

我有一个实体HygieneItem 继承自Item

@Entity
@Table(name="hygieneitems")
public class HygieneItem extends Item
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "hygieneitem_id")
    private long id;

    @Column(name="retailer")
    private String retailer;
    // getters & setters
}

@Entity
@Table(name="items")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Item
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "item_id")
    private long id;

    @Column(name="item_name")
    private String itemName;
    //getters & setters
}

但是hygieneitems 表使用item_id 而不是它自己的hygieneitem_id

Hibernate: create table hygieneitems (item_id bigint not null, item_name varchar(255), cart_id bigint not null, retailer varchar(255), primary key (item_id))
Hibernate: create table items (item_id bigint not null, item_name varchar(255), cart_id bigint not null, primary key (item_id))

jpa documentation我刚刚读到

[...] InheritanceType.TABLE_PER_CLASS,每个具体类都映射到数据库中的单独表。类中的所有字段或属性,包括继承的字段或属性,都映射到数据库中类表中的列。

我如何告诉 JPA 它应该为 hygieneitems 表提供自己的 ID 列以及在 HygieneItemItem 中定义的列(除了 item_id 列)?

该项目可以在github找到

感谢您的帮助

【问题讨论】:

    标签: java jpa spring-data-jpa


    【解决方案1】:

    您不能在继承层次结构中覆盖 id。

    所以主键@Id总是定义在父类中。

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多