如何防止Hibernate生成表与单表策略中的实体一样多?

时间:2012-06-26 09:51:36

标签: java hibernate single-table-inheritance


下面的代码是我的对象模型,我需要为这些实体创建两个表。一个用于InheritanceType.JOINED策略,另一个用于InheritanceType.SINGLE_TABLE策略。 但不幸的是,Hibernate创建了像实体一样的表格。我该怎么办?
提前致谢。

@MappedSuperClass 
public abstract class EntityObject implements Cloneable, Serializable {
private Long id;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}


@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class SuperParent extends EntityObject{

private String lastName;

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

}


@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "d_type", discriminatorType = DiscriminatorType.STRING)
public class Parent extends SuperParent {
private String firstName;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}
}


@Entity
@DiscriminatorValue("Child1")
public class Child1 extends Parent {
private String age;

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}
}


@Entity
@DiscriminatorValue("child2")
public class Child2 extends Parent {

private String hairColor;

public String getHairColor() {
    return hairColor;
}

public void setHairColor(String hairColor) {
    this.hairColor = hairColor;
}
}

1 个答案:

答案 0 :(得分:0)

不支持将连接的继承策略与其他继承策略混合使用。 Hibernate仅支持将每个类的表与每子类的表继承混合。