-->
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.  [ 6 posts ] 
Author Message
 Post subject: Issue with cascade Persist
PostPosted: Tue May 29, 2007 9:44 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hibernate version:
3.2.0 CR1, 13.05.2006
3.2 cr2, May 5, 2006
3.2.0.CR1, 13.05.2006

Hi,
I have a 1:1 relationship between Person and Address. The foreign key is stored on the Person side. I have Cascade=CascadeType.Persist option on both sides of the relationship.

When I remove the Person from the DB, I get the following exception:

org.hibernate.ObjectDeletedException: deleted entity passed to persist.
any ideas what might be wrong, more details follow?


Full stack trace of any exception that occurs:
Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [com.f.ta3g.test.garage.Person#<null>]
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:646)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:638)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:225)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:53)


Debug level Hibernate log excerpt:

14:35:14,787 DEBUG DefaultDeleteEventListener:90 - deleting a persistent instance
14:35:14,787 DEBUG DefaultDeleteEventListener:127 - deleting [com.f.ta3g.test.garage.Person#component[c,i]{i=21563, c=3009}]
14:35:14,787 DEBUG SessionImpl:1301 - setting cache mode to: GET
14:35:14,787 DEBUG Cascade:237 - processing cascade ACTION_DELETE for: com.f.ta3g.test.garage.Person
14:35:14,787 DEBUG Cascade:259 - done processing cascade ACTION_DELETE for: com.f.ta3g.test.garage.Person
14:35:14,787 DEBUG SessionImpl:1301 - setting cache mode to: NORMAL
14:35:14,787 DEBUG SessionImpl:1301 - setting cache mode to: GET
14:35:14,787 DEBUG Cascade:237 - processing cascade ACTION_DELETE for: com.f.ta3g.test.garage.Person
14:35:14,787 DEBUG Cascade:259 - done processing cascade ACTION_DELETE for: com.f.ta3g.test.garage.Person
14:35:14,787 DEBUG SessionImpl:1301 - setting cache mode to: NORMAL
14:35:14,787 DEBUG JDBCTransaction:54 - begin
14:35:14,787 DEBUG ConnectionManager:415 - opening JDBC connection
14:35:14,802 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 2
14:35:14,802 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
14:35:14,802 DEBUG JDBCTransaction:59 - current autocommit status: true
14:35:14,802 DEBUG JDBCTransaction:62 - disabling autocommit
14:35:14,802 DEBUG JDBCContext:203 - after transaction begin
14:35:14,802 DEBUG JDBCTransaction:103 - commit
14:35:14,802 DEBUG SessionImpl:339 - automatically flushing session
14:35:14,802 DEBUG AbstractFlushingEventListener:58 - flushing session
14:35:14,802 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades
14:35:14,802 DEBUG Cascade:237 - processing cascade ACTION_PERSIST_ON_FLUSH for: com.f.ta3g.test.garage.Address
14:35:14,802 DEBUG CascadingAction:224 - cascading to persistOnFlush: com.f.ta3g.test.garage.Person
14:35:14,802 DEBUG AbstractSaveEventListener:473 - deleted instance of: com.f.ta3g.test.garage.Person
14:35:14,802 DEBUG JDBCTransaction:152 - rollback
14:35:14,802 DEBUG JDBCTransaction:193 - re-enabling autocommit
14:35:14,802 DEBUG JDBCTransaction:163 - rolled back JDBC Connection
14:35:14,802 DEBUG JDBCContext:208 - after transaction completion
14:35:14,802 DEBUG ConnectionManager:398 - aggressively releasing JDBC connection
14:35:14,802 DEBUG ConnectionManager:435 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
14:35:14,802 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
14:35:14,802 DEBUG SessionImpl:424 - after transaction completion


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 1:23 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
anybody got any ideas on this, should I open a JIRA?
Regards
Breako


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 1:52 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
As long as you are using release candidates and not final versions, don't do anything except upgrade.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 10:15 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
christian wrote:
As long as you are using release candidates and not final versions, don't do anything except upgrade.

The same happens with version:

Annotations: 3.2.0.GA
Core: version 3.2.3
EntityManager: 3.3.1.GA

Here is the Person entity
Code:
import javax.persistence.*;


@Entity
@Table(name="TPerson")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Person extends JPAEntity {

    // Rels
    //  one to one rel
    // side=from, from = homeaddress, to = person
    @OneToOne
    @JoinColumns({
         @JoinColumn(name="I_ADDRESS_HOMEADDRESS", referencedColumnName="I"),
         @JoinColumn(name="C_ADDRESS_HOMEADDRESS", referencedColumnName="C")})
    private Address homeAddress;

    public Address getHomeAddress() {
        return this.homeAddress;
    }
   
    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }
}


Here is the Address entity
Code:
import javax.persistence.*;

@Entity
@Table(name="TAddress")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Address extends JPAEntity {
   
    @OneToOne(cascade=CascadeType.PERSIST, mappedBy="homeAddress")
   private Person person;
   
   
    public void setPerson(Person person) {
       this.person = person;
    }
   
    public Person getPerson() {
       return person;
    }
}


and here is the test that reproduces the fault:
Code:
public void test2() {
     EntityManagerFactory emf =
                       Persistence.createEntityManagerFactory("h-source");
     EntityManager em = emf.createEntityManager();
     Person person = new Person();
     person.setPk(new EntityOid(3009, 10009));
     Address address = new Address();
     address.setPk(new EntityOid(3001, 10009));
      
     // wire both sides of rel
     person.setHomeAddress(address);
     address.setPerson(person);
      
     System.out.println(">> em.persist(person)");
     em.persist(address);
     em.getTransaction().begin();
     System.out.println(">> em.getTransaction().commit()");
     em.getTransaction().commit();

     em.remove(person);
     em.getTransaction().begin();
     em.getTransaction().commit();
}


The problem happens at the final commit.
If the em.clear() is inserted before em.remove(person), the problem does not happen.
I think the problem is happening, because I am using an extended persistence context and address is still attached. When the final commit happens, firePersistOnflush() excutes in hibernate which seems to want to persist the Address. the Address cascades the Persist to the Person which is of course removed.

Do you need anything else?
Should I write a JIRA?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 12:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Thing is it's your responsibility to maintain the objec model consistent, in your case you don't seem to detach (ie reference to null) the link from address to person. Nullifying the reference will fix the issue I think.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 12:31 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Thanks for your reply.
The thing is the foreign key is on the Person table not the Address entity, so I would have thought I would have got away with what I was doing.

I can get away with what I am doing if I remove the cascade = CascadeType.Persist option in Address.
So the object model consistency rules changes based on this setting which is a bit counter intuitive.
That said, you are correct, the problem goes away if I clear out the rel either explictly before or by using the
@PreRemove annotations to clear the relationship on both sides.
for example:
Code:
    @PreRemove
    public void preRemove() {
       if (homeAddress != null) {
          homeAddress.setPerson(null);
        }
    }


and the following to Address:

Code:
@PreRemove
public void removeReferences(){
        if (person != null) {
            person.setHomeAddress(null);
        }
}


I wonder which is the best approach, I'll take that in a separate thread, thank you for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.