These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Indirect mapping deletion abnormally
PostPosted: Tue Dec 20, 2011 2:09 pm 
Newbie

Joined: Tue Dec 20, 2011 1:43 pm
Posts: 3
Hibernate 3 and 4 could not handle the deletion of entities shows below
Code:
class Master { @OneToMany List<Apprentice> apps; }
class Apprentice { @ManyToOne Master master; }
class Skill { /* an ordinary entity class */ }
class Proficiency {
  ProficiencyPK id;
  @ManyToOne Skill s; @ManyToOne Apprentice a; }
class ProficiencyPK { Integer skillId; Integer Apprentice appId; }

I inserted sample data for testing:
Master has 3 apprentice,
Skills Table have 3 skills(as constant) prepared for testing;
Thus, an apprentice has 3 proficiency entities associated with each skill.

The insertion is good. But when I test the deletion, it fails.
Code:
Master m = getOneSampleFromEntityManager();
List<Apprentice> lsta = m.getApprentices();
for(Apprentice a: lsta) {
  List<Proficiency> lstp = getFromEntityManagerByApprentice(a);
  for(Proficiency p: lstp) {
    em.remove(p); //!! Exception, see below
  }
}

The exception is
Code:
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
   at test.jpa.run.TestIndirectDep.test(TestIndirectDep.java:79)
   at test.jpa.run.TestIndirectDep.main(TestIndirectDep.java:88)
Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [test.jpa.entity.indirectdep.Apprentice#<null>]
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1197)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
   ... 2 more


Please note the apprentice is not deleted yet(the proficiency has dependency of it, so I have to delete the proficiency first. If I delete apprentice first, the database will throw a constraint violation). I do not have any cascade annotation.

Because remove the entity one by one failed, I tried to do batch update like
Code:
delete from Proficiency e where e.app=:app

It still fails with the same exception. I tested the code with Hibrenate 3.6.7(the exception shown above is with 3.6.7) and 4.0.0. The hibernate acts exactly the same.

However, I switched to EclipseLink, it works as expected(deletion is successful). So I wonder if there is a special way to do it with Hibernate. Maybe it could be entity manager bugs?

If there is a way to upload the source code I would do that.


Top
 Profile  
 
 Post subject: Re: Indirect mapping deletion abnormally
PostPosted: Wed Dec 21, 2011 4:51 pm 
Newbie

Joined: Tue Dec 20, 2011 1:43 pm
Posts: 3
I did some debugging inside Hibernate. And I found out the entity manager will do persist before executing query.

I set @OneToMany List< Apprentice > apps (Sorry I did not mention in my previous post) in class Master as CascadeType.ALL. So if I delete one Apprentice directly without removing it from Master.apps collection, Hibernate persist(as auto flush) this deleted Apprentice first before calling any action else(even for select query). Since I set the relation as cascadeType.ALL, when hibernate persists Master entity, it throws EntityNotFoundException for Apprentice shown above.

I believe this is a bug. First, cascade persisting a deleted entity will result in the delete entity removed from entity manager's cache, not throw an EntityNotFoundException. Second, the Exception should be detected earlier before calling the another query, which hides the real problem. In the previous post, the Apprentice was deleted already, but the error was thrown after several entity manager calls.

Any idea?


Top
 Profile  
 
 Post subject: Re: Indirect mapping deletion abnormally
PostPosted: Thu Dec 22, 2011 1:07 pm 
Newbie

Joined: Tue Dec 20, 2011 1:43 pm
Posts: 3
It seems I was rumbling. I have reported this issue to JIRA. Thanks to this forum for listening to my rumbling.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.