I've had a strange problem with my work recently, especially taking notes:
The data in the user table is 2, 2, respectively, and the ID and name fields.
The code is as follows:
Import Org.hibernate.Hibernate;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
public class Test {
private static final sessionfactory sessionfactory;
static {
try {
Create the sessionfactory from Hibernate.cfg.xml
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
} catch (Throwable ex) {
Make sure your log the exception, as it might be swallowed
System.err.println ("Initial sessionfactory creation failed." + ex);
throw new Exceptionininitializererror (ex);
}
}
public static Sessionfactory Getsessionfactory () {
return sessionfactory;
}
public static void Main (string[] args) {
Session session = Sessionfactory.opensession ();
Transaction tr = session.begintransaction ();
Tr.begin ();
User user = (user) session.load (user.class, 1);
User.setname ("2");
Session.saveorupdate (user);
int num = (Integer) session.createsqlquery ("SELECT COUNT (*) num from user where name = 2"). Addscalar ("num", Hibernate.int EGER). Uniqueresult ();
SYSTEM.OUT.PRINTLN (num);
Tr.commit ();
}
Printing results:
Hibernate:select user0_.id as id0_0_, user0_.name as name0_0_ from user user0_ where user0_.id=?
Hibernate:select COUNT (*) num from user where name = 2
1
Hibernate:update user set name=? where id=?
That means that by session.createsqlquery this method does not first update the user object, so the result is still 1, and I want the result is 2, so there will be such an error
To modify the code:
Import Org.hibernate.Hibernate;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
public class Test {
private static final sessionfactory sessionfactory;
static {
try {
Create the sessionfactory from Hibernate.cfg.xml
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
} catch (Throwable ex) {
Make sure your log the exception, as it might be swallowed
System.err.println ("Initial sessionfactory creation failed." + ex);
throw new Exceptionininitializererror (ex);
}
}
public static Sessionfactory Getsessionfactory () {
return sessionfactory;
}
public static void Main (string[] args) {
Session session = Sessionfactory.opensession ();
Transaction tr = session.begintransaction ();
Tr.begin ();
User user = (user) session.load (user.class, 1);
User.setname ("2");
Session.saveorupdate (user);
int num = (Integer) session.createquery ("from User WHERE name = 2"). List (). Size ();
SYSTEM.OUT.PRINTLN (num);
Tr.commit ();
}
}
Execution Result:
Hibernate:select user0_.id as id0_0_, user0_.name as name0_0_ from user user0_ where user0_.id=?
Hibernate:update user set name=? where id=?
Hibernate:select user0_.id as id0_, user0_.name as name0_ from user user0_ where user0_.name=2
2
This is the first update of the user object, and then to query, so this situation is correct
Summary: If with the newly found object, Saveorupdate, and then use the Session.createsqlquery method, but also the query is not modified objects, from the printed statement can be seen:
First query, and then update the Oh, so there will be problems
This article from "Follow the heart of the other side" blog, declined reprint!
An issue that occurs after Hibernate queries an object and executes a createsqlquery query