I'm having trouble saving a table previously saved in the transaction. We save TablasMaestra first and then save DatosMaestro by setting up the TablasMaestra and Area. The error is in cargaDatosMaestro upload method. The repository we use to save DatosMaestro is JpaRepository. Can anyone help? Thank you. I enclose an implemented code:
package com.soprasteria.mater;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.soprasteria.mater.dto.AreaDTO;
import com.soprasteria.mater.dto.DatoMaestroDTO;
import com.soprasteria.mater.dto.DepartamentoDTO;
import com.soprasteria.mater.dto.TablasMaestraDTO;
import com.soprasteria.mater.entity.DatosMaestro;
import com.soprasteria.mater.serviceInterface.IAreaService;
import com.soprasteria.mater.serviceInterface.IDatoMaestroService;
import com.soprasteria.mater.serviceInterface.IDepartamentoService;
import com.soprasteria.mater.serviceInterface.ITablasMaestraService;
@Service
public class CargaInicialTestService implements ICargaInicialTestService {
protected final Log logger = LogFactory.getLog(getClass());
private static final String TITLE_TABLA_MAESTRA_TEMATICA = "tematica";
private static final String TITLE_TABLA_MAESTRA_TIPOPRODUCCION = "tipoProduccion";
private static final String TITLE_TABLA_MAESTRA_TIPOACTIVIDAD= "tipoActividad";
@Autowired
private IDatoMaestroService datoMaestroService;
@Autowired
private ITablasMaestraService tablasMaestraService;
@Autowired
private IAreaService areaService;
@Autowired
private IDepartamentoService departamentoService;
@PersistenceContext
private EntityManager entityManager;
public void cargaInicialBDD() {
insertTablaArea();
insertDepartamento();
cargaDatosMaestro(TITLE_TABLA_MAESTRA_TEMATICA);
cargaDatosMaestro(TITLE_TABLA_MAESTRA_TIPOPRODUCCION);
cargaDatosMaestro(TITLE_TABLA_MAESTRA_TIPOACTIVIDAD);
}
private void insertTablaArea() {
AreaDTO area = new AreaDTO();
area.setDescripcionCastellano("areaTest");
area.setDescripcionCatalan("areaTest");
area.setFechaCreacion(new Date());
areaService.saveArea(area);
}
private void insertDepartamento() {
List<AreaDTO> areas = areaService.getAreas();
if(areas!=null && !areas.isEmpty()) {
DepartamentoDTO departamento = new DepartamentoDTO();
departamento.setDescripcionCastellano("departamentoTest");
departamento.setDescripcionCatalan("departamentoTest");
departamento.setFechaCreacion(new Date());
departamento.setArea(areas.get(0));
departamentoService.saveDepartamento(departamento);
}
}
private void cargaDatosMaestro(String tabla) {
TablasMaestraDTO tablaMaestra = new TablasMaestraDTO();
tablaMaestra.setDescripcionCastellano(tabla);
tablaMaestra.setDescripcionCatalan(tabla);
tablaMaestra=insertTablaMaestra(tablaMaestra);
List<AreaDTO> areas = areaService.getAreas();
List<TablasMaestraDTO> tablasMaestras = tablasMaestraService.getTablasMaestrasOrderedByCreation();
for(int i=0;i<4;i++) {
DatoMaestroDTO datoMaestro = new DatoMaestroDTO();
datoMaestro.setDescripcionCastellano(tabla+i);
datoMaestro.setDescripcionCatalan(tabla+ i +" cat");
datoMaestro.setTablasMaestra(tablasMaestras.get(0));
datoMaestro.setArea(areas.get(0));
datoMaestroService.saveDatoMaestro(datoMaestro);
}
}
private TablasMaestraDTO insertTablaMaestra(TablasMaestraDTO tablaMaestra) {
tablaMaestra.setFechaCreacion(new Date());
tablaMaestra=tablasMaestraService.saveTablasMaestra(tablaMaestra);
return tablaMaestra;
}
}
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void saveDatoMaestro(DatoMaestroDTO dto) {
DatosMaestro entity = new ModelMapper().map(dto, DatosMaestro.class);
// TablasMaestra tablaTemp = null;
// if(entity.getTablasMaestra()!=null) {
// tablaTemp=entity.getTablasMaestra();
// entity.setTablasMaestra(null);
// }
entity=datomaestroRepository.saveAndFlush(entity);
// entity.setTablasMaestra(tablaTemp);
}
package com.soprasteria.mater.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.soprasteria.mater.entity.DatosMaestro;
import com.soprasteria.mater.entity.DatosMaestroPK;
public interface DatoMaestroRepository extends JpaRepository<DatosMaestro, Integer>{
@Query(value = "select count(*) from DatosMaestro datosMaestro where datosMaestro.id = ?1")
long subtaskNumber(int datosMaestroId);
@Query(value = "select max(datosMaestro.id) from DatosMaestro datosMaestro")
long lastDatosMaestroId();
@Query(value="select datosMaestro from DatosMaestro datosMaestro order by id desc")
public List<DatosMaestro> findAllByOrderByCreatedOnDesc();
@Query(value="select a from DatosMaestro a where a.tablasMaestra.descripcionCastellano = 'tematica' ")
public List<DatosMaestro> findAllAgrupaciones();
@Query(value="select a from DatosMaestro a where a.tablasMaestra.descripcionCastellano = 'tipoProduccion' ")
public List<DatosMaestro> getTipoProducciones();
@Query(value="select a from DatosMaestro a where a.tablasMaestra.descripcionCastellano = 'tipoActividad' ")
public List<DatosMaestro> getTipoActividades();
}
package com.soprasteria.mater.entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
/**
* The persistent class for the datos_maestros database table.
*
*/
@Entity
@Table(name = "datos_maestros")
@NamedQuery(name = "DatosMaestro.findAll", query = "SELECT d FROM DatosMaestro d")
public class DatosMaestro implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int id;
@Column(name = "descripcion_castellano", length = 255)
private String descripcionCastellano;
@Column(name = "descripcion_catalan", length = 255)
private String descripcionCatalan;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "fecha_baja")
private Date fechaBaja;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "fecha_creacion")
private Date fechaCreacion;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "fecha_modificacion")
private Date fechaModificacion;
private int orden;
@Column(name = "usuario_baja", length = 45)
private String usuarioBaja;
@Column(name = "usuario_creacion", length = 45)
private String usuarioCreacion;
@Column(name = "usuario_modificacion", length = 45)
private String usuarioModificacion;
// bi-directional many-to-one association to Area
@ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "area_id_area", nullable = false)
private Area area;
// bi-directional many-to-one association to TablasMaestra
@ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "tablas_maestras_id", nullable = false)
private TablasMaestra tablasMaestra;
public DatosMaestro() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescripcionCastellano() {
return this.descripcionCastellano;
}
public void setDescripcionCastellano(String descripcionCastellano) {
this.descripcionCastellano = descripcionCastellano;
}
public String getDescripcionCatalan() {
return this.descripcionCatalan;
}
public void setDescripcionCatalan(String descripcionCatalan) {
this.descripcionCatalan = descripcionCatalan;
}
public Date getFechaBaja() {
return this.fechaBaja;
}
public void setFechaBaja(Date fechaBaja) {
this.fechaBaja = fechaBaja;
}
public Date getFechaCreacion() {
return this.fechaCreacion;
}
public void setFechaCreacion(Date fechaCreacion) {
this.fechaCreacion = fechaCreacion;
}
public Date getFechaModificacion() {
return this.fechaModificacion;
}
public void setFechaModificacion(Date fechaModificacion) {
this.fechaModificacion = fechaModificacion;
}
public int getOrden() {
return this.orden;
}
public void setOrden(int orden) {
this.orden = orden;
}
public String getUsuarioBaja() {
return this.usuarioBaja;
}
public void setUsuarioBaja(String usuarioBaja) {
this.usuarioBaja = usuarioBaja;
}
public String getUsuarioCreacion() {
return this.usuarioCreacion;
}
public void setUsuarioCreacion(String usuarioCreacion) {
this.usuarioCreacion = usuarioCreacion;
}
public String getUsuarioModificacion() {
return this.usuarioModificacion;
}
public void setUsuarioModificacion(String usuarioModificacion) {
this.usuarioModificacion = usuarioModificacion;
}
public Area getArea() {
return this.area;
}
public void setArea(Area area) {
this.area = area;
}
public TablasMaestra getTablasMaestra() {
return this.tablasMaestra;
}
public void setTablasMaestra(TablasMaestra tablasMaestra) {
this.tablasMaestra = tablasMaestra;
}
}
package com.soprasteria.mater.entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
/**
* The persistent class for the tablas_maestras database table.
*
*/
@Entity
@Table(name="tablas_maestras")
@NamedQuery(name="TablasMaestra.findAll", query="SELECT t FROM TablasMaestra t")
public class TablasMaestra implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;
@Column(name="descripcion_castellano", length=255)
private String descripcionCastellano;
@Column(name="descripcion_catalan", length=255)
private String descripcionCatalan;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="fecha_baja")
private Date fechaBaja;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="fecha_creacion")
private Date fechaCreacion;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="fecha_modificacion")
private Date fechaModificacion;
@Column(name="usuario_baja", length=45)
private String usuarioBaja;
@Column(name="usuario_creacion", length=45)
private String usuarioCreacion;
@Column(name="usuario_modificacion", length=45)
private String usuarioModificacion;
//bi-directional many-to-one association to DatosMaestro
@OneToMany(mappedBy="tablasMaestra")
private List<DatosMaestro> datosMaestros;
public TablasMaestra() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getDescripcionCastellano() {
return this.descripcionCastellano;
}
public void setDescripcionCastellano(String descripcionCastellano) {
this.descripcionCastellano = descripcionCastellano;
}
public String getDescripcionCatalan() {
return this.descripcionCatalan;
}
public void setDescripcionCatalan(String descripcionCatalan) {
this.descripcionCatalan = descripcionCatalan;
}
public Date getFechaBaja() {
return this.fechaBaja;
}
public void setFechaBaja(Date fechaBaja) {
this.fechaBaja = fechaBaja;
}
public Date getFechaCreacion() {
return this.fechaCreacion;
}
public void setFechaCreacion(Date fechaCreacion) {
this.fechaCreacion = fechaCreacion;
}
public Date getFechaModificacion() {
return this.fechaModificacion;
}
public void setFechaModificacion(Date fechaModificacion) {
this.fechaModificacion = fechaModificacion;
}
public String getUsuarioBaja() {
return this.usuarioBaja;
}
public void setUsuarioBaja(String usuarioBaja) {
this.usuarioBaja = usuarioBaja;
}
public String getUsuarioCreacion() {
return this.usuarioCreacion;
}
public void setUsuarioCreacion(String usuarioCreacion) {
this.usuarioCreacion = usuarioCreacion;
}
public String getUsuarioModificacion() {
return this.usuarioModificacion;
}
public void setUsuarioModificacion(String usuarioModificacion) {
this.usuarioModificacion = usuarioModificacion;
}
public List<DatosMaestro> getDatosMaestros() {
return this.datosMaestros;
}
public void setDatosMaestros(List<DatosMaestro> datosMaestros) {
this.datosMaestros = datosMaestros;
}
public DatosMaestro addDatosMaestro(DatosMaestro datosMaestro) {
getDatosMaestros().add(datosMaestro);
datosMaestro.setTablasMaestra(this);
return datosMaestro;
}
public DatosMaestro removeDatosMaestro(DatosMaestro datosMaestro) {
getDatosMaestros().remove(datosMaestro);
datosMaestro.setTablasMaestra(null);
return datosMaestro;
}
}
The exception he's throwing full is:
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.soprasteria.mater.entity.TablasMaestra; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.soprasteria.mater.entity.TablasMaestra
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:280)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy121.saveAndFlush(Unknown Source)
at com.soprasteria.mater.service.DatoMaestroService.saveDatoMaestro(DatoMaestroService.java:63)
at com.soprasteria.mater.service.DatoMaestroService$$FastClassBySpringCGLIB$$60d4a08a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.soprasteria.mater.service.DatoMaestroService$$EnhancerBySpringCGLIB$$d3fb00f1.saveDatoMaestro(<generated>)
at com.soprasteria.mater.CargaInicialTestService.cargaDatosMaestro(CargaInicialTestService.java:99)
at com.soprasteria.mater.CargaInicialTestService.cargaInicialBDD(CargaInicialTestService.java:55)
at com.soprasteria.mater.MaterApplicationTests.cargaInicial(MaterApplicationTests.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.soprasteria.mater.entity.TablasMaestra
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:806)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:773)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:467)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:392)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:414)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:252)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:782)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:767)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:304)
at com.sun.proxy.$Proxy111.persist(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:490)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:504)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 53 more
DatosMaestro is new entity and is saved is entityManager.persist()
method. Persist method is cascaded to TablasMaestra and fails because TablasMaestra is existing entity. Existing entities have to be saved using entityManager.merge()
.
Spring repository save method implementation:
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
} }
One solution is don't save TablasMaestra before saving DatosMaestro . It will be saved together with DatosMaestro.
Or use @Transactional on cargaDatosMaestro()
method