![](/img/trans.png)
[英]Saving lazy child is causing: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
[英]TransientObjectException: save the transient instance before flushing
我目前正在處理一個項目,遇到了以下錯誤:
org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing
發生了什么:1.)我有一個登錄后設置的會話范圍變量,假設為SessionScopeVariableA。
2)然后,我在其中添加實體的頁面,即EntityA。
3.)EntityA有一個惰性字段sessionScopeVariableA,因此,當我調用add方法時,必須設置此變量。
entityA.setSessionScopeVariableA(sessionScopeVariableA);
em.persist(entityA);
4.)請注意,當操作為對話范圍時,SessionScopeVariableA包裝在會話范圍生成器中。
5.)無論我做什么,我總是會遇到上面指定的瞬態錯誤。
任何想法?
解決此問題的方法是使用焊料管理與CDI的連接資源。 這是我們這樣做的方式:
//租戶的限定詞
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
public @interface CurrentTenant { }
//producer for the current tenant
@Produces
@Named("currentTenant")
@CurrentTenant
public Provider getCurrentTenant() { //.. }
//在單獨的util類中,定義要如何管理連接資源(cdi)
@ExtensionManaged
@ConversationScoped
@Produces
@PersistenceUnit(unitName="myEM")
@MyEMJpa
private EntityManagerFactory em;
//連接資源的接口
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
public @interface MyEMJpa { }
//在服務中注入實體管理器
@Inject
@MyEMJpa
protected EntityManager em;
//如何注入當前租戶
@Inject
@CurrentTenant
private Provider currentTenant;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.