This method begins with Transaction
@Transactional(propagation = Propagation.REQUIRED)
public Account generateDirectCustomerAccountEntity(AccountReq source, Map<String, String> headers, String workflowId) {
Account dbAccount = dCustomerModelToEntityMapper.map(source, headers);
dbAccount = saveAccount(source, dbAccount, headers, workflowId);
return dbAccount;
}
This is a mapper class where I create a DB Account entity and map addresses and contacts.
@Override
@LogExecutionTime(log=true)
public Account map(AccountReq accountReq, Map<String, String> headers) {
logger.info("AccountModelToEntityMapper.mapDirectCustomer*****START");
Account dbAccount = new Account();
AccountCode accountCode = dbService.getAccountCodeForDirectCustomer(accountReq);
String accountId = dbService.genarateAccountId(accountCode);
logger.info("genarated AccountID for Direct Customer is={}", accountId);
dbAccount.setAccountId(accountId);
setAccountContact(dbAccount, accountReq, headers);
setAddress(dbAccount, accountReq);
dbAccount.setAccountType(accountCode.getCodeId());
return dbAccount;
}
When I don't call the below method inside the map, everything works fine. But when I call it I get the error described in the title.
private void setAccountContact(Account dbAccount, AccountReq accountReq, Map<String, String> headers) {
try {
if (null != accountReq.getContacts() && !accountReq.getContacts().isEmpty()) {
logger.info("setAccountContact accountReq.getContacts().size()={}", accountReq.getContacts().size());
List<String> emailList=new ArrayList<>();
for (ContactReq contact : accountReq.getContacts()) {
String email = contact.getEmail();
logger.info("setAccountContact:"+email);
if(emailList.contains(email)) {
logger.error("ERROR={}", "same emailId sent in multiple contacts");
throw new AccountException(AccountConstant.INVALID_FIELDS, AccountConstant.INVALID_FIELDS_CODE);
}
emailList.add(email);
Contact dbcontact = contactRepository.findByEmail(email);
if (null == dbcontact) {
dbcontact = new Contact();
dbcontact.setCreatedAt(dbAccount.getCreatedAt());
dbcontact.setCreatedBy(dbAccount.getCreatedBy());
dbcontact.setEmail(contact.getEmail());
dbcontact.setFirstName((contact.getFirstName()));
dbcontact.setLastName((contact.getLastName()));
dbcontact.setPhone(contact.getPhoneNumber());
dbcontact.setStatus(AccountConstant.STATUS_ACTIVE);
logger.info("contactRepository={} {}", contactRepository,contact.getEmail());
try {
dbcontact = contactRepository.save(dbcontact);
} catch (Exception e) {
logger.error("ERROR in updating contact table={}", e);
throw new InternalServerException(AccountConstant.INTERNAL_SERVER_ERROR,
AccountConstant.INTERNAL_SERVER_ERROR_CODE);
}
}
AccountContact dbAccountContact = new AccountContact();
dbAccountContact.setCreatedAt(dbAccount.getCreatedAt());
dbAccountContact.setCreatedBy(dbAccount.getCreatedBy());
dbAccountContact.setStatus(AccountConstant.STATUS_ACTIVE);
ContactRole contactRole = getContactRole(contact.getType());
if (null == contactRole) {
logger.error("ERROR={}", "contact type is invalid");
throw new AccountException(AccountConstant.INVALID_FIELDS, AccountConstant.INVALID_FIELDS_CODE);
}
dbAccountContact.setContactRole(contactRole);
dbAccountContact.setAccount(dbAccount);
dbAccountContact.setContact(dbcontact);
if (null != dbcontact.getAccountContact() && !dbcontact.getAccountContact().isEmpty()) {
logger.error("dbcontact.getAccountContact() is not null, dbcontact.getAccountContact().size()={}",
dbcontact.getAccountContact().size());
List<AccountContact> accountContactList = dbcontact.getAccountContact();
accountContactList.add(dbAccountContact);
} else {
logger.error("getAccountStatusHistory is null");
List<AccountContact> accountContactList = new ArrayList<>();
accountContactList.add(dbAccountContact);
dbcontact.setAccountContact(accountContactList);
}
if (null != contact && AccountConstant.ADMIN_CONTACT_ROLE.equalsIgnoreCase(contact.getType())) {
if (null != contact.getAdminId()) {
dbService.saveExternalID(String.valueOf(dbcontact.getContactId()), contact.getAdminId(), headers, AccountConstant.STATUS_ACTIVE,
CosConstants.ID_TYPE);
}
}
}
}
} catch (Exception e) {
logger.error("ERROR in setAccountContact to dbAccount contacts={},{}", accountReq.getContacts(), e);
throw e;
}
}
@LogExecutionTime(log = true)
public Account saveDirectCustomerAccount(AccountReq source, Account dbAccount, Map<String, String> headers, String workflowId) {
try {
String statusCode=AccountConstant.INITIAL_STATUS_CODE;
dbAccount = updateAccountStatusHistory(dbAccount, statusCode);
} catch (Exception e) {
logger.error("ERROR in updateAccountStatusHistory={}", e);
throw new InternalServerException(AccountConstant.INTERNAL_SERVER_ERROR,
AccountConstant.INTERNAL_SERVER_ERROR_CODE);
}
return dbAccount;
}
@LogExecutionTime(log = true)
private Account updateAccountStatusHistory(Account dbAccount, String statusCode) {
logger.info("updateAccountStatusHistory *****START");
AccountStatusHistory accountStatusHistory = new AccountStatusHistory();
accountStatusHistory.setAccount(dbAccount);
accountStatusHistory.setCreatedAt(dbAccount.getCreatedAt());
accountStatusHistory.setCreatedBy(dbAccount.getCreatedBy());
try {
updateAccountStatus(dbAccount, statusCode, accountStatusHistory);
} catch (Exception e) {
logger.error("ERROR updateAccountStatus e={}", e);
}
if (null != dbAccount.getAccountStatusHistory()) {
logger.error("getAccountStatusHistory not null");
dbAccount.getAccountStatusHistory().add(accountStatusHistory);
} else {
logger.error("getAccountStatusHistory is null");
List<AccountStatusHistory> accountStatusHistoryList = new ArrayList<>();
accountStatusHistoryList.add(accountStatusHistory);
dbAccount.setAccountStatusHistory(accountStatusHistoryList);
}
Account createdAccount = saveAccount(dbAccount);
logger.debug("createdAccount.getAccountId()={}", createdAccount.getAccountId());
return createdAccount;
}
And the final method is
public Account saveAccount(Account dbAccount) {
try {
Account createdAccount = accountRepository.save(dbAccount);
logger.debug("createdAccount.getAccountId()={}", createdAccount.getAccountId());
return createdAccount;
} catch (Exception e) {
logger.error("ERROR in account creation={}", e);
throw new InternalServerException(AccountConstant.INTERNAL_SERVER_ERROR,
AccountConstant.INTERNAL_SERVER_ERROR_CODE);
}
}
Exception stacktrace
ERROR Throwable={}","stack_trace":"org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [com.adobe.costheta.account.model.db.mysql.Account#1000000080]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.adobe.costheta.account.model.db.mysql.Account#1000000080]\n\tat org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:400)\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:256)\n\tat org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:537)\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534)\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305)\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)\n\tat com.adobe.costheta.service.DbService$$EnhancerBySpringCGLIB$$8d6d52b1.generateDirectCustomerAccountEntity()\n\tat com.adobe.costheta.service.DirectCustomerAccountServiceImpl.createDirectCustomerAccount(DirectCustomerAccountServiceImpl.java:158)\n\tat com.adobe.costheta.service.DirectCustomerAccountServiceImpl$$FastClassBySpringCGLIB$$27f4a473.invoke()\n\tat org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:750)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)\n\tat com.adobe.costheta.service.DirectCustomerAccountServiceImpl$$EnhancerBySpringCGLIB$$13af2ff1.createDirectCustomerAccount()\n\tat com.adobe.costheta.resource.AccountResource.createAccount(AccountResource.java:93)\n\tat com.adobe.costheta.resource.AccountResource$$FastClassBySpringCGLIB$$dd5cfc46.invoke()\n\tat org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:750)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\n\tat org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)\n\tat com.adobe.asr.logging.aspect.ExecutionTimeAspect.logExecutionTime(ExecutionTimeAspect.java:83)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\n\tat org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)\n\tat org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)\n\tat org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)\n\tat com.adobe.costheta.resource.AccountResource$$EnhancerBySpringCGLIB$$d988334a.createAccount()\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:665)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:750)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:97)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.asr.filter.AsrRequestResponseFilter.doFilterInternal(AsrRequestResponseFilter.java:88)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.asr.logging.http.servlet.AsrLoggingFilter.doFilter(AsrLoggingFilter.java:69)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.asr.filter.AsrRequestIdFilter.doFilterInternal(AsrRequestIdFilter.java:99)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.costheta.filters.ApiLoggingFilter.doFilter(ApiLoggingFilter.java:52)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.asr.exception.AsrExceptionFilter.doFilterInternal(AsrExceptionFilter.java:82)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat com.adobe.costheta.filters.MDCFilter.doFilter(MDCFilter.java:44)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)\n\tat org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.base/java.lang.Thread.run(Thread.java:834)\nCaused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.adobe.costheta.account.model.db.mysql.Account#1000000080]\n\tat org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:123)\n\tat org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)\n\tat org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)\n\tat org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1478)\n\tat org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:512)\n\tat org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3310)\n\tat org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2506)\n\tat org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447)\n\tat org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)\n\tat org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)\n\tat org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)\n\tat org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104)\n\tat org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:533)\n\t... 126 common frames omitted\n","requestId":"f5d17eccdfa90271","Content-Type":"application/json"}
dbService.genarateAccountId(accountCode)
generates a unique ID? It might be thatdbcontact.getAccountContact()
(called inside the method you mention) loads some otherAccount
s into the session, one of which happens to have the sameid
as the newly generated one – StroupdbAccount.setAccountId(accountId)
to the end ofmap
(2) callingaccountRepository.save(dbAccount);
right before that line? I think I can see the problem now, if the suggestion helps then that would confirm my hypothesis – Stroupid
after persisting. I think you're confused about how thesave
method works. It only marks the entity as being tracked, the actual flushing to the db happens later, usually upon transaction commit – StroupAccount
implementorg.springframework.data.domain.Persistable
, returningtrue
fromisNew
– StroupPersistable
and assign the id before saving? I know its not the solution, I'm still trying to help you diagnose the symptoms – StroupPersistable
and assigning theid
before thesave
method. Spring Data JPA decides whether to callEntityManager.merge()
orEntityManager.persist()
insidesave
, depending on whether it considers the entity to be new or not. By default, it assumes anything that already has an id cannot be a new entity (and somerge()
is selected). The point is to force thesave
method to callpersist
rather thanmerge
instead. – StroupPersistable
as well – Stroup