views:

199

answers:

2

hello good fellas! i use spring and hibernate for my data access layer i'll like to have some guidance about how to construct my unit testing to test if hibernate is effectively insert in the child table(parent hibernate mapping has the cascade all on the set). For what i know i shouldn't mix dao's unit testing.So supposing the i'm testing the Parent DAO methods saveWithChild:

public void testSaveWithChild() {
  Child c1 = new Child("prop1", "prop2", prop3);
  Child c2 = new Child("prop4", "prop4", prop3);
  Parent p = new Parent("prop6","prop7");
  p.addChild(c1);
  p.addChild(c2);
  Session session = MysessionImplementation.getSession();
  Transaction tx = session.begingTransaction();
  ParentDAO.saveWithChild(p);
  tx.commit();

  Session session1 = MysessionImplementation.getSession();
  //now it is right to call child table in here?
   Child c1fromdb = (Child)session1.get(ChildClass.class,c1.getID());
   Child c2fromdb = (Child)session1.get(ChildClass.class,c2.getID());
   //parent asserts goes here 
   //children asserts goes here.
}

I don't know but i don't feel confortable doing this.Isn't there any better way? How will you check those things? thanks for reading. ;)

A: 

You could instead do:

public void testSaveWithChild() {
  Child c1 = new Child("prop1", "prop2", prop3);
  Child c2 = new Child("prop4", "prop4", prop3);
  Parent p = new Parent("prop6","prop7");
  p.addChild(c1);
  p.addChild(c2);
  Session session = MysessionImplementation.getSession();
  Transaction tx = session.begingTransaction();
  ParentDAO.saveWithChild(p);
  tx.commit();

  Session session1 = MysessionImplementation.getSession();
  Parent p2 = session1.get(ParentClass.class,p.getID());
  // children from db should be in p2.getChildren()
}

This way, at least you are not mixing DAOs.

martin
thanks will try it
black sensei
A: 

First of all, you should definitely close the session after you've called tx.commit().

If MysessionImplementation.getSession() returns active session (similar to SessionFactory.getCurrentSession()) then your test is not even going to hit the database as session1 would be the same as session and both children would still be bound to it.

If MysessionImplementation.getSession() returns a new session every time, you're leaking resources.

Secondly, are children in your example TRUE children (is their lifecycle bound to parent)? If that's the case you should not have a ChildDAO at all (and maybe you don't) and you may or may not have a getChildInstance(id) method (whatever's it's called) in your ParentDAO. It's perfectly fine to call such method (or, if you don't have it, use session.load()) in the ParentDAOTest because you're testing ParentDao's functionality.

Finally, keep in mind that just testing that children were inserted is not enough. You also need to test that they were inserted with correct parent (if your parent-child relationship is bidirectional, you can do it via child.getParent() method or whatever it's called in your case). You should also test child deletion if that's supported by your dao.

ChssPly76
very good insight.thanks for the answer. MysessionImplementation return a new session everytime.So how to prevent ressource leak?
black sensei
Close the session. Use try / finally: Session session = MysessionImplementation.getSession(); try { do stuff} finally { if (session!=null) session.close()};
ChssPly76
thanks dude.really appreciate it
black sensei
Also, consider using Hibernate's built-in session management via sessionFactory.getCurrentSession() - details are here: https://www.hibernate.org/42.htmlOr, better yet, take a look at Spring: http://static.springsource.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate Spring's HibernateTemplate and HibernateDaoSupport help with a lot of common DAO stuff
ChssPly76