views:

420

answers:

4

I'm building an application using a JPA object model, and creating several Data Access Objects to interact with that model. I'm finding that I'm repeating a lot of code (essentially CRUD) between different Data Access Objects. The only thing that differs are the targeted JPA classes.

I was wondering if anybody knows of an abstraction library that could be used with any JPA model and would produce the necessary DAOs (or CRUD operations). Supporting of course all the One-to-one, One-to-many, Many-to-one and Many-to-many relations.

Thanks

PS - Do you know of such a library even if it is not for JPA models?

+2  A: 

You could use generics and code your DAO's to interfaces. The Spring Documentation explains this.

zmf
+2  A: 

I suggest you take a look at this article: http://www.ibm.com/developerworks/java/library/j-genericdao.html.

It explains a pattern for implementing an generic DAO with Hibernate and Spring AOP, but could be easily adapted to JPA named queries (and AspectJ-based AOP, if you don't want to use Spring).

Being based on the Java Reflection API, the concept is very extensible. I've for example seen implementations using annotated method arguments as named parameters.

springify
A: 

DAO's with JPA or Hibernate make no sense. A DAO exists to hide other code from the complexity of the database. Which is what JPA and Hibernate do, quite well. So, why add a DAO to a DAO layer?

Jim Barrows
A: 

You could have a look on http//www.adichatz.org if you want to quickly develop your application using JPA data model targeted to an Eclipse RCP application. At this moment it works only with Jboss or in JSE mode.

artouste