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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:15:11+00:00 2026-05-27T16:15:11+00:00

most people should be familiar with making a Generic DAO for Spring + hibernate.

  • 0

most people should be familiar with making a Generic DAO for Spring + hibernate. The reference is from here http://www.ibm.com/developerworks/java/library/j-genericdao/index.html but there is an improvement to it on Single DAO & generic CRUD methods (JPA/Hibernate + Spring)

This improvement is the detection of the type as it is part of the superclass, instead of using the constructor to tell which class it is

public GenericDaoJpaImpl() {
    ParameterizedType genericSuperclass = (ParameterizedType) getClass()
         .getGenericSuperclass();
    this.entityClass = (Class<T>) genericSuperclass
         .getActualTypeArguments()[0];
}

HOWEVER, this cast will fail with Guice. To be injected, the interface and the class need to be binded in a module like this

bind(TestDao.class).to(TestDaoImpl.class);

And by doing so the constructor trick for our GenericDAO won’t work because of the following:

getClass().getGenericSuperclass() = java.lang.Class
getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl$$EnhancerByGuice$$5fe0d6fd

contrary to what a Spring + Hibernate returns

getClass().getGenericSuperclass() = sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
getClass().getName() =  com.gwtplatform.samples.basic.server.dao.TestDaoImpl

I’m just using a super constructor in my extended DAOs now, but still would like to obtain the type instead of providing it, any ideas?

  • 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-27T16:15:11+00:00Added an answer on May 27, 2026 at 4:15 pm

    The “guicy” way is to let Guice inject a TypeLiteral into your code.

    bind(new TypeLiteral<Dao<Foo>>(){}).to(GenericDAO.class);
    

    and then….

    class GenericDao implements Dao<T>
      @Inject
      GenericDao(TypeLiteral<T> type) {
        this.whatever = type;
      }
    }
    

    Guice knows the type of T and is happy to tell you. TypeLiteral has accessors to get the raw underlying type.

    The guice docs have precious little information on this technique, but this blog post is helpful: http://blog.publicobject.com/2008/11/guice-punches-erasure-in-face.html

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

Sidebar

Related Questions

In my memory, most people told me I should design from top to bottom.
Most people have heard of Content Management Systems. However, what basic features should an
I am looking for what most people use as their collection type when making
We are told that we should implement hashCode() for our classes but most people
Most people are asking about migrating from J2ME to Android. I'm not. I'd like
Here is the lay of the land. Like most people I have my domain
Most people talk about progressive enhancement right now as serving browsers with javascript (enhanced
Most people would agree that internationalizing an existing app is more expensive than developing
Do most people use .NET's SqlMembershipProvider, SqlRoleProvider, and SqlProfileProvider when developing a site with
I know that most people have DirectX9-capable cards but is it really necessary to

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.