Estou com uma dúvida na questão de um DAO genérico, sendo usado em um projeto de testes com Vraptor.
Este dao, funciona normal nos meus requests, mais ao ser usado em um teste unitário o mesmo da o seguinte problema ao tentar remover:
@Before
public void init(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("TEST");
EntityManager manager = factory.createEntityManager();
this.dao = new DaoMaster(manager);
}
@Test
public void refusePersistDuplicateDomain(){
Company c1 = new Company("Name", "teste");
Company c2 = new Company("Name", "teste");
Assert.assertTrue(this.dao.persist(c1));
Assert.assertFalse(this.dao.persist(c2));
this.dao.remove(c1.getId(), Company.class);
}
O mesmo gera o seguinte erro ao fazer a remoção do objeto.
java.lang.IllegalArgumentException: attempt to create delete event with null entity
at org.hibernate.event.spi.DeleteEvent.<init>(DeleteEvent.java:48)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:896)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:1214)
at br.com.adapcon.jpro.dao.DaoMaster.remove(DaoMaster.java:96)
at br.com.adapcon.jpro.test.dao.CompanyDaoTest.refusePersistDuplicateDomain(CompanyDaoTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Esses são os dois métodos usados.
public boolean persist(IEntity o){
try{
this.charger();
if (o.getId() != null && o.getId() > 0) {
manager.merge(o);
} else {
manager.persist(o);
}
manager.getTransaction().commit();
return true;
}catch(Exception e){
return false;
}
}
public boolean remove(Long id, Class<?> objectClass){
try{
this.charger();
Object o = findById(id, objectClass);
System.out.println("M: "+id);
manager.remove(o);
manager.getTransaction().commit();
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
private void charger(){
if(!manager.getTransaction().isActive()){
manager.getTransaction().begin();
}
}