Summary: When using mySQL 5.0.41 and stepping through the code, encountering a session.flush() operation causes Hibernate to output the appropriate sql to the command line, but the database does not get updated until committing the transaction. However with HSQL flush updates the DB correctly.
Environment:
Hibernate 3.2
mySQL 5.041
connector/j 5.0.7 (also tried 5.0.6)
Windows
I discovered this in the middle of my project, however I reproduced using the CaveatEmptor native example available here
http://www.hibernate.org/400.html
Mapping documents:
Everything is as in the CaveatEmptor native example, however for User.hbm.xml I changed the ID generator to be <generator class="increment"/> and I commented out the hsql config and replaced it with (With HSQL everything works as expected):
Code:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">test_writer</property>
<property name="connection.password">password</property>
My test is as follows:Code:
public static void main(String[] args) {
System.out.println("Starting");
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
System.out.println("Middler");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
User user = new User("firstname", "lastname", "abcdef", "abcdef", "abcdef@foobar.com");
session.save(user);
user.setFirstname("George");
session.flush();
user.setFirstname("sam");
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
System.out.println("End");
}
The generated SQL (show_sql=true):The generated SQL is being printed out at the expected times. Save causes the first select. The next two (insert & update) are a result of the flush (a bit surpised by the update, but not a huge concern.) The final update occurs after the commit. No change in the DB until after the commit.
Code:
select
max(USER_ID)
from
USERS
Hibernate:
insert
into
USERS
(OBJ_VERSION, FIRSTNAME, LASTNAME, USERNAME, PASSWORD, EMAIL, RANK, IS_ADMIN, CREATED, DEFAULT_BILLING_DETAILS_ID, HOME_STREET, HOME_ZIPCODE, HOME_CITY, USER_ID)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
/* update
auction.model.User */ update
USERS
set
OBJ_VERSION=?,
FIRSTNAME=?,
LASTNAME=?,
`PASSWORD`=?,
EMAIL=?,
RANK=?,
IS_ADMIN=?,
DEFAULT_BILLING_DETAILS_ID=?,
HOME_STREET=?,
HOME_ZIPCODE=?,
HOME_CITY=?
where
USER_ID=?
and OBJ_VERSION=?
Hibernate:
/* update
auction.model.User */ update
USERS
set
OBJ_VERSION=?,
FIRSTNAME=?,
LASTNAME=?,
`PASSWORD`=?,
EMAIL=?,
RANK=?,
IS_ADMIN=?,
DEFAULT_BILLING_DETAILS_ID=?,
HOME_STREET=?,
HOME_ZIPCODE=?,
HOME_CITY=?
where
USER_ID=?
and OBJ_VERSION=?