openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pinaki Poddar (JIRA)" <>
Subject [jira] Created: (OPENJPA-291) Entity Relation as identity for self-referential relation breaks enhancer
Date Mon, 23 Jul 2007 21:41:31 GMT
Entity Relation as identity for self-referential relation breaks enhancer

                 Key: OPENJPA-291
             Project: OpenJPA
          Issue Type: Bug
            Reporter: Pinaki Poddar
            Priority: Minor

Use Case:
A Node of a tree has a reference to parent node and a collection of child nodes.
The complexity is how the node is identified. Instead of a simple primary identity,
the node is using an identity that is composed of a primitive and its parent.

OpenJPA allows parent (a uni-cardinarity relation) to be used as part of compound primary
But the entire process of enhancement and metadata resolution do not fully cover when the
relation is self-recursive. 

The use case of defining such a key however does not seem to be common.

Anyway, consider Node of a tree defined for persistence as follows:


public class Node {
    private long id;
    @ManyToOne private Node parent;
    @OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
    private List<Node> children;

    public class SelfId {
	public long id;
	public SelfId parent;

1. Enhancer breaks on Node.class

   Because metadata resolution goes to infinite recursive loop and a StackOverflow

   The codelines that breaks are in MetaDataRepository and the 
   tentative patch shows why infinite recursion is happening by just stopping it. 
  The impact of cutting it short is not clear/investigated.
Index: openjpa-kernel/src/main/java/org/apache/openjpa/meta/
--- openjpa-kernel/src/main/java/org/apache/openjpa/meta/        (revision
+++ openjpa-kernel/src/main/java/org/apache/openjpa/meta/        (working
@@ -552,11 +552,15 @@

         // resolve relation primary key fields for mapping dependencies
         FieldMetaData[] fmds = meta.getDeclaredFields();
-        for (int i = 0; i < fmds.length; i++)
-            if (fmds[i].isPrimaryKey())
+        for (int i = 0; i < fmds.length; i++) {
+            if (fmds[i].isPrimaryKey()) {
+               if (fmds[i].getObjectIdFieldType().
+                       isAssignableFrom(meta.getDescribedType()))
+                       continue;
                     meta.getEnvClassLoader(), false);
+            }
+        }    

2. This patch stops the recursion but does not solve the problem.

   The enhanced Node class fails to create a ApplicationId. For a simple test case such as
(the test case is attached)
	public void testPersistTree() {
		OpenJPAEntityManager em = getEM();
		Node root = new Node(1000L,null);
		createTree(root, new int[]{1,2,3}, 2);// 3-levels, fanout/node=2 

<1.0.0-SNAPSHOT-SNAPSHOT nonfatal general error> org.apache.openjpa.persistence.PersistenceException:
	at org.apache.openjpa.kernel.BrokerImpl.persist(
	at org.apache.openjpa.kernel.BrokerImpl.persist(
	at org.apache.openjpa.kernel.BrokerImpl.persistAll(
	at org.apache.openjpa.kernel.SingleFieldManager.persist(
	at org.apache.openjpa.kernel.StateManagerImpl.cascadePersist(
	at org.apache.openjpa.kernel.BrokerImpl.persist(
	at org.apache.openjpa.kernel.BrokerImpl.persist(
	at org.apache.openjpa.kernel.DelegatingBroker.persist(
	at org.apache.openjpa.persistence.EntityManagerImpl.persist(
	at tree.TestTree.testPersistTree(
Caused by: java.lang.ClassCastException: org.apache.openjpa.util.ObjectId
	at tree.Node.pcCopyKeyFieldsToObjectId(
	at org.apache.openjpa.util.ApplicationIds.create(
	at org.apache.openjpa.kernel.BrokerImpl.persist(
	... 27 more

3. How does the enhanced Node class looks like?

   Firstly, Java decomiler (jad) itself is confused about the same method.
   $ jad classes\tree\Node.class
     Parsing classes\tree\Node.class...Parsing inner class classes\tree\Node$SelfId.class...Overwrite
Node.jad [y/n/a/s] ? y
     Generating Node.jad
     Couldn't fully decompile method pcCopyKeyFieldsToObjectId

   The decompiled file is attached.

4. The enhanced code is generated in

   But I found the debugging the piece hard.

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message