This is my main 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 able to connect and execute a query in my database ...
I want to create the same function, but more general ... The previous one was specific to only one table ( User), the new one should be able to accept the Stringtype of the class in which the query will be executed as an input parameter for the query. This will allow me to use only one line to execute the request.
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 do you find ClassOfTableI don’t know how to "generalize" the code ...
I hope it was clear ...
PS ClassOfTableshould be a cool representation of the table in the database ( Hibernate).
Thank.