4

This is my basic function:

public static void main(String[] a) throws Exception {

    Session sessione = HibernateUtil.getSessionFactory().openSession();
    Query query = sessione.createSQLQuery("select * from User").addEntity(User.class);
    List<User> rows = query.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        User usr = (User) it.next();
        System.out.println(usr.getEmail());
        System.out.println(usr.getName());
        System.out.println(usr.getIdUser());
        System.out.println(usr.getUser());

    }

This function is capable to connect and perform a query on my DB...

I want to create the same function but more general... The previous was specific for only one table (User), the new one must be able to accept as input a String parameter for the query, and the class type where the query will be executed. This will allow me to use only one row in order to perform a query.

It should be something like this:

public static void queryResult(String query, <ClassOfTable>) {

    Session sessione = HibernateUtil.getSessionFactory().openSession();
    Query qy = sessione.createSQLQuery(query).addEntity(<ClassOfTable>.class);
    List<<ClassOfTable>> rows = qy.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        <ClassOfTable> obj = (<ClassOfTable>) it.next();
    }

}

Where you find ClassOfTable I don't know how to "generalize" the code...

I hope to have been clear...

P.S. ClassOfTable should be the class rappresentative of a table in DB (Hibernate).

Thanks.

Bubletan
  • 3,833
  • 6
  • 25
  • 33
rugby82
  • 679
  • 1
  • 9
  • 25

1 Answers1

4
public static <T> void queryResult(String query, Class<? extends T> clazz) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Query q = session.createSQLQuery(query).addEntity(clazz);
    List rows = q.list();
    Iterator it = rows.iterator();
    while (it.hasNext()) {
        T t = (T) it.next();
        // do your work on object t
    }
}

If your intention is to return resultset, use:

public static <T> List<T> queryResult(String query, Class<? extends T> clazz) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Query q = session.createSQLQuery(query).addEntity(clazz);
    List<T> rows = (List<T>) q.list();
    return Collections.unmodifiableList(rows);
}

// now call generic method
List<User> users = queryResult("select * from User", User.class);
users.forEach(usr -> {
    System.out.println(usr.getEmail());
    System.out.println(usr.getName());
    System.out.println(usr.getIdUser());
    System.out.println(usr.getUser());
});
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • 1
    `List rows = q.list()` should be changed to `List rows = q.list()`. Method should return a `List` so that it actually serves some purpose rather than being a mute. – Chetan Kinger Apr 27 '15 at 15:39
  • 1
    @bot hibernate returns resultset of objects. So you'd also need to cast an iterator. That's why I prefer to cast objects themselves. – Alex Salauyou Apr 27 '15 at 15:40
  • So you are saying that it doesn't matter if the method does not return the list back to the outside world? What's the point of populating that `result` if it's going to be collected after the method completes? – Chetan Kinger Apr 27 '15 at 15:42
  • 1
    Great! thanks Sasha...it is that i need......i call the generic methot like below: funcUtil.queryResult("select * from User", User.class).forEach(usr -> { – rugby82 Apr 28 '15 at 10:03