【发布时间】:2016-03-18 20:28:26
【问题描述】:
我对使用 JPA/Hibernate 还是很陌生。我正在尝试找出用复合键连接两个表且没有真正的数据库关系的最佳方法。
@Embeddable
public class StepPK implements Serializable{
@Column(name = "step_id")
protected String stepId;
@Column(name = "packet_id")
protected String packetId;
...
}
@Entity
@Table(name = "step_def")
@IdClass(StepPK.class)
public class InprocessPacketStep implements Serializable{
@Id
private String stepId;
@Id
private String packetId;
}
@Embeddable
public class StepDetailPK implements Serializable {
@Column(name = "step_id")
protected String stepId;
@Column(name = "packet_id")
protected String packetId;
@Column(name="user_id")
protected String userId;
@Column(name="parallel_step_id")
protected String parallelId;
...
}
@Entity
@Table(name = "step_detail")
@IdClass(StepDetailPK.class)
public class InprocessPacketStepDetail implements Serializable {
@Id
private String stepId;
@Id
private String packetId;
@Id
private String parallelId;
@Id
private String userId;
}
到目前为止,我在单独从表格中提取信息时效果很好,但我需要一些方法将它们加入 step_id。由于确实没有分配关系,因此执行 OneToMany 和 JoinColumns 似乎不起作用。
我尝试过使用 ManyToMany,它在编译时不会崩溃,但在检查我的测试时,它会导致 SQLGrammar 异常。
我唯一的另一个想法是通过查询手动加入它们,但运行 entityManager.createQuery("select InprocessPacketStep step join inprocessPacketStepDetail d on d.stepId = step.stepId") 会导致运行时错误。
任何建议将不胜感激。我花了很多时间在谷歌上搜索尝试不同的东西,但没有成功。
【问题讨论】:
-
哦,上面的图片链接是基本的表格描述
-
请提供有关所遇到错误的更多信息。另外,你想得到什么输出?
-
为什么不能使用附加关系表?这些实体之间是什么关系?