Hibernate混合继承策略用于多层次层次

时间:2017-02-28 09:16:38

标签: java database hibernate persistence hibernate-mapping

我有以下类结构:

Class diagram

我想用hibernate生成以下三个表:

  • PlanItem:包含其所有属性
  • 任务:包含其所有属性及其子属性
  • 阶段:具有所有属性

我试图混合继承类型,但它没有工作:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class PlanItem {...}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Stage extends PlanItem {...}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Task extends PlanItem {...}

@Entity
public class HumanTask extends Task {...}

@Entity
public class ProcessTask extends Task {...}

@Entity
public class CaseTask extends Task {...}

我已经尝试过二级表,并且或多或少得到了预期的结果:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class PlanItem {...}

@Entity
@SecondaryTable( name = "Stage" )
public class Stage extends PlanItem {...}

@Entity
@SecondaryTable( name = "Task" )
public class Task extends PlanItem {...}

@Entity
@SecondaryTable( name = "Task" )
public class HumanTask extends Task {...}

@Entity
@SecondaryTable( name = "Task" )
public class ProcessTask extends Task {...}

@Entity
@SecondaryTable( name = "Task" )
public class CaseTask extends Task {...}

但我想知道是否存在更优雅和简单的解决方案?

1 个答案:

答案 0 :(得分:1)

目前无法进行此操作,请参阅following issue以获取更多信息。我认为使用辅助表的“黑客”目前是最好的。