Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem saving/retrieving embedded objects and enums [moved] #154

Closed
lvca opened this issue Dec 10, 2012 · 0 comments
Closed

problem saving/retrieving embedded objects and enums [moved] #154

lvca opened this issue Dec 10, 2012 · 0 comments

Comments

@lvca
Copy link
Member

lvca commented Dec 10, 2012

This is Issue 154 moved from a Google Code project.
Added by 2010-11-22T12:15:51.000Z by m.siemen...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Closed (Fixed).

Original labels: Type-Defect, Priority-Medium, v0.9.24

Original description

<b>What steps will reproduce the problem?</b>
1. declare a member of type object/enum in a domain class
2. create a schema property of type EMBEDDED for this member
3. try to save/retrieve it from orient database

What is the expected output?
objects and enums can be saved/retrieved from orientdb

What do you see instead?
exceptions when saving retrieving objects/enums

below is a patch that fixes the problem. see also discussion 
titled &quot;problem saving embedded objects re-introduced again&quot;
a testcase is also attached.

regards,
markus


Index: core/src/main/java/com/orientechnologies/orient/core/metadata/ 
schema/OType.java 
=================================================================== 
--- core/src/main/java/com/orientechnologies/orient/core/metadata/ 
schema/OType.java       (revision 1815) 
+++ core/src/main/java/com/orientechnologies/orient/core/metadata/ 
schema/OType.java       (working copy) 
@@ -25,6 +25,7 @@ 
 import com.orientechnologies.orient.core.id.ORID; 
 import com.orientechnologies.orient.core.record.ORecord; 
+import com.orientechnologies.orient.core.record.impl.ODocument; 
 /** 
  * Generic representation of a type.<br/> 
@@ -52,7 +53,7 @@ 
        }, 
        BINARY(&quot;Binary&quot;, 8, false, false, 8, new Class<?>[] { Array.class }, 
new Class<?>[] { Array.class }) { 
        }, 
-       EMBEDDED(&quot;Embedded&quot;, 9, true, false, 8, new Class<?>[] 
{ Object.class }, new Class<?>[] {}) { 
+       EMBEDDED(&quot;Embedded&quot;, 9, true, false, 8, new Class<?>[] 
{ Object.class }, new Class<?>[] { Object.class }) { 
        }, 
        EMBEDDEDLIST(&quot;List&quot;, 10, true, false, 8, new Class<?>[] 
{ List.class }, new Class<?>[] { Collection.class, Array.class }) { 
        }, 
@@ -290,6 +291,9 @@ 
                        return iValue; 
                if (iTargetClass.isEnum()) { 
+                       if (iValue instanceof ODocument) { 
+                               return Enum.valueOf((Class<Enum>) iTargetClass, (String) 
((ODocument) iValue).field( &quot;name&quot; ) ); 
+                       } 
                        if (iValue instanceof Number) 
                                return ((Class<Enum>) iTargetClass).getEnumConstants()[((Number) 
iValue).intValue()]; 
Index: core/src/main/java/com/orientechnologies/orient/core/db/ 
ODatabasePojoAbstract.java 
=================================================================== 
--- core/src/main/java/com/orientechnologies/orient/core/db/ 
ODatabasePojoAbstract.java      (revision 1815) 
+++ core/src/main/java/com/orientechnologies/orient/core/db/ 
ODatabasePojoAbstract.java      (working copy) 
@@ -23,6 +23,7 @@ 
 import com.orientechnologies.orient.core.command.OCommandRequest; 
 import 
com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; 
+import com.orientechnologies.orient.core.db.object.ODatabaseObject; 
 import 
com.orientechnologies.orient.core.db.object.OObjectNotManagedException; 
 import 
com.orientechnologies.orient.core.exception.OConfigurationException; 
 import com.orientechnologies.orient.core.hook.ORecordHook; 
@@ -259,10 +260,20 @@ 
                                if (record.getInternalStatus() == STATUS.NOT_LOADED) 
                                        record.load(); 
-                               pojo = newInstance(record.getClassName()); 
+                               boolean isEnum = false; 
+                               Class<?> javaClass = null; 
+                               if (this instanceof ODatabaseObject 
+                                               && (javaClass = ((ODatabaseObject) 
this).getEntityManager().getEntityClass( record.getClassName() )) != 
null 
+                                               && (isEnum = Enum.class.isAssignableFrom( javaClass ))) { 
+                                       pojo = (T) Enum.valueOf( (Class<Enum>) javaClass, (String) 
record.field( &quot;name&quot; ) ); 
+                               } else { 
+                                       pojo = newInstance(record.getClassName()); 
+                               } 
                                registerPojo(pojo, record); 
-                               stream2pojo(record, pojo, iFetchPlan); 
+                               if (!isEnum) { 
+                                       stream2pojo(record, pojo, iFetchPlan); 
+                               } 
                        } catch (Exception e) { 
                                throw new OConfigurationException(&quot;Can't retrieve pojo from the 
record &quot; + record, e); 
Index: core/src/main/java/com/orientechnologies/orient/core/ 
serialization/serializer/object/OObjectSerializerHelper.java 
=================================================================== 
--- core/src/main/java/com/orientechnologies/orient/core/serialization/ 
serializer/object/OObjectSerializerHelper.java  (revision 1815) 
+++ core/src/main/java/com/orientechnologies/orient/core/serialization/ 
serializer/object/OObjectSerializerHelper.java  (working copy) 
@@ -245,6 +245,9 @@ 
                                } else if (type.isEnum()) { 
                                        String enumName = ((ODocument) iLinked).field(iFieldName); 
+                                       if (null == enumName) { 
+                                               enumName = ((ODocument) iLinked).field( &quot;name&quot; ); 
+                                       } 
                                        @SuppressWarnings(&quot;rawtypes&quot;) 
                                        Class<Enum> enumClass = (Class<Enum>) type; 
                                        fieldValue = Enum.valueOf(enumClass, enumName); 
=================================================================== 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant