Doubts with DAO method

Asked

Viewed 165 times

1

I have a question on the issue of a generic DAO, being used in a test project with Vraptor.

This dao, works normal in my requests, more when being used in a unit test the same as the following problem when trying to remove:

@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);
}

It generates the following error when removing the object.

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)

These are the two methods used.

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();
    }
}
  • 1

    Doesn’t answer the question, but take a look here.

1 answer

2

Actually, integration tests should not be running in the database but with some database in memory, but the problem may be because you need to refresh the entityManager for each entity persisted before performing delete, ex:

Update the status of the database instance by replacing the changes made to the organisation, if any.

em.refresh(c1);

In-memory database configuration example.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
    <persistence-unit name="minhaUnidadePersistencia-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>br.com.suaApp.SuaEntidade</class>
        ..... demais entidades
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:meuDataBase-test" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
  • Dilnei, what type of memory bank can I use together with Ibernate? I would like Hibernate to create the bank for me as he creates it in the real database. ?

  • 1

    These days you have to various flavors i like hsqldb, edited the response and added my persistence.xml for you to see, and it can be with xml files, right in the code or let Hibernate create. recalling that persistence is in the test structure ex: src/test/Resources/META-INF

Browser other questions tagged

You are not signed in. Login or sign up in order to post.