Issue Type: Bug Bug
Affects Versions: 3.6.10
Assignee: Unassigned
Components: core
Created: 11/Jan/13 2:08 PM
Description:

I am unsure about how to create a test case for this one. Reproduction steps:

  1. Setup two entities with a bidirectional @OneToMany/@ManyToOne relationship like this:
    @Entity
    class Parent
    {
       @OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
        public List<Child> children;
    }
    
    @Entity
    class Child
    {
       @ManyToOne
       public Parent parent;
    }
  2. Create and persist a Parent with no children.
  3. In a new session, load the Parent from the database.
  4. Add a transient Child to the children collection.
  5. Call a read operation like .size() on the children collection.
  6. Attempt to persist the Parent
  7. Exception will result

The ultimate issue:

  1. When user does PersistentBag.add() under these circumstances write operation is queued.
  2. PersistentBag.size() triggers PersistentBag.read() which triggers PersistentBag.initialize()
  3. org.hibernate.engine.loading.CollectionLoadContext::endLoadingCollection() is eventually called. This is where the actual bug is.
    1. endLoadingCollection() calls org.hibernate.collection.AbstractPersistentCollection::endRead() which flushes all queued operations.
    2. AFTER flushing queued operations, endLoadingCollection() calls org.hibernate.enging.CollectionEntry::postInitialize() which creates what is supposed to be the pre-write snapshot of the bag.
  4. Bag snapshot is now incorrect – it contains all entities queued for write. If any of these entities are transient, they will cause an exception in org.hibernate.collection.AbstractPersistentCollection.getOrpahans(). When it iterates over the old snapshot, it makes a call to org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved() that cannot handle transient entities. Normally this wouldn't be an issue, but because snapshot contains queued writes, it can contain transient entities.
Environment: Hibernate 3.6.10, any database platform.
Project: Hibernate ORM
Labels: hibernate core
Priority: Major Major
Reporter: John Walker
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira