我尝试使用JPA
连接两个表第一个表与实体ReportTripSingle相关联 第二个表与实体TripData相关联 使用复合密钥(实体TripDataPK)
描述第二个表的主键@Embeddable
public class TripDataPK implements Serializable {
public TripDataPK() {
}
@Column(name = "FTP_ID", nullable = false)
private long ftpId;
@Column(name = "FTP_BATCH_ID", nullable = false)
private long ftpBatchId;
@Column(name = "FTP_DAY", nullable = false)
private long ftpDay;
}
public class TripData implements java.io.Serializable {
@EmbeddedId
private TripDataPK tripDataPK;
// some properties ...
}
public class ReportTripSingle implements java.io.Serializable {
@Id
@Column(name = "TRE_ID", precision = 10, scale = 0)
private long id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID")
private TripData tripData;
}
如您所见,我想加入ReportTripSingle和TripData
它不起作用:(
这是堆栈跟踪:
引起:org.hibernate.AnnotationException: referencedColumnNames(FTP_ID)of com.nexo.susan.be.model.ReportTripSingle.tripData引用 com.nexo.susan.be.model.TripData未映射到单个属性 org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:336) 在 org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116) 在 org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1522) 在 org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1443) 在 org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1346) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
确实TripData没有映射到单个属性,但我想加入FTP_ID
问候。
答案 0 :(得分:0)
我无法加入复合键的一部分。 为了解决这个问题,我通过添加ftpBatchId和ftpDay作为外键修改了与实体ReportTripSingle相关联的表
如果有人有兴趣,这是我的实施:
实体ReportTripSingle
public class ReportTripSingle implements java.io.Serializable {
@Id
@Column(name = "TRE_ID", precision = 10, scale = 0)
private long id;
@Column(name = "TRE_FTP_ID", length = 15, nullable = false, precision = 10, scale = 0, insertable = false, updatable = false)
private long tripDataId;
@Column(name = "TRE_BATCH_ID", nullable = false, insertable = false, updatable = false)
private long tripBatchId;
@Column(name = "TRE_FTP_DEP_DAY", nullable = false,insertable = false, updatable = false)
private long tripDay;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID"),
@JoinColumn(name = "TRE_BATCH_ID", referencedColumnName = "FTP_BATCH_ID"),
@JoinColumn(name = "TRE_FTP_DEP_DAY", referencedColumnName = "FTP_DAY")
})
private TripData tripData;
}
答案 1 :(得分:0)
一个更好的选择是在{Joincolumn中使用修饰符insertable = false和updatable = false,如https://dwuysan.wordpress.com/2012/02/22/joincolumn-is-part-of-the-composite-primary-keys
所述public class ReportTripSingle implements java.io.Serializable {
@Id
@Column(name = "TRE_ID", precision = 10, scale = 0)
private long id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "TRE_FTP_ID", referencedColumnName = "FTP_ID", **insertable = false, updatable = false**)
private TripData tripData;
}