Reputation: 7106
I have a design question about the use of Hibernate annotations and DAO pattern. The DTO are supposed to represent entities of the data model. The DAOs are interfaces that define operations on these DTOs. The DAOs implementations are classes that implement the DAO interfaces, and implement the operations defined by these interfaces (for example using Hibernate/MySQL). My question is : How can I use Hibernate annotations in this case? If I annotate directly the DTO, I couple my DTO with the Hibernate framework which is a bad practice I think.
Maybe it's a simple question but the problem is interesting.
Thanks
Upvotes: 2
Views: 3718
Reputation: 1838
From Wikipedia:
In a traditional EJB (Enterprise JavaBeans) architecture, DTOs serve dual purposes: first, they work around the problem that entity beans pre-ejb 3.0 are not serializable; second, they implicitly define an assembly phase where all data to be used by the view are fetched and marshalled into the DTOs before returning control to the presentation tier.[citation needed]; a third reason of using DTOs could be that certain layers of the application should not be able to access the underlying data access objects, and hence change the data.
Thus DTO are the objects which are created from entities and are used by the presentation layer (JSP, for example, should access DTO-s and not the entities directly). So, you should annotate not the DTO-s but your Entity classes, and then provide the code to translate Entities to DTO-s.
Upvotes: 0
Reputation: 18861
When you are using annotations from javax.persistence package you are NOT coupling your code with Hibernate (you would however when using org.hibernate annotations, because they rely on 3rd party libraries).
Note that annotations are just meta information not affecting your design (you don't force classes to implement methods like with interfaces), you just annotate them with additional information for certain purposes.
Client of annotated classes isn't forced to couple his code with additional dependencies as long as annotations belong to standardized java api (javax.persistence in this case).
Upvotes: 1
Reputation: 10332
If I understand you correctly, DTOs are object-relation mapping between Hibernate and your database tables, if that is the case I believe you better have your DTO objects annotated.
Upvotes: 0
Reputation: 691685
It's a matter or supposed "independance" vs. ease of use. Either you choose to use Hibernate or another JPA engine, and you indeed need to have annotations on your entities and have the hibernate jars in your classpath to use them. Or you choose to be completely independant of JPA/Hibernate, but you have to implement the whole persistence yourself.
My opinion is that sacrificing the ease of use and productivity gains offered by JPA just to avoid some jars in the classpath is a bad choice. But YMMV.
Upvotes: 1