aspose file tools*
The moose likes Object Relational Mapping and the fly likes Generic DAO Pattern - Subclass Entities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Generic DAO Pattern - Subclass Entities" Watch "Generic DAO Pattern - Subclass Entities" New topic
Author

Generic DAO Pattern - Subclass Entities

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Guys,

I use the Generic DAO pattern to model my DAO layer. I have a domain object (application level) termed as Content. A Content may be an Article, Question, Answer or any other object which is persistable in the database. All these persistable entities extent the Content which is just a marker object. Now in my persistence layer, I would like to have a method that returns a Question entity based on the id passed to it. The DAO (for the Content domain) looks like:



But here when I call the getRandomQuestion by passing the technologyId, I should get a Question object in return. I'm thinking of using the JDK dynamic proxy in this scenario. Any suggestions?




SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

What I want to acheive is that, get a ContentDAOImpl which has Question as the entity type at run time instead of having Content as defined above. What I'm literrally doing here is clubbing Content related entities to a single DAO class.

Any ideas?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Speaking in terms of design patterns, I'm working out on an alternative to the Anemic DAO pattern. I really do not want to have one DAO for each entity that I have. But rather one DAO per Domain. One Domain in my application consists of many Entities and my application has many such Domains. I want to club this idea together with the Generic DAO pattern.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Joe Harry wrote:Guys,

I use the Generic DAO pattern to model my DAO layer. I have a domain object (application level) termed as Content. A Content may be an Article, Question, Answer or any other object which is persistable in the database. All these persistable entities extent the Content which is just a marker object. Now in my persistence layer, I would like to have a method that returns a Question entity based on the id passed to it. The DAO (for the Content domain) looks like:



But here when I call the getRandomQuestion by passing the technologyId, I should get a Question object in return. I'm thinking of using the JDK dynamic proxy in this scenario. Any suggestions?



I want to make this part dynamic. I should be able to pass Question, or rather anything that extends Content.

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Any suggestions?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9506
    
    2

Here is my complete source:

The Generic DAO interface (has methods to define the basic CRUD operations):



The abstract class that implements the above interface: Each DAO will then extend this abstract class by specifying the actual Entity type on which it intends to operate.



The DAO implementation:



I want to experiment the getRandomQuestion method which actually should return a Question which extends from Content. I do not want to have new DAO's for Question. I also have some other Entity which IS-A Content (for example., Answer, SearchableContent and so on) which are logically grouped under Content. I really do not want to clog my persistence layer with a bunch of DAO's but rather one chic DAO (on the Content domain) which gives me what I need for all Content types and which I can instantiate based on the method that I'm going to call. Hope my question is much clear now for any suggestions from you guys.
Michele Cozart
Greenhorn

Joined: Jan 31, 2012
Posts: 1
Did you ever get an answer on this? Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic DAO Pattern - Subclass Entities