0
votes

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.

2.) Then I have a page where I'm adding an entity, let's say EntityA.

3.) EntityA has a lazy field sessionScopeVariableA, so when I invoke the add method I have to set this variable.

entityA.setSessionScopeVariableA(sessionScopeVariableA);
em.persist(entityA);

4.) Note that SessionScopeVariableA is wrap in a session scope producer while the action is conversation scope.

5.) Whatever I do, I always end up with the transient error specified above.

Any idea?

1
is sessionScopeVariableA mapped as an entity too ?Gab
Yes, the app is a multi-tenant 1 so during login, the user who signin needs to select what tenant he has to work and that will be save in the session scope.czetsuya

1 Answers

1
votes

What solved this problem was managing the connection resource with CDI using solder. 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)

@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;