我們有2個具有@ManyToOne關係的實體。JPA insertable = false updatable = false id創建時未更新
當我們在@Transactional方法中創建一個EntityB實例entityAld(insertable = false updatable = false)時,它不會自動更新 - 即使entityA實例已經持久化了。
有沒有辦法解決這個問題?我們是否必須在ctor中手動更新它?
@Entity
public class EntityA {
@Id
@GeneratedValue
private Long id;
public EntityA() {
super();
}
...
}
@Entity
public class EntityB {
@Id
@GeneratedValue
private Long id;
@ManyToOne(optional = true, fetch = FetchType.LAZY)
private EntityA entityA;
@Column(name = "entityA_id", insertable = false, updatable = false)
private Long entityAId;
public EntityB() {
super();
}
public EntityB(EntityA entityA) {
super();
this.entityA = EntityA;
}
...
}
編輯:也試過以下,但仍entityAId = NULL交易(即使entityA是之前持續)內。
@Entity
public class EntityB {
@Id
@GeneratedValue
private Long id;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "entityA_id", insertable = false, updatable = false)
private EntityA entityA;
@Column(name = "entityA_id")
private Long entityAId;
...
}
你是如何堅持'EntityB'與'EntityA'關聯的?請編輯答案以添加代碼。如果您使用其構造函數創建entityB並傳遞了entityA,那麼發佈的第一個代碼片段應該可以工作,所以我認爲問題與邏輯和事務有關。另一個評論是,如果你沒有設置正確的'entityAId'值,你發佈的第二個代碼將不會保持關係。 – Guillermo