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

  • Home
  • SEARCH
  • 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 8440395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:17:11+00:00 2026-06-10T08:17:11+00:00

I have been trying to fully understand and implement a GenericDAO layer in hibernate.

  • 0

I have been trying to fully understand and implement a GenericDAO layer in hibernate. I am new to the concept and have been doing a bit of reading and studying. I have found numberous examples on example implementations of a GenericDAO Layer, and this is what I have ended up with.

public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {

    private static Logger log = Logger.getLogger(GenericDAOImpl.class.getName());


    private SessionFactory sessionFactory;


    @SuppressWarnings("unchecked")
    public T findById(long id, Class<T> objectClass) {
        log.info("Entered GenericDAOImpl findById(" + id +")");
        T result = (T) getSessionFactory().getCurrentSession().load(objectClass, id);
        if(result != null){
            Hibernate.initialize(result);
            return result;
        }else{ 
            return null;
        }
    }

    public boolean create(T newInstance) {
        log.info("Entered GenericDAOImpl create()");
        if(newInstance == null){
            return false;
        }
        getSessionFactory().getCurrentSession().saveOrUpdate(newInstance);
        return true;        
    }


    public boolean updpate(T updateInstance) {
        log.info("Entered GenericDAOImpl updpate()");
        if(updateInstance == null){
            return false;
        }
        getSessionFactory().getCurrentSession().update(updateInstance); 
        return true;
    }

    public boolean delete(T entity) {
        log.info("Entered GenericDAOImpl delete()");
        if(entity == null){
            return false;
        }
        getSessionFactory().getCurrentSession().delete(entity);
        return true;
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, Class<T> objectClass){
        log.info("Entered GenericDAOImpl findByExample()");
        Criteria searchCriteria = getSessionFactory().getCurrentSession().createCriteria(objectClass);

        searchCriteria.add(Example.create(exampleInstance));

        return (List<T>)searchCriteria.list();          

    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }       
}

It seems as though this should work in theory (may need some tweaking)

My question is can I use a generic service and view layer to “pass through” the layered architecture approach? I do not fully understand hibernate transactions enough to know if it is safe to do this, with its handling of transactions etc…

Maybe something like this for the service layer for example

public class GenericServiceImpl<T, ID extends Serializable> implements GenericService<T, ID>{

    private GenericDAO<T, ID> genericDao;

    @Override
    public T findById(long id, Class<T> objectClass) {
        return this.getGenericDao().findById(id, objectClass);
    }

    @Override
    public boolean create(T newInstance) {
        return this.getGenericDao().create(newInstance);
    }

    @Override
    public boolean updpate(T updateInstance) {
        return this.getGenericDao().updpate(updateInstance);
    }

    @Override
    public boolean delete(T entity) {
        return this.getGenericDao().delete(entity);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List findByExample(T exampleInstance, Class<T> objectClass) {
        return this.getGenericDao().findByExample(exampleInstance, objectClass);
    }


    public GenericDAO<T, ID> getGenericDao() {
        return genericDao;
    }

    public void setGenericDao(GenericDAO<T, ID> genericDao) {
        this.genericDao = genericDao;
    }


}

Then could I go on and do a generic view layer as well?

Please let me know if this approach is acceptable or if there are any concerns with this approach.

Thanks in advance for your thoughts and responses!

  • 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-06-10T08:17:13+00:00Added an answer on June 10, 2026 at 8:17 am

    Your service, as it stands, is simply delegating everything to the underlying DAO. This may be desired sometimes, but typically I put “business logic” in the service layer. Putting logic in the service layer will help keep your controller layer pretty light too.

    A service can use one or more DAOs to accomplish the task it needs. So consider a simple bank system, where I have a AccountDao

    public class AccountDao implements GenericDao<Account, Long> {
      // your save, insert, delete, find, etc
    }
    

    Then in my service, I would put “makePayment” or something

    @Service
    public class AccountService {
    
       @Autowired
       private AccountDao dao;
    
       @Transactional
       public void makePayment(Long fromId, Long toId, double amount) {
          Account from = dao.find(fromId);
          from.withdrawl(amount);
    
          Account to = dao.find(toId);
          to.deposit(amount);
    
          dao.save(from);
          dao.save(to);
       }
    }
    

    Use transactions on your service layer, to give you more control over which operations need to be in the same transaction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I am very new to MVC3 / jQuery combo and have been reading
Have been trying to encrypt an xml file to a string so that I
Have have been trying to make a validator for my xml files. I have
I have been trying to setup git for our web development team unsuccessfully. Some
I have been trying for almost a week now to create an SQLite database
I have been trying to generate report as per age differences of different months
I have been trying to create a ListView which I can sort using drag
I have been trying to align an entire label along with text to the
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I have been trying to serialize a list that contains arrays and lists. I

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.