[英]TransientObjectException: save the transient instance before flushing
I'm currently working on a project and I've encountered this error: 我目前正在处理一个项目,遇到了以下错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing
What happened: 1.) I have a session scope variable that I set after login, let's say SessionScopeVariableA. 发生了什么:1.)我有一个登录后设置的会话范围变量,假设为SessionScopeVariableA。
2.) Then I have a page where I'm adding an entity, let's say EntityA. 2)然后,我在其中添加实体的页面,即EntityA。
3.) EntityA has a lazy field sessionScopeVariableA, so when I invoke the add method I have to set this variable. 3.)EntityA有一个惰性字段sessionScopeVariableA,因此,当我调用add方法时,必须设置此变量。
entityA.setSessionScopeVariableA(sessionScopeVariableA);
em.persist(entityA);
4.) Note that SessionScopeVariableA is wrap in a session scope producer while the action is conversation scope. 4.)请注意,当操作为对话范围时,SessionScopeVariableA包装在会话范围生成器中。
5.) Whatever I do, I always end up with the transient error specified above. 5.)无论我做什么,我总是会遇到上面指定的瞬态错误。
Any idea? 任何想法?
What solved this problem was managing the connection resource with CDI using solder. 解决此问题的方法是使用焊料管理与CDI的连接资源。 This is how we did this:
这是我们这样做的方式:
//qualifier for the tenant //租户的限定词
@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() { //.. }
//in a separate util class, define how you want to manage the connection resource (cdi) //在单独的util类中,定义要如何管理连接资源(cdi)
@ExtensionManaged
@ConversationScoped
@Produces
@PersistenceUnit(unitName="myEM")
@MyEMJpa
private EntityManagerFactory em;
//interface for the connection resource //连接资源的接口
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE })
public @interface MyEMJpa { }
//inject entity manager in your service //在服务中注入实体管理器
@Inject
@MyEMJpa
protected EntityManager em;
//How to inject the current tenant //如何注入当前租户
@Inject
@CurrentTenant
private Provider currentTenant;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.