Unnecessary version updates in two cases.
-----------------------------------------
Key: HHH-2955
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2955
Project: Hibernate3
Issue Type: Bug
Components: core
Affects Versions: 3.2.5
Environment: JDK 5.0
Reporter: Sławomir Wojtasiak
Attachments: HibernateTest.zip
I found two situations where hibernate generates unnecessary version updates. Let's
illustrate it with a simple example:
Session session = SessionFactory.getSession();
Transaction transaction = session.getTransaction();
transaction.begin();
Article a = new Article();
a.setName( "atricle" );
*********************************************
*** Quantity is the owner of the relation ***
*********************************************
Quantity q = new Quantity();
q.setName( "quantity" );
q.setArticle( a );
a.getQuantities().add( q );
session.persist( a );
session.flush();
***** Hibernate generates following SQLs *****
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Article (name, version, id) values (?, ?, ?)
Hibernate: insert into Quantity (article_id, name, version, id) values (?, ?, ?, ?)
**********************************************
a.getQuantities().clear();
session.flush();
*** Hibernate generates following SQLs ***
Hibernate: update Article set name=?, version=? where id=? and version=?
This update of version field is performed because collection of quantities is marked as
dirty, but Article entity is not relation owner so nothing change in database after this
clear. Should it works like this? It looks like a bug because database remain unchanged so
version changing is unnecessary in my opinion.
******************************************
session.clear();
**** SECOND PROBLEM ***
Now I generate true copy of persisted objects.
Notice that I use HashSet instead of PersistSet which was set during persist operation.
This operation is similar to merging objects prepared by SOAP, during communication with
remote client for example.
***********************
Article a1 = new Article();
a1.setId( a.getId() );
a1.setName( a.getName() );
a1.setVersion( a.getVersion() );
Quantity q1 = new Quantity();
q1.setArticle( a1 );
q1.setName( q.getName() );
q1.setVersion( q.getVersion() );
q1.setId( q.getId() );
a1.getQuantities().add( q1 );
a1 = (Article)session.merge( a1 );
session.flush();
***** This operation generates following SQLs *****
Hibernate: select article0_.id as id0_1_, article0_.name as name0_1_, article0_.version as
version0_1_, quantities1_.article_id as article4_3_, quantities1_.id as id3_,
quantities1_.id as id1_0_, quantities1_.article_id as article4_1_0_, quantities1_.name as
name1_0_, quantities1_.version as version1_0_ from Article article0_ left outer join
Quantity quantities1_ on article0_.id=quantities1_.article_id where article0_.id=?
Hibernate: update Article set name=?, version=? where id=? and version=?
It looks like problem is located in replaceElements() method of CollectgionType class (or
somewhere near it). Maybe I'm wrong but this collection was checked for changes during
merge operation (See this select above.) so why it remains dirty if it contains the same
data as database?. I checked this issue on other JPA implementations (OpenJPA for example)
and version is not incremented after similar merge operation.
*****************************************************
transaction.rollback();
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira