Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:20:16+00:00 2026-05-29T06:20:16+00:00

With the generic dao interface: public interface GenericDAO<T extends DataObject, ID extends Serializable> {

  • 0

With the generic dao interface:

public interface GenericDAO<T extends DataObject, ID extends Serializable> {
      T save (T object);
      T load (ID id);
      void remove (ID id);
      Class<T> getPersistedType();
}

You implement GenericDaoImpl with your favorite ORM, like Hibernate to handle loading and persisting generic objects. You end up with UserDao, StoreDao, TransactionDao, PetsDao, and 50 other Dao that each have different logic on how the object is to be saved, loaded, and removed. You may also have many instances of the GenericDao, each for a class type that only requires generic handling.

I am looking to implement an service that will handle these operations for any DataObject and will choose the right Dao to do it. If there is an Dao implementation for this object, use it, otherwise use the GenericDao.

One way to do it is to have a DaoFactory if/else statement to check the object type, and return the corrosponding Dao. However, this is will require you to make modification to the factory each time a new Dao is added and not very elegant.

What’s a better way to implement this? To ask it another way,
how do you find the Dao that’s associated with particular class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:20:16+00:00Added an answer on May 29, 2026 at 6:20 am

    I once wrote a GenericHibernateDAOImpl; an instance of which could be used to perform read/ write or delete operation on types of any entity. I found the following links useful:

    https://community.jboss.org/wiki/GenericDataAccessObjects?_sscc=t

    http://www.ibm.com/developerworks/java/library/j-genericdao/index.html

        public class GenericHibernateDAOImpl <T, ID extends Serializable> implements GenericDAO<T extends DataObject, ID extends Serializable> {
            ...
    
            GenericHibernateDAOImpl(Class<? extends T> persistentClass, ISessionManager manager,             IPersister persister, ILogger l) {
                Validate.noNullElements(Arrays.asList(manager, persister, l));
                this.pClass = persistentClass;
                this.sessionManager = manager;
                this.persistenceDelegate = persister;
                this.logger = l;
    
            }
            /**
             * {@inheritDoc}
             */
            @Override
            public final T save(final T entity) { 
                persistenceDelegate.save(entity);
                return entity;
            }
    
            public List<T> findFromCriterions(final Criterion... pCriterions) {
            final Criteria crit = sessionManager.getSession().createCriteria(pClass);
            for (Criterion criterion : pCriterions) {
                crit.add(criterion);
            }
            return crit.list();
        }
    
        }
    
    public final class SimplePersister implements IPersister {
    
        private final ISessionManager sessionManager;
    
        public SimplePersister (final ISessionManager manager) {
            sessionManager = manager;
        }
    
        @Override
        public <T> void save(final T entity) {
            sessionManager.getSession().save(entity);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public <T> void delete(final T entity) {
            sessionManager.getSession().delete(entity);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public <T> void update(final T entity) {
            sessionManager.getSession().saveOrUpdate(entity);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the generic dao pattern, I define the generic interface: public interface GenericDao<T extends
I have class: public class GenericDAO<T, ID extends Serializable> { private final EntityManager em;
I have a generic interface: public interface DAO<T> { public T get(long id); public
My dao class com.myapp.dao.hibernate.XyzDaoImpl is annotated with @Repository @Repository public class UserDaoImpl extends GenericDaoJpa
I found this code for a generic DAO interface while browsing around: public interface
I'm refatoring a home-grown DAO container, hoping to make the class generic. It internally
I have made a generic Observer interface and an Observable class, but can't compile
If I have the following class: public class ObjectDAOMongoDBImpl<T> extends GenericDAOMongoDBImpl<T, ObjectId> implements ObjectDAO<T>
I've noticed that having a data access object (DAO) interface and implementation is really
i thinking of doing this architecture genericdao +interface ---> servicelayer+interface---> view layer my dao

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.