In fact, Chris Landry’s “explanation” blog skips 3 important SQLQuery API methods, and in fact, why he has such problems. In particular, (1) addSynchronizedQuerySpace, (2) addSynchronizedEntityName and (3) addSynchronizedEntityClass
As partenon points out, based on the SQL query string itself, Hibernate has no way of knowing which tables and / or entities are being requested in the query. Therefore, he does not know what changes in the queue for the session need to be dumped to the database. On a blog post, Chris points out that you can make a flush () call yourself before running an SQL query. However, what I am describing is the ability to automatically launch Hibernate. This actually does the same for HQL and Criteria queries. Only there he knows which tables are affected. In any case, this automatic flushing process makes the "minimum flash" flush only those that affect the request. Where these methods come into play.
As an example, a Chirs SQL query
session.createSqlQuery("select name from user where name = :userName")
Indeed, all he had to do was say ...
session.createSqlQuery("select name from user where name = :userName") .addSynchronizedQuerySpace( "user" )
addSynchronizedQuerySpace( "user" ) tells Hibernate that the query uses a table named "user". Hibernate can now automatically flush all changes pending for objects mapped to this user table.
source share