My favorites | Sign in
Project Home Downloads Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 1456: Cannot model a tree structure with JPA
13 people starred this issue and may be notified of changes. Back to list
Status:  Duplicate
Owner:  ----
Closed:  Sep 2009


Sign in to add a comment
 
Reported by danilo.a...@gmail.com, Apr 30, 2009
Hi.
I'm trying to implement something like a tree structure.
So i defined a class like this

@Entity
public class TestParent {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Extension(vendorName="datanucleus", key="gae.encoded-pk",
value="true")
        private String id;

        @OneToMany(mappedBy="padre", cascade=CascadeType.ALL)
        private Collection<TestParent> childs;

        @ManyToOne
        private TestParent padre;

        @Enumerated
        private String name;

        //getters and setters

}

I tried a lot of ways to persist this class.
in some cases i do it, in other i can't...

But my principal problem is that sometimes i'm getting this exception
when i wan to query the persisted objects

Field cl.faith.condoadmin.domain.TestParent.padre should be able to
provide a reference to its parent but the entity does not have an
parent.  Did you perhaps try to establish an instance of
cl.faith.condoadmin.domain.TestParent as the child of an instance of
cl.faith.condoadmin.domain.TestParent after the child had already been
persisted?
org.datanucleus.exceptions.NucleusUserException: Field
cl.faith.condoadmin.domain.TestParent.padre should be able to provide
a reference to its parent but the entity does not have an parent.  Did
you perhaps try to establish an instance of
cl.faith.condoadmin.domain.TestParent as the child of an instance of
cl.faith.condoadmin.domain.TestParent after the child had already been
persisted?

Reading the documentation i found something about entity groups parent
child relationships
but i expected that JPA took care of that...
in fact
When i manually created Key objects and set parent and child
relationships i got my expected result...
but i want to implement something not so tied to this platform...

Some ideas to share? 
Jul 23, 2009
#1 todd.lindner
The problem is in DatastoreRelationFieldManager.  There should be an additional check to see if the 
ManyToOne relationship is specified as optional=true  (which is should be in this above test case).  An entity 
with a collection of its own type will always require one top level entity with a null parent, and datanucleus 
doesn't want to allow a null Many To One Bidirectional.

private Object lookupParent(AbstractMemberMetaData ammd, JavaTypeMapping mapping) {
    Key parentKey = fieldManager.getEntity().getParent();
    if (parentKey == null) {
      String childClass = fieldManager.getStateManager().getClassMetaData().getFullClassName();
      throw new NucleusUserException("Field " + ammd.getFullFieldName() + " should be able to "
          + "provide a reference to its parent but the entity does not have a parent.  "
          + "Did you perhaps try to establish an instance of " + childClass  +  " as "
          + "the child of an instance of " + ammd.getTypeName() + " after the child had already been "
          + "persisted?").setFatal();
    }
    ObjectManager om = getStateManager().getObjectManager();
    return mapping.getObject(om, parentKey, NOT_USED);
  }

Jul 23, 2009
#2 max.r...@gmail.com
DatastoreRelationFieldManager should indeed be checking to see if the ManyToOne is
optional before throwing this exception.  The problem, however, is that DataNucleus
isn't processing this annotation attribute:
http://datanucleus.svn.sourceforge.net/viewvc/datanucleus/platform/jpa/branches/1.1/src/java/org/datanucleus/jpa/metadata/JPAAnnotationReader.java?revision=6540&view=markup

(see line 1435 and below).  I'll talk to DataNucleus about getting this fixed.
Sep 25, 2009
#4 max.r...@gmail.com
Marking this issue as a duplicate because it is already being tracked in the JDO/JPA
project:

https://code.google.com/p/datanucleus-appengine/issues/detail?id=80

Please star this issue in the other project if this is still important to you.

Thanks,
Max
Status: Duplicate
Jul 23, 2010
#5 alrah...@gmail.com
is this fixed yet?
Jan 27, 2011
#6 alrah...@gmail.com
why the hell google doesn't fix this?
Sign in to add a comment

Powered by Google Project Hosting