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 6866427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:10:56+00:00 2026-05-27T03:10:56+00:00

Originally based on this thread: Spring IoC and Generic Interface Type and this one

  • 0

Originally based on this thread:

Spring IoC and Generic Interface Type

and this one

Write Less DAOs with Spring Hibernate using Annotations

I’m wondering how to approach implementation of the former’s idea. Lets say I’ve got

@Repository
@Transactional
public class GenericDaoImpl<T> implements GenericDao<T> {

    @Autowired
    private SessionFactory factory;
    private Class<T> type;

    public void persist(T entity){
        factory.getCurrentSession().persist(entity);
    }

    @SuppressWarnings("unchecked")  
    public T merge(T entity){
        return (T) factory.getCurrentSession().merge(entity);
    }

    public void saveOrUpdate(T entity){
        factory.getCurrentSession().merge(entity);
    }

    public void delete(T entity){
        factory.getCurrentSession().delete(entity);
    }

    @SuppressWarnings("unchecked")      
    public T findById(long id){
        return (T) factory.getCurrentSession().get(type, id);
    }

 }

and I am O.K. with having marker interfaces:

 public interface CarDao extends GenericDao<Car> {}
 public interface LeaseDao extends GenericDao<Lease> {}

But I want to funnel the implementation details through 1 GenericDaoImpl (something like above’s GenericDaoImpl), to avoid writing duplicate Impl(s) for simple CRUD. (I would write custom Impl(s) for Entities which require more advanced DAO functionality.)

So then eventually in my controller I can do:

CarDao carDao = appContext.getBean("carDao");
LeaseDao leaseDao = appContext.getBean("leaseDao");    

Is this possible? What would I need to do to achieve this?

  • 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-27T03:10:57+00:00Added an answer on May 27, 2026 at 3:10 am

    interfaces can not extend classes so marker interfaces can not be used in this case. you can have classes though. in its current form, I dont think you’ll be able to create beans of GenericDAOImpl so you would need to create those of specific classes that extend GenericDAOImpl. That said, you can definitely pull out sessionfactory into a separate class as a static field, have it wired and use the static reference in the DAO. Then, you wont need to wire the entire DAO, just create instances as new GenericDAOImpl() (or via a factory) and it will work. Obviously, for specific operations, you can have implementations that extend GenericDAOImpl.

    HTH!

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

Sidebar

Related Questions

I'm opening a new thread based on this how to store an array in
Based on info in this thread: Implement own state - INACTIVE_STATE? I have built
I have a UDP based application that is implemented using a thread pool. Messages
Originally posted on Server Fault , where it was suggested this question might better
I originally asked this question on RefactorMyCode , but got no responses there... Basically
Background: This is a follow-up question to this thread about handling EINTR for system
All, I originally had my setup like this and everything worked as expected. WEB-INF
NOTE: I have updated this since originally asking the question to reflect some of
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
Based on my previous thread : RVM installed by Ruby not working? where 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.