0

Having a look at some JPA code and I see:

public interface Dao<T extends DomainObject>

public interface EventDao extends Dao<Event> - nothing added to Dao<Event>

public abstract class AbstractDaoJPAImpl<T extends DomainObject> extends JpaDaoSupport implements Dao<T> 

public class EventDaoJPAImp extends AbstractDaoJPAImpl<Event> implements EventDao

Why are these 2 interfaces needed? Why not have simply

public abstract class AbstractDao<T extends DomainObject> extends JpaDaoSupport

public class EventDao extends AbstractDaoJPAImpl<Event>

I'm coming from a Ruby on Rails world where things seem simpler. I'm positive this Java approach has many advantages. I can often recognize when an interface should be used, but sometimes I get the feeling Java devs go interface-crazy.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alexandre
  • 5,035
  • 7
  • 29
  • 36
  • 1
    You're mixing JPA and DAO concepts. JPA != DAO. They can be used together, but more than often JPA is seen as replacement of DAO as JPA is backed by a *standard*. Related: http://stackoverflow.com/questions/3818589/java-ee-architecture-are-daos-still-recommended-when-using-an-orm-like-jpa-2 – BalusC Nov 18 '11 at 17:20
  • Find out what JPA itself is first, and repost a real question – DataNucleus Nov 18 '11 at 18:03
  • Actually you need an interface for DAO so that you can easily switch for one implementation to another (for exemple to mock the dao's for unit tests...). You can also do things like switch dao implementations easily to compare load testing results with the different dao sets or anything like that... And you use a generic interface to avoid having to add all the basic crud methods to all the non generic dao interfaces – Sebastien Lorber Nov 23 '11 at 09:45

2 Answers2

2

None of these interface is required by JPA.

The only thing you need to have in JPA is your entity definition, including annotations to map to the database. That's it. The EntityManager that manages the database connection and storage of your entities is already written by JPA and you don't need all those interfaces and classes.

They were probably written by a developer that thought it would dissociate the database layer (and the EntityManager given to you by JPA) and any other layer of the application.

If that's a good or bad thing is another topic...

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
1

Using an interface allows defining the contract. This contract is implemented in the concrete EventDao class.

A business service using such a DAO will typically use the EventDao interface as an injected dependency. This has several benefits:

  • being able to easily inject a mock EventDao implementation in order to unit test the service
  • being able to use a dynamic proxy around the concrete implementation to add
    • declarative transaction management (although it's the service that should demarcate transactions, you could verify in the proxy that a transaction exists, using the MANDATORY propagation)
    • statistics and performance gathering
    • other useful aspects...
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255