An issue that occurs after Hibernate queries an object and executes a createsqlquery query

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.