继承类上的Hibernate嵌套鉴别器

时间:2015-03-24 17:56:27

标签: java mysql hibernate maven inheritance

我有2个MYSQL表

表1

id     bigint auto increment Primary Key
type   Enum ('vegetable','fruit')
color  Enum ('green','red','yellow')

表2

id     bigint (same as the id in Table 1)
sweet  boolean
sour   boolean
.. other fields specific to type fruit

现在我创建3个对象,首先是父类

@Entity
@Configurable
@Table(name = "table1")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public class ParentClass {
 ..
}

现在是蔬菜的第二课

@Entity
@Configurable
@DiscriminatorValue("vegetable")
@DiscriminatorColumn(name = "color", discriminatorType = DiscriminatorType.STRING)
public class Vegetable extends Parent{
 ..
}

第三,水果类

@Entity
@Configurable
@SecondaryTables({ @SecondaryTable(name = "table2",
    pkJoinColumns = { @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id") }) })
@DiscriminatorValue("fruit")
@DiscriminatorColumn(name = "color", discriminatorType = DiscriminatorType.STRING)
public class Fruit extends Parent{
 ..
}

我需要第二个鉴别器在蔬菜和水果上添加更多继承的类(另外6个类),

@Entity
@Configurable
@DiscriminatorValue("red")
public class RedVegetable extends Vegetable{
..
}

@Entity
@Configurable
@DiscriminatorValue("green")
public class GreenFruit extends Fruit{
..
}

等等。

Hibernate不允许我这样做。我的设计有什么问题?提前致谢!

1 个答案:

答案 0 :(得分:0)

据悉,这不能在Hibernate中完成。因此,通过合并table1中的鉴别器朋友,如Enum('fruit | red','fruit | green','vegetable | red'..& so so),找到了另一种方法。 如果我错了,请纠正我。