我在数据库的两个表中有以下数据(为清楚起见,省略了其他列)。这基本上代表了一个有向图,在其自己的表中存在子步骤。外键按预期方式布置。
mysql> select * from template_step;
+----+-----------+
| id | step_name |
+----+-----------+
| 1 | Step 1 |
| 2 | Step 2 |
| 3 | Step 3 |
| 4 | Step 4 |
+----+-----------+
mysql> select * from next_steps;
+-----------------+--------------+
| current_step_id | next_step_id |
+-----------------+--------------+
| 1 | 2 |
| 1 | 3 |
| 3 | 4 |
+-----------------+--------------+
然后我有了一个代表第一个表的Entity类,并且我试图找出正确的JPA设置,以便将子步骤作为此类中的列表公开。
@Entity
@Table(name = "template_step")
public class TemplateStepRecord {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="step_name")
private String name;
@OneToMany
@JoinTable(name="next_steps", joinColumns={@JoinColumn(name="next_step_id", referencedColumnName="current_step_id")})
private List<TemplateStepRecord> children = new ArrayList<>();
}
在children
上的那些注释显然是不正确的,因为我遇到了这样的错误:
Unable to find column with logical name: current_step_id in org.hibernate.mapping.Table(template_step) and its related supertables and secondary tables
我不确定要在此处添加什么内容,也不确定是否应该在该内容上或OneToMany
注释上添加任何其他注释。
谢谢。
答案 0 :(得分:0)
这是一个OneToMany / ManyToOne双向映射。 参见https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/one-to-many-bidirectional.html
@Entity
@Table(name = "template_step")
public class TemplateStepRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "step_name")
private String name;
@OneToMany(mappedBy="templateStepRecord")
private final List<TemplateNextStepRecord> children = new ArrayList<>();
}
@Entity
@Table(name = "next_steps")
public class TemplateNextStepRecord {
@Id
@Column(name="next_step_id")
private Long id;
@ManyToOne
@JoinColumn(name="current_step_id")
private TemplateStepRecord templateStepRecord;
}