Olá Senhores,
Recentemente adotei a JPA para abstrair os meus DAO’s. E o primeiro problema que estou tendo, é ao excluir um Entity,
sempre recebo a exception : org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance x.x.x.
Já tentei resgatar o Entity pela ID e depois excluir, já tentei dar um merge… e simplesmente nao exlcui!.
Estou usando um DaoGenerico e tambem a JPADaoSupport do Spring, segue detalhes do meu código:
Pais
@Entity
@Table(name = "SAVIOR_PAIS")
public class Pais
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_PAIS", unique = true, nullable = false)
private int idPais;
@Column(name = "NM_PAIS", nullable = false, length = 50)
private String nmPais;
@Column(name = "SG_PAIS", nullable = false, length = 3)
private String sgPais;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pais")
private Set<Estado> estados;
....
DAOGenerico
public abstract class AbstractGenericJpaDao<DomainObject> extends JpaDaoSupport implements IGenericDao<DomainObject>
{
private Class<DomainObject> clazz;
@SuppressWarnings("unchecked")
public AbstractGenericJpaDao()
{
this.clazz = (Class<DomainObject>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public AbstractGenericJpaDao(Class<DomainObject> clazz)
{
this.clazz = clazz;
}
/**
* Remove uma lista de DomainObject (Entities)
*
* @param List<DomainObject>
*/
public void remove(List<DomainObject> listObj)
{
log.info("Removendo lista de objetos" + listObj);
for (DomainObject obj : listObj)
{
log.debug("Removendo o objeto "+obj.getClass().getName());
super.getJpaTemplate().remove(obj);
}
}
/**
* Remove um DomainObject (Entity)
*
* @param DomainObject
*/
public void remove(DomainObject obj)
{
log.info("Removendo o objeto "+obj.getClass().getName());
super.getJpaTemplate().remove(obj);
}
....
ImplementacaoDao
@Repository(value="paisDao")
public class PaisDaoImpl extends AbstractGenericJpaDao<Pais> implements PaisRepository
{
}
TesteUnitario
public class PaisServiceImplTests extends AbstractTestConfigurable
{
@Autowired
private PaisService paisService;
private Pais pais;
@Test
public void testRemove()
{
this.pais = new Pais();
this.pais.setIdPais(2);
this.pais = this.paisService.findById(pais);
this.paisService.remove(pais);
}
...
}
StackTrace
28-04-2008 18:36:45 [ERROR] com.apolloti.Savior.infrastructure.hibernate.dao.endereco.PaisDaoImpl - Ocorreu um erro desconhecido.
org.springframework.dao.InvalidDataAccessApiUsageException: Removing a detached instance com.apolloti.Savior.domain.entity.endereco.Pais#2; nested exception is java.lang.IllegalArgumentException: Removing a detached instance com.apolloti.Savior.domain.entity.endereco.Pais#2
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:269)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:97)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:189)
at org.springframework.orm.jpa.JpaTemplate.remove(JpaTemplate.java:284)
at com.apolloti.Savior.infrastructure.dao.AbstractGenericJpaDao.remove(AbstractGenericJpaDao.java:81)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:54)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy40.remove(Unknown Source)
at com.apolloti.Savior.domain.service.endereco.PaisServiceImpl.remove(PaisServiceImpl.java:45)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy41.remove(Unknown Source)
at com.apolloti.Savior.test.service.PaisServiceImplTests.testRemove(PaisServiceImplTests.java:54)
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.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:163)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:142)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:26)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.IllegalArgumentException: Removing a detached instance com.apolloti.Savior.domain.entity.endereco.Pais#2
at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:45)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:86)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:253)
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.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:357)
at $Proxy55.remove(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$7.doInJpa(JpaTemplate.java:286)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:184)
... 48 more
...
Bom… se alguem tiver alguma idéia, é bem vinda!
Abraços e obrigado pela atenção.
\o/