Hibernate:将table的foreign_key指向secondary表

时间:2017-03-08 15:46:40

标签: java database hibernate persistence hibernate-mapping

我有三个班级:

  • PlanItem
  • 任务
  • SubTask类

它有以下层次结构:

public abstract class PlanItem {...}

public class Task extends PlanItem {
   ...
   private Set<SubTask> subTasks;
   ...
}

public class SubTask {...}

我正在使用hibernate生成三个表:“PlanItem”,“Task”和“SubTask”。

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

    @Id
    private String id;

}

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

    @Column( table = "Task" )
    private String task_id;

    @OneToMany(mappedBy = "id")
    @Column( table = "Task" )
    private Set<SubTask> subTasks;

}

@Entity
public class SubTask {

    @Id
    @ManyToOne(targetEntity = Task.class)
    private String id;

}

这将生成正确的三个表,并生成以下外键关系:

alter table SubTask
add constraint FKrqtooosvfj0qtdol7arw1ur71 
foreign key (id) 
references PlanItem;

但我希望得到以下关系:

alter table SubTask
add constraint FKrqtooosvfj0qtdol7arw1ur71 
foreign key (task_id) 
references Task;

如何做到这一点?

0 个答案:

没有答案