views:

936

answers:

2

Hi,

I'm looking for a Generic DAO implementation in Hibernate that includes parent/child relationship management (adding, removing, getting children, setting parents, etc).

Actually the most used generic DAO on the web is the one I found on jboss.org.

And also, i was looking for some DAO/DTO sample implementations and design patterns.

Do you know some good resources out there?

A: 

Parent/Child relationships are a special kind of one-to-many relationships, and they do not require a special DAO to interact with. You simply write code like:

Parent p = new Parent();
Child c1 = new Child();
Child c2 = new Child();
// populate c1 and c2
p.addChild(c1);
p.addChild(c2);
childDao.save(c1);
childDao.save(c2);
parentDao.save(p);

There's a section of the Hibernate doc that actually shows an example parent/child implementation: Chapter 21. Example: Parent/Child

If you prefer to use annotations and/or Hibernate/JPA, have a look at: Taking JPA for a Test Drive

Bytecode Ninja
+1  A: 

I'm looking for a Generic DAO implementation in Hibernate that includes parent/child relationship management (adding, removing, getting children, setting parents, etc).

I would keep the parent/child links management at the entity level (not all entities have parent/childs) but I would create link management methods on them to set both sides when working with bi-directional links as described in 1.2.6. Working bi-directional links.

Actually the most used generic DAO on the web is the one I found on jboss.org.

There are several projects with samples on Google code. I'd suggest to check:

  • generic-dao - JPA Data Access Object Toolkit
  • daofusion - Java based DAO pattern implementation using JPA / Hibernate.
  • hibernate-generic-dao - Generic DAO implementation: extendable, detailed search, remote service interface
Pascal Thivent
Adding the parent/child management at entity level means to add custom class code to the entity mappings (i'm using xml mappings), so don't you think this would increase the complexity and maintainability of the mappings?
Marco
@Marco In any case, you'll need to declare these associations in the XML mappings (adding methods to handle both sides of the link is just handy, it doesn't change anything to the mapping).
Pascal Thivent