Ok. So Hibernate sends the SQL statements in a "random" time to the database. If you use flush(), it sends the statements when you want.
Using postgreSQL on windows, I HAVE TO work with transactions to have my data really inserted/updated/deleted. But if I use HSQLDB, it works fine just to call flush and close the sessions.
So what I'm trying to do is
Code:
//Creates a new session or returns a previously
//created one, stored in a ThreadLocal.
session = MyManagmentClass.getSession();
session.save(o);
session.flush();
//Closes the session (if not in a ThreadLocal).
//If it's in a ThreadLocal that means that this method is making part in a
//longer transaction.
MyManagementStuff.closeSession(session);
I can see the SQL statements on my console, and they are right. But when I query the database, the record is not there. As I said before this only happens with postgreSQL. HSQLDB runs ok (I just tried with these two).
I do not want to code like this:
Code:
//Creates a new session or returns a previously
//created one stored in a ThreadLocal.
session = MyManagmentClass.getSession();
//This code won't work if this method participates in a transaction beeing
//managed by another class.
tx = session.beginTransaction();
session.save(o);
session.flush();
tx.commit();
//Closes the session (if not in a ThreadLocal).
//If it's in a ThreadLocal that measn that this method is making part in a
//longer transaction.
MyManagementStuff.closeSession(session);
for the simple fact that there's no need to a transaction here.
Also, I may be calling this method from another class that's responsible for managing a transaction.
One example goes like this:
I have Users, Groups and Permissions.
Permissions can be assigned only to groups. To simulate an User having permissions, each User has an unique group for himself, wich has himself as the only User. So calls like addPermission(user, permission) are actually adding the permission to that special Group.
So I can call createGroup(group) that does something like the first lines of code I wrote above. But if I try to create an User, I must create the User and his special Group. So the transaction is being handled by another class wich calls createUser() and createGroup() in sequence and handles the transaction to asure that both classes are persisted.
Is this how hibernate must work or is this a bug with postgres. What should be the behavior of hibernate in this case?
I hope I made myself clear. If you guys can't understand what I mean. please tell so and I'll try to make myself clearer.