0

how this code works?

asked 2015-01-06 04:12:05 +0800

jwasky gravatar image jwasky
11 3

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;
}
delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-01-06 12:56:49 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

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.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-01-06 04:12:05 +0800

Seen: 22 times

Last updated: Jan 06 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More