Hibernate version: 3.1.2
I havent had much experience with Hibernate but we are working on making a Tapestry project with Hibernate back-end. I am facing a basic problem when an entity retrieved from Hibernate, if dirtied, is updated without an explicit "save" being issued. I reproduced the behaviour in a small code :
Code between sessionFactory.openSession() and session.close():
Code:
Client cl = new Client("John", "Smith", "john@yahoo.com");
Session session = factory.openSession();
session.save(cl);
session.flush();
session.close();
session = factory.openSession();
Client cl2 = (Client) session.get(Client.class, cl.getId());
cl2.setFirstName("James");
Address adr = new Address();
adr.setLine1("Someplace");
session.saveOrUpdate(adr);
session.flush();
session.close();
The log :
Code:
Hibernate: select max(id) from Client
Hibernate: insert into Client (first_name, last_name, email, id) values (?, ?, ?, ?)
Hibernate: select client0_.id as id0_0_, client0_.first_name as first2_0_0_, client0_.last_name as last3_0_0_, client0_.email as email0_0_ from Client client0_ where client0_.id=?
Hibernate: select max(id) from Address
Hibernate: insert into Address (line1, id) values (?, ?)
Hibernate: update Client set first_name=?, last_name=?, email=? where id=?
Without having issued an update on the Client entity, it is getting updated as part of the flush. This may work very badly with Tapestry because values in the POJOs get modified even during validation errors. Basically, dirty entities are bound to be around.
Is there anyway to avoid the entities being updated like this implicitly ? I know that the evict() method would solve but that means that I have to evict evey object being loaded from the DB !!
Pl help.
Thanks.[/code]