-->
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.  [ 13 posts ] 
Author Message
 Post subject: @CollectionOfElements not honoring @Cascade
PostPosted: Wed Jun 20, 2007 10:52 pm 
Newbie

Joined: Mon Apr 16, 2007 5:52 pm
Posts: 13
I am trying to get a save to cascade through into a CollectionOfElements. I can't get the annotations to work, but I have been able to make it work with a mapping file. I am hoping I am just missing something in my annotations.

I have a Person class who has a Set of ParentChild. ParentChild in turn contains a reference to another Person class. If I create personA, create personB, create a ParentChild, set the reference to the Person in the ParentChild object to personB and then add the ParentChild object to the Set in personA, when saving (actually flushing), I get a TransientObjectException.

I turned logging up to trace and ran with the mapping file and then with annotations and noticed several cascade-related log messages present with the mapping file are missing with annotations.

Mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "file:c:/dev/dtd/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
  <class name="collectionofelements.Person" table="Person" >

    <id name="id" type="long" access="property">
      <generator class="common.IncrementMinValueGenerator"/>
    </id>

    <version name="version" type="long" column="version"/>

    <property name="name" access="property" lazy="false"/>

    <set name="children" table="ParentChild" cascade="all-delete-orphan">
      <key column="parentId" not-null="true"/>
      <composite-element class="collectionofelements.ParentChild">
        <property name="rank" type="long" not-null="true"/>
        <many-to-one name="child" class="collectionofelements.Person"
            column="childId" not-null="true" cascade="save-update"/>
      </composite-element>
    </set>
  </class>
</hibernate-mapping>

Person annotations:
Code:
package collectionofelements;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.Version;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "Person")
public class Person {

  private long id;
  private String name;
  private long version;
  private Set<ParentChild> children = new HashSet<ParentChild>();

  @Id
  @GenericGenerator(name = "mygen", strategy = "common.IncrementMinValueGenerator")
  @GeneratedValue(generator = "mygen")

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @CollectionOfElements
  @JoinTable(name = "ParentChild", joinColumns = @JoinColumn(name = "parentId"))
  @Cascade({ org.hibernate.annotations.CascadeType.ALL,
             org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
  public Set<ParentChild> getChildren() {
    return children;
  }

  public void setChildren(Set<ParentChild> children) {
    this.children = children;
  }

  public void addChild(ParentChild parentChild) {
    this.children.add(parentChild);
  }

  @Version
  public long getVersion() {
    return version;
  }

  public void setVersion(long version) {
    this.version = version;
  }
}

ParentChild annotations:
Code:
package collectionofelements;

import org.hibernate.annotations.Cascade;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class ParentChild {
  private Person child;
  private long rank;

  @ManyToOne(targetEntity = Person.class)
  @JoinColumn(name = "childId")
  @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
  public Person getChild() {
    return child;
  }

  public void setChild(Person child) {
    this.child = child;
  }

  public long getRank() {
    return rank;
  }

  public void setRank(long rank) {
    this.rank = rank;
  }
}


Hibernate version:
3.2.3 GA, ANN 3.3.0 GA

Full stack trace of any exception that occurs:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: collectionofelements.Person
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)
   at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:307)
   at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)


Name and version of the database you are using:
SQLServer 2005

Trace level Hibernate log excerpt, mapping file:
Code:
2007-06-20 21:47:29,524 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:58 - flushing session
2007-06-20 21:47:29,539 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:111 - processing flush-time cascades
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.engine.Cascade:115 - processing cascade ACTION_SAVE_UPDATE for: collectionofelements.Person
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.engine.Cascade:291 - cascade ACTION_SAVE_UPDATE for collection: collectionofelements.Person.children
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.engine.CascadingAction:216 - cascading to saveOrUpdate: collectionofelements.Person
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.event.def.AbstractSaveEventListener:488 - persistent instance of: collectionofelements.Person
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.event.def.DefaultSaveOrUpdateEventListener:99 - ignoring persistent instance
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.event.def.DefaultSaveOrUpdateEventListener:136 - object already associated with session: [collectionofelements.Person#10001]
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.engine.Cascade:306 - done cascade ACTION_SAVE_UPDATE for collection: collectionofelements.Person.children
2007-06-20 21:47:29,539 [main] TRACE org.hibernate.engine.Cascade:150 - done processing cascade ACTION_SAVE_UPDATE for: collectionofelements.Person
2007-06-20 21:47:29,555 [main] TRACE org.hibernate.engine.Cascade:115 - processing cascade ACTION_SAVE_UPDATE for: collectionofelements.Person
2007-06-20 21:47:29,555 [main] TRACE org.hibernate.engine.Cascade:291 - cascade ACTION_SAVE_UPDATE for collection: collectionofelements.Person.children
2007-06-20 21:47:29,555 [main] TRACE org.hibernate.engine.Cascade:306 - done cascade ACTION_SAVE_UPDATE for collection: collectionofelements.Person.children
2007-06-20 21:47:29,555 [main] TRACE org.hibernate.engine.Cascade:150 - done processing cascade ACTION_SAVE_UPDATE for: collectionofelements.Person
2007-06-20 21:47:29,555 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:154 - dirty checking collections
2007-06-20 21:47:29,555 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:171 - Flushing entities and processing referenced collections
2007-06-20 21:47:29,555 [main] DEBUG org.hibernate.engine.Collections:176 - Collection found: [collectionofelements.Person.children#10000], was: [<unreferenced>] (initialized)
2007-06-20 21:47:29,571 [main] DEBUG org.hibernate.engine.Collections:176 - Collection found: [collectionofelements.Person.children#10001], was: [<unreferenced>] (initialized)
2007-06-20 21:47:29,571 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:210 - Processing unreferenced collections
2007-06-20 21:47:29,571 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:224 - Scheduling collection removes/(re)creates/updates
2007-06-20 21:47:29,571 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:85 - Flushed: 2 insertions, 0 updates, 0 deletions to 2 objects
2007-06-20 21:47:29,571 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:91 - Flushed: 2 (re)creations, 0 updates, 0 removals to 2 collections

Trace level Hibernate log excerpt, annotations:
Code:
2007-06-20 21:46:58,448 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:58 - flushing session
2007-06-20 21:46:58,463 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:111 - processing flush-time cascades
2007-06-20 21:46:58,463 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:154 - dirty checking collections
2007-06-20 21:46:58,463 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:171 - Flushing entities and processing referenced collections
2007-06-20 21:46:58,463 [main] DEBUG org.hibernate.engine.Collections:176 - Collection found: [collectionofelements.Person.children#10000], was: [<unreferenced>] (initialized)
2007-06-20 21:46:58,463 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:210 - Processing unreferenced collections
2007-06-20 21:46:58,479 [main] TRACE org.hibernate.event.def.AbstractFlushingEventListener:224 - Scheduling collection removes/(re)creates/updates
2007-06-20 21:46:58,479 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:85 - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
2007-06-20 21:46:58,479 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener:91 - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Cascade does not make sense with collection of elements

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:30 pm 
Newbie

Joined: Mon Apr 16, 2007 5:52 pm
Posts: 13
I'm not sure what you mean by "does not make sense".

As shown in my example, I have an entity, Class A, that contains a composite collection of Class B, which contains a member variable that is an instance of Class A. If I create a new instance of A, a1 and a new instance of B, b and another new instance of A, a2, and then set the reference in b to a2 and the reference in a1 to b, when I save a1, b and a2 should be saved also.

This works with the mapping file I provided. Perhaps I should not be using CollectionOfElements?

I'll use whatever annotations are correct, but I should be able to do what I can do with a mapping file using annotations, shouldn't I?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 4:03 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Emmanuel, he complains that cascade on @ManyToOne in @Embeddable doesn't work. I also had this at some point. Which is fine I guess, because it's not even in the spec.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 4:09 pm 
Newbie

Joined: Mon Apr 16, 2007 5:52 pm
Posts: 13
christian -

What spec do you mean?
The JPA spec?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 8:50 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes?

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 10:02 pm 
Newbie

Joined: Mon Apr 16, 2007 5:52 pm
Posts: 13
I asked because it didn't seem relevant when talking about @CollectionOfElements and @Cascade which are
Hibernate-specific annotations and not in the JPA spec.

I had assumed the Hibernate-specific annotations exist so that one can do what is possible with mapping files but not with the JPA annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 10:12 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Your case has nothing to do with @CollectionOfElements.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 22, 2007 9:48 am 
Newbie

Joined: Mon Apr 16, 2007 5:52 pm
Posts: 13
I had assumed it was both cascades, for @CollectionOfElements and @ManyToOne.

I still have the issue of these cascades working when specified using a mapping file and not working with annotations.

If I were to submit a patch that would enable this to work with annotations, would it be applied to the code?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 22, 2007 10:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, attach it to a JIRA case with a testcase for the annotations test suite. I'm not 100% sure that it is not working already, so wait for Emmanuels response before doing anything.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 22, 2007 4:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As christian pointed out, I misunderstood you case.
I don't clearly understand why it fails, can you post the minimal test case to JIRA, I'll have a look. (If you find the issue and fix, even better).

But remove your cascade strategies on the collectionOfElements property, it does not make sense.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 11:05 am 
Newbie

Joined: Fri Mar 28, 2008 9:08 am
Posts: 3
Hi,

I have bumped into the same problem. I think the problem is somewhere around ManyToOne association cascading in Embeddable classes. Does this issue became a JIRA issue? I can't see any trace of it. If needed, I have created a unit test that I could share.

Also I have found some other forum issues about this:

http://forum.hibernate.org/viewtopic.php?t=983766
http://forum.hibernate.org/viewtopic.php?t=983780


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:06 pm 
Newbie

Joined: Tue Nov 21, 2006 6:46 pm
Posts: 6
Just encountered the same issue. ManyToOne not cascading in Embeddable class.

I also found a JIRA issue on this:

http://opensource.atlassian.com/project ... e/HHH-3218


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