0

Am struggling for almost a day trying to prevent saving to child entity(USER) it will have value already so dont perform insert/update on user from company.

Reference Links

Actual code:

Company:

@OneToMany(mappedBy = "parentCompany")
private List<User> users;

User:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_COMPANIES")
private Company parentCompany;

Tried one by one

@OneToMany(mappedBy = "parentCompany", cascade = CascadeType.PERSIST)
@JoinColumn(name = "ID_USERS", insertable = false, updatable = false)
private List<User> users;

@OneToMany(mappedBy = "parentCompany")
@JoinColumn(name = "ID_USERS", insertable = false, updatable = false)
private List<User> users;    


@OneToMany(mappedBy = "parentCompany", fetch = FetchType.LAZY)
private List<User> users;
 // called getuser().size to initialize 

@OneToMany(mappedBy = "parentCompany", fetch = FetchType.EAGER)
private List<User> users; 
// called getuser().size to initialize and its returning vector but insert not preventing.

Saving:

public T update(T t) {
   return this.entityManager.merge(t);
}

Error:

 Caused by: 
    java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.volvocars.tis.domain.model.User@4.
        at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:313)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:727)
        at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.writeChanges(RepeatableWriteUnitOfWork.java:441)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:878)
        at org.eclipse.persistence.internal.jpa.QueryImpl.performPreQueryFlush(QueryImpl.java:967)
        at org.eclipse.persistence.internal.jpa.QueryImpl.executeUpdate(QueryImpl.java:296)
        at com.volvocars.tis.domain.dao.bean.CompanyAddressDaoBean.updateVolvoAddress(CompanyAddressDaoBean.java:99)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
        at java.lang.reflect.Method.invoke(Method.java:508)
        at com.ibm.ejs.container.EJSContainer.invokeProceed(EJSContainer.java:4886)
        at com.ibm.ejs.container.interceptors.InvocationContextImpl.proceed(InvocationContextImpl.java:592)
        at com.ibm.ws.cdi.ejb.impl.InterceptorChain.proceed(InterceptorChain.java:119)
        at com.ibm.ws.cdi.ejb.impl.EJBCDIInterceptorWrapper.invokeInterceptors(EJBCDIInterceptorWrapper.java:131)
        at com.ibm.ws.cdi.ejb.impl.EJBCDIInterceptorWrapper.aroundInvoke(EJBCDIInterceptorWrapper.java:54)
        at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
        at java.lang.reflect.Method.invoke(Method.java:508)
        at com.ibm.ejs.container.interceptors.InterceptorProxy.invokeInterceptor(InterceptorProxy.java:189)
        at com.ibm.ejs.container.interceptors.InvocationContextImpl.proceed(InvocationContextImpl.java:577)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
        at com.ibm.ws.cdi.ejb.interceptor.WeldSessionBeanInterceptorWrapper.aroundInvoke(WeldSessionBeanInterceptorWrapper.java:58)
        at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
        at java.lang.reflect.Method.invoke(Method.java:508)
        at com.ibm.ejs.container.interceptors.InterceptorProxy.invokeInterceptor(InterceptorProxy.java:189)

How to prevent saving child entity on particular case? Any way can achieve programmatically or with annoation?

James Z
  • 12,209
  • 10
  • 24
  • 44
senthil kumar
  • 237
  • 1
  • 6
  • 18
  • Have you tried reading the instance in through this EntityManager context and then using that reference in your object graph? The exception you see is required by JPA any time the provider comes across an instance that isn't managed - Instances read from the persistence unit are managed, as is anything persisted or the return value from a merge call – Chris Oct 05 '21 at 13:48
  • Or make sure you don't touch the Company's user list at all. Merge on company should not be given a new company instance - use the Company instance read in from the EntityManager and make your changes to it directly. This would avoid replacing the user list with your unmanaged users. – Chris Oct 05 '21 at 13:54
  • Thank you for your reply ..actually issue with some other logic when we deeply analyzed its assigning wrong value to user while saving in company .. its corrected and its working fine now.. – senthil kumar Oct 06 '21 at 09:28

0 Answers0