4

I have generic method for add, update and delete entities. But I also want to use generics for retrieving data.

Here is my methods to get data:

public static List<ClassA> getAllClassAData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassA.class);
    return criteria.list();
}

public static List<ClassB> getAllClassBData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassB.class);
    return criteria.list();
}

public static List<ClassC> getAllClassCData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassC.class);
    return criteria.list();
}

I tried to make something like this:

public static <T> List<T> getAllData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(???.class); //Here is where I'm stuck
    return criteria.list();
}

And I stuck with passing the class as arg for criteria creation:

 Criteria criteria = session.createCriteria(Generic.class);

How can I make JVM know what class I'm trying to pass during criteria creation?

4 Answers 4

7

Your generic method should look more like :

public static <T> List<T> getAllData(Class<T> clazz) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(clazz);
    return criteria.list();
}

and you should invoke it like:

List<ClassA> list = getAllData(ClassA.class);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works for me as I wanted to. Btw, I use static context because I have several methods to communicate with DB and there's no need in DBUtil instance creation. At least I don't see the reason to do it in a small project.
2

A simple step to simulate being dynamic is to pass the class instance on query. It looks ugly, but does the work. Unfortunately there is no way to do sth like T.class

public static <T> List<T> getAllData(Class<T> klass) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(klass);
    return criteria.list();
}

Comments

0

Firstly static and generics dont mix well, so you will have to do away with static.

Next have a look at how this is handled here , https://github.com/sajit/Webgearz/blob/master/src/main/java/com/webgearz/tb/daos/impl/AbstractMongoDao.java

protected Class<N> persistentClass;

        public Class<N> getPersistentClass() {
                return persistentClass;
        }

and related classes. That should help what you are trying to achieve.

Comments

0

Have a look over the generic repository pattern, it is used a lot in c# and although the generics are not the same in Java you should be able to do normal generic runtime hacks to get the underlying type.

http://www.codeproject.com/Articles/526874/Repositorypluspattern-2cplusdoneplusright

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.