My favorites | Sign in
Project Home Downloads Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 80: Recursive relation does not work
66 people starred this issue and may be notified of changes. Back to list
Status:  Verified
Owner:  ----
Closed:  Apr 2011


Sign in to add a comment
 
Reported by yongli2...@gmail.com, Jun 19, 2009
What steps will reproduce the problem?
1.
Define my model:

public class Category extends ModelObject {
    
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String id;
    
    @Persistent
    private Category parent;
    
    @Persistent(mappedBy="parent")
    private Set<Category> children = new HashSet<Category>();
2.
Execute create parent followed by creating child and assign the relation:

	public void testAddCategory() {
		System.out.println("start of testAddCategory");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {

			pm.currentTransaction().begin();
			Category p = new Category();
			p.setName("yyy");
			pm.makePersistent(p);
			String pid = p.getId();
			System.out.println("pid="+pid);
			pm.currentTransaction().commit();

			
			pm.currentTransaction().begin();
			Category c = new Category();
			c.setName("xxx");
			
			Category p4 = pm.getObjectById(Category.class, pid);
			c.setParent(p4);

			pm.makePersistent(c);
			String cid = c.getId();
			System.out.println("cid="+cid);	
			
			pm.currentTransaction().commit();
			

			// validation
			pm.currentTransaction().begin();
			Category c2 = pm.getObjectById(Category.class, cid);
			Category p2 = c2.getParent();
			String p2Name = p2.getName();
			System.out.println("p2Name: "+p2Name);
			pm.currentTransaction().commit();
			
			assertTrue("yyy".equals(p2Name));
			
		}catch (Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}finally {
			pm.close();
		}
	}
3.

What is the expected output? What do you see instead?

javax.jdo.JDOFatalUserException: Field
com.jcommerce.core.model.Category.parent 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 com.jcommerce.core.model.Category
as the child of an instance of com.jcommerce.core.model.Category after the
child had already been persisted?
	at
org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:406)
	at org.datanucleus.jdo.JDOTransaction.commit(JDOTransaction.java:137)


What version of the product are you using? On what operating system?
GAE-Java 1.2.1
WindowsXP

Please provide any additional information below.

Sep 25, 2009
Project Member #1 max.r...@gmail.com
(No comment was entered for this change.)
Status: Accepted
Labels: -Priority-Medium Priority-High
Sep 25, 2009
Project Member #2 max.r...@gmail.com
 Issue 119  has been merged into this issue.
Oct 22, 2010
#3 mel.llag...@gmail.com
Is this still an issue with the latest version of DataNucleus shipped with GAE 1.3.8?
Apr 14, 2011
Project Member #4 max.r...@gmail.com
Thanks for that patch, Rolf!
Status: Fixed
May 26, 2011
Project Member #5 max.r...@gmail.com
(No comment was entered for this change.)
Labels: TargetRelease-1.0.9
Jun 23, 2011
#6 roman.bosak
I am sorry, but it seems like it stil does not solve the issue.
Sep 2, 2011
#7 Mail.Rol...@googlemail.com
Roman, could you please be a little more specific about the problem you are facing?

The following Test for example, based on the information provided with the issue, does not fail (datanucleus-appengine 1.0.9).

>-------------------- ModelObject  ------------------------------<

package org.datanucleus.test;

public abstract class ModelObject {

}

>---------------------- Category --------------------------------<

package org.datanucleus.test;

import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Category extends ModelObject {

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String id;
    
    @Persistent
    private Category parent;
    
    @Persistent
    private String name;
    
    @Persistent(mappedBy="parent")
    private Set<Category> children = new HashSet<Category>();

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

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

	public String getId() {
		return id;
	}

	public Set<Category> getChildren() {
		return children;
	}
}

>-------------- RecursiveCategoryTest ---------------------------<

package org.datanucleus.store.appengine;

import org.datanucleus.test.Category;

public class RecursiveCategoryTest extends JDOTestCase {
	public void testAddCategory() {
		System.out.println("start of testAddCategory");
		
		//	pm is provided by JDOTestCase
		//	PersistenceManager pm = PMF.get().getPersistenceManager();
		try {

			pm.currentTransaction().begin();
			Category p = new Category();
			p.setName("yyy");
			pm.makePersistent(p);
			String pid = p.getId();
			System.out.println("pid="+pid);
			pm.currentTransaction().commit();

			
			pm.currentTransaction().begin();
			Category c = new Category();
			c.setName("xxx");
			
			Category p4 = pm.getObjectById(Category.class, pid);
			c.setParent(p4);

			pm.makePersistent(c);
			String cid = c.getId();
			System.out.println("cid="+cid);	
			
			pm.currentTransaction().commit();
			

			// validation
			pm.currentTransaction().begin();
			Category c2 = pm.getObjectById(Category.class, cid);
			Category p2 = c2.getParent();
			String p2Name = p2.getName();
			System.out.println("p2Name: "+p2Name);
			pm.currentTransaction().commit();
			
			assertTrue("yyy".equals(p2Name));
			
		}catch (Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}finally {
			pm.close();
		}
	}
}

 Rolf
Sep 2, 2011
#8 roman.bosak
Hi, I am sorry for my quick post before. Problem was on my side. I created and set keys  myself and did it wrong way - I didn't created Key for child using parent key. When I fixed this, it works perfectly.
Thank you
Sep 20, 2011
Project Member #9 max.r...@gmail.com
(No comment was entered for this change.)
Status: Verified
Sign in to add a comment

Powered by Google Project Hosting