休眠-@MappedSuperclass中的@OneToMany为每个子实体生成表

时间:2018-12-13 12:00:23

标签: java hibernate spring-data-jpa spring-data

我有以下实体,它是应用程序中其他所有实体的基础实体:

@Audited
@Data
@MappedSuperclass
public abstract class BaseEntity {

    public static final long UNSAVED = 0;

    @Id
    @GeneratedValue
    private long id;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Image> images;

    @OneToMany(cascade = CascadeType.ALL)
    private List<File> files;

}

我的目标是拥有两个表-图像文件,以便将这两个表共享给所有扩展BaseEntity的实体。我确实意识到,所有扩展BaseEntity的实体都应具有一个共享的ID序列。

然后我有两个实体,它们是BaseEntity的子类型:

@Entity
@Table
@Data
@EqualsAndHashCode(callSuper = false)
public class Equipment extends BaseEntity {
  private String name;
  @Enumerated(EnumType.STRING)
  private EquipmentType type;
  private String model;
  private String serial;
  private Boolean status;
}

@Entity
@Table
@Data
@EqualsAndHashCode(callSuper = false)
public class News extends BaseEntity {
  private String title;
  private String summary;
  private String content;
}

现在,当我设置spring.jpa.hibernate.ddl-auto = create-drop时,hibernate会为我生成以下表格:

news, equipment, news_images, news_files, equipment_images, equipment_files

但是,我想有四个表:

   news, equipment, files, images

我非常感谢您的帮助,

谢谢

1 个答案:

答案 0 :(得分:0)

尝试添加到@OneToMany列表中的@JoinColumn(name="ID_SOMETHING")。如果未指定news_images,则这两部分列名@JoinColumn是由Hibernate创建的联接表。