hello Guys,
I've been looking into this issue since past one week.
I've got information about hibernate cache types and using different cache types.
I tried using ehcache for my database and decreasing the cache time for one of my entities, but it didn't worked. Even waiting for about 10 times more than the refresh period set in ehcache.xml.
I even tried using StatelessSessions. I've tried many other things also, but none of them seemed to be working.
I suffered for a week without any result.
Yesterday I was surfing the internet about the same and soon I had the idea of using a transaction since hibernate must validate the database before inserting (just come up as a thought, nothing to do with how actually hibernate works).
So I myself invented a new technique, although it'll slow up your application, but for now atleast I can go with it, instead of having a buggy feature of not showing database update by hibernate. Below is what I used:
RESULT
Use Transaction in every select query you make along with all insert, update & delete queries.
So the main idea is to use a Transaction every time one has to query the database, no matter what type of query.
E.g.
Earlier Code :
Session sess = NewHibernateUtil.getSessionFactory().openSession();
SQLQuery q = sess.createSQLQuery("select * from user where iduser=(select iduser from account where idaccount"
+ "=:idaccount)");
q.setInteger("idaccount", idaccount);
q.addEntity(User.class);
User u = (User) q.uniqueResult();
sess.close();
New Code :
Session sess = NewHibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
SQLQuery q = sess.createSQLQuery("select * from user where iduser=(select iduser from account where idaccount"
+ "=:idaccount)");
q.setInteger("idaccount", idaccount);
q.addEntity(User.class);
User u = (User) q.uniqueResult();
tx.commit();
sess.close();
I've been looking into this issue since past one week.
I've got information about hibernate cache types and using different cache types.
I tried using ehcache for my database and decreasing the cache time for one of my entities, but it didn't worked. Even waiting for about 10 times more than the refresh period set in ehcache.xml.
I even tried using StatelessSessions. I've tried many other things also, but none of them seemed to be working.
I suffered for a week without any result.
Yesterday I was surfing the internet about the same and soon I had the idea of using a transaction since hibernate must validate the database before inserting (just come up as a thought, nothing to do with how actually hibernate works).
So I myself invented a new technique, although it'll slow up your application, but for now atleast I can go with it, instead of having a buggy feature of not showing database update by hibernate. Below is what I used:
RESULT
Use Transaction in every select query you make along with all insert, update & delete queries.
So the main idea is to use a Transaction every time one has to query the database, no matter what type of query.
E.g.
Earlier Code :
Session sess = NewHibernateUtil.getSessionFactory().openSession();
SQLQuery q = sess.createSQLQuery("select * from user where iduser=(select iduser from account where idaccount"
+ "=:idaccount)");
q.setInteger("idaccount", idaccount);
q.addEntity(User.class);
User u = (User) q.uniqueResult();
sess.close();
New Code :
Session sess = NewHibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
SQLQuery q = sess.createSQLQuery("select * from user where iduser=(select iduser from account where idaccount"
+ "=:idaccount)");
q.setInteger("idaccount", idaccount);
q.addEntity(User.class);
User u = (User) q.uniqueResult();
tx.commit();
sess.close();
No comments:
Post a Comment