我正在尝试创建一种使用 AbstractEntity 来建模我 future 的应用程序的方法 我现在的问题是 Postgres 的 Sequence 类型
在我的抽象类中,我现在不知道如何为每个实体类生成一个序列
可能吗?
摘要
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Abstract {
@Id
@GeneratedValue(generator="seq_Broker",strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="seq_Broker",sequenceName="seq_Broker")
private Long id;
}
实体模型
@Entity
@Table(name = "tb_EntityModel")
public class EntityModel extends Abstract{
private String value;
private String value2;
public EntityModel(String value, String value2) {
this.value = value;
this.value2 = value2;
}
}
最佳答案
任何继承类型都不可能使用@Entity
父类(super class),因为主表(Abstract
的表)将始终包含使用的ID - 并且它显然,您只能使用一个序列,否则您会遇到唯一性问题。
但是你可以为Abstract
定义@MappedSuperclass
:
@MappedSuperclass
public abstract class Abstract {
public static final String SEQUENCE_GENERATOR = "seq";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_GENERATOR)
private Long id;
}
@Entity
@Table(name = "tb_EntityModel")
@SequenceGenerator(name = Abstract.SEQUENCE_GENERATOR, sequenceName = "tb_entity_sequence")
public class EntityModel extends Abstract {
...
}
关于java - 如何使用@Inheritance加上GenerationType.SEQUENCE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726346/