Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - Hibernate: point foreign_key of table to secondary table

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...