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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:12:29+00:00 2026-05-12T18:12:29+00:00

In searching for an answer to an interesting situation which I had recently encountered

  • 0

In searching for an answer to an interesting situation which I had recently encountered I came upon the following question: Type safety, Java generics and querying

I have written the following class (cleaned up a bit)

public abstract class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements BaseDao<T> {

    /**
     * Finds and Returns a list of persistent objects by a collection of criterions
     * @param criterions
     * @return list of persistent objects
     * @throws DBException
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Collection<Criterion> criterions) throws DBException {
        try {
            DetachedCriteria criteria = DetachedCriteria.forClass(T.class); // BAD!!!
            for (Criterion criterion : criterions) {
                criteria.add(criterion);
            }

            List<T> result = getHibernateTemplate().findByCriteria(criteria);
            return result;
        }
        catch (Exception e) {
            throw new DBException(T.class + " lookup by " + criterions + " failed", e); // BAD!!!
        }
    }
}

For some (probably good reason) T.class causes a compile time error.

My first question is why?

If I change it to T.getClass() which obviously shouldn’t compile – because no ‘T’ when “expanded” or goes through “erasure” – should have a static method such as that. The eclipse IDE gives the following compilation message:

Cannot make a static reference to the
non-static method getClass() from the
type Object

My second question is why? And what does this error imply actually?

Finally, would solving this in the manner specified in the link above (or rather my interpretation of) be the most optimal way?

public abstract class BaseDaoImpl<T extends Serializable> extends HibernateDaoSupport implements BaseDao<T>, MyGenericHelper<T> {

    /**
     * Finds and Returns a list of persistent objects by a collection of criterions
     * @param criterions
     * @return list of persistent objects
     * @throws DBException
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Collection<MyCriterion> criterions) throws DBException {
        try {
            DetachedCriteria criteria = DetachedCriteria.forClass(getGenericClass()); // BAD!!!
            for (Criterion criterion : criterions) {
                criteria.add(criterion);
            }

            List<T> result = getHibernateTemplate().findByCriteria(criteria);
            return result;
        }
        catch (Exception e) {
            throw new DBException(getGenericClass() + " lookup by " + criterions + " failed", e); // BAD!!!
        }
    }
}

public interface MyGenericHelper<T extends Serializable>  {
    public Class<T> getGenericClass();
}

Thanks!

  • 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-12T18:12:29+00:00Added an answer on May 12, 2026 at 6:12 pm

    The reason why T.class can’t be accessed is because T is erased at compile time, so it doesn’t exist at runtime to get a class.

    The typical hack around this is to make a factory method:

     public static <T extends Serializable> BaseDAOImpl<T> createBaseDAO(Class<T> klass) {
           return new BaseDAOImpl<T>(klass);
     }
    

    And then in the constructor store the klass variable as a field and reference it when you need it.

    You could use your interface (personally I would just go with a protected abstract method) if you want to keep a no-arg constructor.

     protected abstract Class<T> getGenericClass();
    

    EDIT: In the case of the generic abstract superclass, there are a couple of options. One is have the constructor and then have the subclasses just have to call it (by not having a no-arg constructor). Something like this:

      protected BaseDAOImpl(Class<T> klass) {
           //store the parameter in a field.
      }
    

    Then the static method isn’t relevant, as you have to create the subclass. The static factory method is more used when based on the class you can return the right implementation (so you have a factory, not just a strategy).

    For the sake of completeness, I should point out that if the subclasses declare the generic when they extend the abstract class like this:

     public IntegerImpl extends BaseDAOImpl<Integer> {}
    

    then the generic type is preserved in the class. There is a really ugly hack to get at this using the class. I experimented with this, and it worked:

      (Class<?>) ((ParameterizedType)  this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]
    

    But it makes huge assumptions about the inheritance hierarchy, and should not be used in anything serious if it can at all be avoided, but I’m including it for the sake of a complete picture of what is going on.

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

Sidebar

Related Questions

While searching for an answer to this question I found this sample code which
Searching for an answer I found this question , which is an exact opposite
I have been searching for an answer for a couple days now and had
I have spent about half a day searching for an answer to this question
First of all, I did some searching for an answer to this question...but I
I'm trying to implement jqgrid search on MVC, following the interesting answer by @Oleg,
I have been searching for an answer to this question for days and it
(I hope this question is on-topic -- I tried searching for an answer but
I have been searching the answer to this question for a while. It seemed
total n00b question here but I can't seem to find the answer searching around

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.