Hi all,
In my web application I got a problem, If oneone help, I ll be very pleasent.
I developed my application using hibernate (at all requests I used a new openSession() and after the request I closed session). Because I open and close sessiın for every request, there was no error when I render changes.
But then I changed I application with session.getCurrentSession(). Now after I make a change at any object,It doesnt affect at render when I requset an ajax call.
I got there is no row with selected object.
I give two example and can you tell me where shall I make changes?
Code:
public Media addMessage(String filename, String content, Long uid, Long recipientId) {
Media med = null;
Transaction tx = null;
try {
session = HibernateUtil.getCurrentSession();
if (!session.isConnected()) {
session = HibernateUtil.getSession();
}
tx = session.beginTransaction();
User user = (User) session.load(User.class, uid);
User recipient = (User) session.load(User.class, recipientId);
med = new Media(filename, content, user, recipient);
session.save(med);
tx.commit(); // Automatically closes session
session.flush();
} catch (Exception e) {
return null;
}
return med;
}
First, I add e text message to db,
Code:
// used for refreshing
public Media getMessage(String filename, Long uid) {
Media m = null;
try {
session = HibernateUtil.getCurrentSession();
if (!session.isConnected()) {
session = HibernateUtil.getSession();
}
Query q = session.createQuery("from Media m where (m.fileName = :var1 and m.user = :var2)");
q.setString("var1", filename);
q.setLong("var2", uid);
List result = q.list();
if(result.size() > 0){
m = (Media) result.get(0);
}
session.flush();
} catch (HibernateException ex) {
return null;
}
return m;
}
then I use this method for render with ajax.
But I got exception here.
How Can I fix it?
Thanks...