User-Managed Transaction and Flush problem using JTA and Arquillian

Asked

Viewed 47 times

0

I’m doing some tests with Junit and, to inject dependencies in it with CDI, I adopted Arquillian. But there’s one problem I haven’t figured out how to solve without using the method flush() of EntityManager.

Basically, I need to remove an object and check that it is not in the object listing, which I do with the following commands:

class1DAO.remove(1); assertFalse(class1DAO.getAll().contains(object1FromClass1));

What the method: remove(Class1 class1);
do DAO do is give a: entityManager.remove(class1);
What would be enough to exclude the object. From what I understood of Hibernate.

Already the method getAll() returns a entityManager.createQuery("from Class1 c", Class1.class).getResultList();

So far so good! But the object deleted a line above, in the test, comes in the method listing getAll() and my test fails. The only way I found to avoid this is to give a entityManager.flush(); just below entityManager.remove(class1);

But I was already using Transaction Manager and Dependency Injection Control, precisely, to avoid having to do it on hand. Is there any way to solve this, without explicitly using the flush()?

  • put your test there !!

  • Class1 class1 = class1DAO.add(new Class1("payment")); assertTrue(classe1DAO.getAll().contains(class1)); class1DAO.remove(1); assertFalse(class1DAO.getAll().contains(class1));

  • The test is basically this one. Add, check, remove and check. But if I don’t give one flush() no remove, the test fails.

  • I removed my answer because I made a mistake with what you’re asking. Without the entityManager.flush() to remove it is a problem, as changes in the database are only written after the transaction is confirmed, the flush causes the changes to be written immediately. So when you try to access a list, the reading gives the error because the transaction has not yet been confirmed.

1 answer

0

Do it like this:

@Test
public void delete()  {


    class1DAO.remove(1);//deleta


    assertNull( class1DAO.find(Class1.class, 1) );// se null ok.

}//
  • I did it and returned null, yes. But, as I said, using the getAll(); with a basic query, it does not return null and, in a more extensive test, I need these guys to be removed and not come up with the getAll();

  • I didn’t understand your class so it helps you a lot.

  • I need to not only verify that this guy was excluded. If so, the find do Entitymanager resolver. .

  • ai when you bring all of the database the deleted data will no longer be among them.

  • But bring it. If I give one find(Class1.class, 1); it does not come, it gives null. But if, then I give a entityManager.createQuery("from Class1 c", Class1.class).getResultList(); he comes along.

  • entityManager.createQuery("from Class1 c", Class1.class). getResultList() hereIt is searching all. Ideal is to search for your default object.

  • But I must fetch them all.

  • why you need to fetch all

  • I need to iterate on them. Know how it is possible to flush through the Usertransaction?

  • I didn’t understand the phrase.

  • I’m not trying , understanding why you’re using flush.

  • Because without flush, remove does not work. I want to know if it is possible to use Usertransaction or a test Entitymanager to flush the DAO. Is there a way to do this? Not with the DAO Entitymanager. I don’t want to touch the DAO, but, yes, give the flush for the test.

  • put your classes without them becomes difficult..

Show 8 more comments

Browser other questions tagged

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