Tôi có các loại lớp sau đây để phân cấp thực thể ngủ đông. Tôi đang cố gắng có hai lớp phụ cụ thể Sub1Class
và Sub2Class
. Chúng được phân tách bằng một cột phân biệt đối xử (field
) được xác định trong MappedSuperClass
. Có một lớp thực thể trừu tượng EntitySuperClass
được tham chiếu bởi các thực thể khác. Các thực thể khác không nên quan tâm nếu chúng thực sự tham chiếu Sub1Class
hoặc Sub2Class
.Hibernate, thừa kế bảng đơn và trường sử dụng từ siêu lớp làm cột phân biệt đối tượng
Điều này thực sự có thể? Hiện nay tôi nhận được lỗi này (vì định nghĩa cột được thừa hưởng hai lần trong Sub1Class và trong EntitySuperClass):
Repeated column in mapping for entity: my.package.Sub1Class column: field (should be mapped with insert="false" update="false")
Nếu tôi thêm @MappedSuperClass
để EntitySuperClass
, sau đó tôi nhận được lỗi khẳng định từ hiberante: nó không giống như nếu một lớp là cả Thực thể và một lớp siêu ánh xạ. Nếu tôi xóa @Entity
khỏi EntitySuperClass
, lớp đó không còn là thực thể và không thể được tham chiếu từ các thực thể khác:
MappedSuperClass
là một phần của gói bên ngoài, vì vậy nếu có thể, không nên thay đổi.
lớp của tôi:
@MappedSuperclass
public class MappedSuperClass {
private static final String ID_SEQ = "dummy_id_seq";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ)
@GenericGenerator(name=ID_SEQ, strategy="sequence")
@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false)
private Integer id;
@Column(name="field", nullable=false, length=8)
private String field;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
@Entity
@Table(name = "ACTOR")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="field", discriminatorType=DiscriminatorType.STRING)
abstract public class EntitySuperClass extends MappedSuperClass {
@Column(name="description", nullable=false, length=8)
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@Entity
@DiscriminatorValue("sub1")
public class Sub1Class extends EntitySuperClass {
}
@Entity
@DiscriminatorValue("sub2")
public class Sub2Class extends EntitySuperClass {
}
@Entity
public class ReferencingEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
@Column
private Integer value;
@ManyToOne
private EntitySuperClass entitySuperClass;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public EntitySuperClass getEntitySuperClass() {
return entitySuperClass;
}
public void setEntitySuperClass(EntitySuperClass entitySuperClass) {
this.entitySuperClass = entitySuperClass;
}
}
Nhưng tại sao bạn muốn để lộ bộ phân biệt cột? Cột này thường là chi tiết triển khai "ẩn" mà bạn không muốn hiển thị. –
Cột phân biệt đối xử đã được hiển thị trong MappedSuperClass là một phần của gói bên ngoài. Một gói tôi muốn tránh sửa đổi, nếu có thể. –