[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-1661) merge of a deleted object results in a insert?

Michael Grünewald (JIRA) noreply at atlassian.com
Mon Apr 20 12:55:18 EDT 2009


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1661?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=32933#action_32933 ] 

Michael Grünewald commented on HHH-1661:
----------------------------------------

Hi, may be you should validate saveOrUpdate too.

Part of my JUNit-Test: (PostgreSql 8.2.x)
/* Runs on empty DB. Otherwise the assert-statements would be a little bit mor complex. */
[code]
 for (int i = 0; i < 2; i++) {

		s = HibernateUtil.openSession();

		final int rows = listCriteriaAndEvictResults(s).size();
		System.out.println("Exists? " + s.get(CountryModel.class, country.getId()));
		s.close();
		s = HibernateUtil.openSession();

		t = s.beginTransaction();

		// s.saveOrUpdate(country);
		if (i == 0) {
		    s.save(country);
		}
		else {
		    country = (CountryModel) s.merge(country);
		}
		t.commit();
		System.out.println("SAVED!");
		s.close();

		s = null;
		s = HibernateUtil.openSession();
		Assert.assertEquals(rows + 1, listCriteriaAndEvictResults(s).size(), 0);
		s.close();

		s = HibernateUtil.openSession();
		t = s.beginTransaction();
		s.delete(country);
		t.commit();
		System.out.println("DELETED!");
		s.close();

		s = HibernateUtil.openSession();
		Assert.assertEquals(rows, listCriteriaAndEvictResults(s).size(), 0);
		s.close();
	    }
[/code]

[code]

    private List<Object> listCriteriaAndEvictResults(final Session s) {
	final Criteria c = s.createCriteria(CountryModel.class);
	final List<Object> list = c.list();
	for (final Object o : list) {
	    s.evict(o);
	}
	return list;
    }
[/code]

Cause with saveOrUpdate instead of the if-else-clause it throws an 
"org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update" (foreign key), with merge it works and prints this output:

Exists? null
SAVED!
DELETED!
Exists? null
SAVED!
DELETED!

Even I'm not quite sure if the above exception, is really caused by this problem. But merge works. so it should be.
In other set-ups I also got:
org.hibernate.HibernateException: reassociated object has dirty collection reference (or an array)

org.hibernate.HibernateException: Found two representations of same collection:
and.
org.hibernate.HibernateException: reassociated object has dirty collection reference (or an array)

(e.g. with lock())



So merge shouldn't work, but saveOrUpdate should work  instead. So cause the current behaviour is vice versa at the moment. You may should look at both methods.
May be that was the reason, why merge wasn't implemented that way it used to be.

By the way the extensive use of sessions was just to track down the real error cause.

Hope this piece of code fragment is enough to reproduce the problem. 
Otherwise please ask for the rest of it. Even Countrymodel is a very complex class with a huge object tree.

Greetings Michael






> merge of a deleted object results in a insert?
> ----------------------------------------------
>
>                 Key: HHH-1661
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1661
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.1.3
>         Environment: Hibernate 3.1.3, SQL Server
>            Reporter: Tu-Thach
>            Assignee: Gail Badner
>
> A persistent object is loaded from a session and deleted, then close the session.  Using that same object in another session and call merge results in the object being inserted into the database.  Here's the sample code:
> Session session = factory.getCurrentSession();
> session.beginTransaction();
> MyClass obj = (MyClass)session.load(MyClass.class, new Integer(10));
> session.delete(obj);
> session.getTransaction().commit();
> session = factory.getCurrentSession();
> session.beginTransaction();
> session.merge(obj);
> session.getTransaction().commit();
> Since the object is already deleted, merge could not find it and thus proceeds with a INSERT.  Shouldn't it throw an exception instead?

-- 
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.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       




More information about the hibernate-issues mailing list