0
我有一個項目與模型如下圖所示(僞):休眠嵌套延遲加載和LazyInitializationException中
class Transaction{
@OneToMany(mappedBy="transaction", fetch = FetchType.LAZY)
private Set<TransactionVersion> transactionVersions;
}
class TransactionVersion{
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="ID_TRANSACTION_VERSION")
private Transaction transaction;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="ID_CLIENT_VERSION")
private ClientVersion clientVersion;
}
class ClientVersion{
@OneToMany(mappedBy="clientVersion",fetch=FetchType.Lazy)
private Set<TransactionVersion> transactionVersions;
@OneToMany(mappedBy="client", fetch=FetchType.LAZY)
private Set<Client> clients;
}
現在,如果我試圖讓這樣的事(讓(0)只是一個例子):
transaction.getTransactionVersions().get(0)
.getClientVersion().getClients()
我得到LazyInitializationException中未能懶洋洋地初始化角色的集合:ClientVersion.clients,沒有會話或會話關閉
如果我改變關係betwee FetchType ClientVersion和客戶端EAGER它的工作原理,但我一定寧願讓它加載懶惰。
我知道我可以手動初始化集合,如果我有訪問休眠會話,但有任何方法來映射它,讓Hibernate可以自己做到這一點?我不想在我想獲得一些相關對象時使用自定義方法。