Hibernate: point foreign_key of table to secondary table

本小妞迷上赌 提交于 2019-12-13 06:15:04

问题


I have three classes:

  • PlanItem
  • Task
  • SubTask

It has the following hierachy:

public abstract class PlanItem {...}

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

public class SubTask {...}

I am using hibernate to generate three tables: "PlanItem", "Task" and "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;

}

This generates the correct three tables and it generates the following foreign key relation:

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

but I would like to get the following relation:

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

How can this be done?

来源:https://stackoverflow.com/questions/42675789/hibernate-point-foreign-key-of-table-to-secondary-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!