Permalink
Browse files

Added null check

  • Loading branch information...
1 parent 13eb9a7 commit 65e6e0180ee2d625b209182e8e3bd92d94a54708 xamry committed Jun 1, 2011
Showing with 25 additions and 23 deletions.
  1. +25 −23 src/main/java/com/impetus/kundera/mongodb/MongoDBDataHandler.java
View
48 src/main/java/com/impetus/kundera/mongodb/MongoDBDataHandler.java
@@ -69,29 +69,31 @@ public Object getEntityFromDocument(EntityManagerImpl em, Class<?> entityClass,
Object embeddedObject = document.get(embeddedPropertyField.getName());
-
- if(relation.isUnary()) {
- BasicDBObject relDBObj = (BasicDBObject) embeddedObject;
- Object embeddedEntity = new MongoDBDataHandler().getEntityFromDocument(em, embeddedEntityClass, relMetadata, relDBObj);
- PropertyAccessorHelper.set(entity, embeddedPropertyField, embeddedEntity);
- } else if(relation.isCollection()) {
- BasicDBList relList = (BasicDBList) embeddedObject; //List of embedded objects
-
- Collection<Object> embeddedEntities = null; //Collection of embedded entities
- if (relation.getPropertyType().equals(Set.class)) {
- embeddedEntities = new HashSet<Object>();
- } else if (relation.getPropertyType().equals(List.class)) {
- embeddedEntities = new ArrayList<Object>();
- }
-
- for(int i = 0; i < relList.size(); i++) {
- BasicDBObject relObj = (BasicDBObject)relList.get(i);
- Object embeddedEntity = new MongoDBDataHandler().getEntityFromDocument(em, embeddedEntityClass, relMetadata, relObj);
- embeddedEntities.add(embeddedEntity);
- }
-
- PropertyAccessorHelper.set(entity, embeddedPropertyField, embeddedEntities);
- }
+ if(embeddedObject != null) {
+ if(relation.isUnary()) {
+ BasicDBObject relDBObj = (BasicDBObject) embeddedObject;
+ Object embeddedEntity = new MongoDBDataHandler().getEntityFromDocument(em, embeddedEntityClass, relMetadata, relDBObj);
+ PropertyAccessorHelper.set(entity, embeddedPropertyField, embeddedEntity);
+ } else if(relation.isCollection()) {
+ BasicDBList relList = (BasicDBList) embeddedObject; //List of embedded objects
+
+ Collection<Object> embeddedEntities = null; //Collection of embedded entities
+ if (relation.getPropertyType().equals(Set.class)) {
+ embeddedEntities = new HashSet<Object>();
+ } else if (relation.getPropertyType().equals(List.class)) {
+ embeddedEntities = new ArrayList<Object>();
+ }
+
+ for(int i = 0; i < relList.size(); i++) {
+ BasicDBObject relObj = (BasicDBObject)relList.get(i);
+ Object embeddedEntity = new MongoDBDataHandler().getEntityFromDocument(em, embeddedEntityClass, relMetadata, relObj);
+ embeddedEntities.add(embeddedEntity);
+ }
+
+ PropertyAccessorHelper.set(entity, embeddedPropertyField, embeddedEntities);
+ }
+ }
+
}
} catch (InstantiationException e) {

0 comments on commit 65e6e01

Please sign in to comment.