当我使用table-per-subclass策略时,我的基类(父类)包含复合主语。
@Column(nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XXX")
@SequenceGenerator(name = "XXX", sequenceName = "XXX", allocationSize = 1)
private Long systemId;
@Id
@Column(nullable = false)
private Long version;
因此,所有扩展它的实体(因为我得到它)都会继承id及其带注释的属性。 问题是,当我创建一个新的Parent实体时,主要是从序列创建的,没关系。但是当我创建子类实例时,它会增加我不需要的父ID(从seq创建一个新的主要内容),`coz我使用table-per-subclass和primaries gottta是相同的。
问题:如何在Subclasses中抑制id生成?
@Entity
@PrimaryKeyJoinColumns(
{
@PrimaryKeyJoinColumn(name = "systemId"),
@PrimaryKeyJoinColumn(name = "version")
}
)
public class SUb extends Parent {
public SUb (Parent t) {
super(t);
}
public SUb () {
}
...
... no ids...
答案 0 :(得分:0)
创建子类时,它将发出两个插入:
子类不继承@Ids,而是使用:
作为FK到Base表的关联列。
因此,虽然基类有一个由这两列组成的主键:
子类应该有:
因此,在插入子类实体时,不应该调用该序列两次。
您能否确认数据库表符合此设计?
答案 1 :(得分:0)
我无法重现您的问题。您能更具体地了解您的加盟策略是什么?是InheritanceType.TABLE_PER_CLASS
还是InheritanceType.JOINED
?尽管如此,我还是设法用TABLE-PER-CLASS连接策略准备了完整的例子,当你持久SubDepartment
时,它只会增加一次序列。也许有帮助。
@Entity
@IdClass(CompoundPK.class)
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Department {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XXX")
@SequenceGenerator(name = "XXX", sequenceName = "XXX", allocationSize = 1)
@Id
private Long id;
@Id
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
@Entity
public class SubDepartment extends Department {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class CompoundPK implements Serializable {
private Long id;
private Long version;
public CompoundPK() {
}
public CompoundPK(Long id, Long version) {
this.id = id;
this.version = version;
}
public Long getId() {
return id;
}
public Long getVersion() {
return version;
}
// hashCode & equals
}