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.