-
FEATURED COMPONENTS
First time here? Check out the FAQ!
public String getName(long kodePN) { Transaction tx; HibernateTemplate hib = super.getHibernateTemplate(); SessionFactory sessionFactory = hib.getSessionFactory(); Session ses = sessionFactory.openSession(); tx = ses.beginTransaction();
String a = null;
Query qq = ses.createQuery("SELECT nama_karyawan FROM Karyawan WHERE kode_pn=:p");
Query qq2 = ses.createQuery("SELECT nik_karyawan FROM Karyawan WHERE kode_pn=:p");
qq.setLong("p", kodePN);
qq2.setLong("p", kodePN);
try {
a = qq.list().get(0).toString() + "_" + qq2.list().get(0).toString();
tx.commit();
} catch (HibernateException e) {
tx.rollback();
} finally {
ses.close();
}
return a;
}
As it's not ZK but hibernate this is the wrong forum.
The code works very easy, you create 2 query's and execute them to your DB.
Then you ask twice the first result from the list and concatenate this into a String.
The tx.commit()
and tx.rollback()
are not needed.
Commit will make the database commit. The changes to persistent object will be written to database.
Rollback will male the database rollback from the commit if the commit wasn't succesfull.
Because you do a select operation you will not make any change to the database and that's why you don't need it.
I strongly suggest improvement of the query, cause you don't need to create 2 query's. You can refactor the code so that you only use 1 query what fetching the 2 info's you need. (the table and WHERE clause is the same)
Greetz chill.
Asked: 2015-01-06 04:12:05 +0800
Seen: 22 times
Last updated: Jan 06 '15