private void populateRelations(EntityMetadata m, Object entity, Map<String, Object> relationMap, Node node,
            Map<Long, Object> nodeIdToEntityMap)
    {
        // Populate all relationship entities that are in Neo4J
        for (Relation relation : m.getRelations())
        {
            if (relation != null)
            {
                Class<?> targetEntityClass = relation.getTargetEntity();
                EntityMetadata targetEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                        targetEntityClass);
                Field property = relation.getProperty();
                if (relation.getPropertyType().isAssignableFrom(Map.class))
                {
                    Map<Object, Object> targetEntitiesMap = new HashMap<Object, Object>();

                    // If relationship entity is stored into Neo4J, fetch it
                    // immediately
                    if (isEntityForNeo4J(targetEntityMetadata))
                    {

                        for (Relationship relationship : node.getRelationships(Direction.OUTGOING,
                                DynamicRelationshipType.withName(relation.getJoinColumnName(kunderaMetadata))))
                        {
                            if (relationship == null)
                            {
                                continue;
                            }

                            // Target Entity
                            Node endNode = relationship.getEndNode();
                            if (endNode == null)
                            {
                                continue;
                            }
                            Object targetEntity = nodeIdToEntityMap.get(endNode.getId());
                            if (targetEntity == null)
                            {
                                targetEntity = mapper.getEntityFromNode(endNode, targetEntityMetadata);
                            }

                            // Relationship Entity
                            Object relationshipEntity = mapper.getEntityFromRelationship(relationship, m, relation);

                            // If this relationship is bidirectional, put source
                            // entity into Map field for target entity
                            Field bidirectionalField = relation.getBiDirectionalField();
                            Map<Object, Object> sourceEntitiesMap = new HashMap<Object, Object>();
                            if (bidirectionalField != null)
                            {
                                for (Relationship incomingRelationship : endNode.getRelationships(Direction.INCOMING,
                                        DynamicRelationshipType.withName(relation.getJoinColumnName(kunderaMetadata))))
                                {
                                    Node startNode = incomingRelationship.getStartNode();
                                    Object sourceEntity = nodeIdToEntityMap.get(startNode.getId());
                                    if (sourceEntity == null)
                                    {
                                        sourceEntity = mapper.getEntityFromNode(startNode, m);
                                        nodeIdToEntityMap.put(startNode.getId(), sourceEntity);
                                    }
                                    sourceEntitiesMap.put(relationshipEntity, sourceEntity);
                                }
                                PropertyAccessorHelper.set(targetEntity, bidirectionalField, sourceEntitiesMap);
                            }

                            // Set references to Target and owning entity in
                            // relationship entity
                            Class<?> relationshipClass = relation.getMapKeyJoinClass();
                            for (Field f : relationshipClass.getDeclaredFields())
                            {
                                if (!ReflectUtils.isTransientOrStatic(f))
                                {

                                    if (f.getType().equals(m.getEntityClazz()))
                                    {
                                        PropertyAccessorHelper.set(relationshipEntity, f, entity);
                                    }
                                    else if (f.getType().equals(targetEntityClass))
                                    {
                                        PropertyAccessorHelper.set(relationshipEntity, f, targetEntity);
                                    }
                                }
                            }
                            targetEntitiesMap.put(relationshipEntity, targetEntity);
                        }

                        PropertyAccessorHelper.set(entity, property, targetEntitiesMap);
                    }

                    /**
                     * If relationship entity is stored in a database other than
                     * Neo4J foreign keys are stored in "Proxy Nodes", retrieve
                     * these foreign keys and set into EnhanceEntity
                     */
                    else
                    {

                        for (Relationship relationship : node.getRelationships(Direction.OUTGOING,
                                DynamicRelationshipType.withName(relation.getJoinColumnName(kunderaMetadata))))
                        {
                            Node proxyNode = relationship.getEndNode();

                            String targetEntityIdColumnName = ((AbstractAttribute) targetEntityMetadata
                                    .getIdAttribute()).getJPAColumnName();
                            Object targetObjectId = proxyNode.getProperty(targetEntityIdColumnName);
                            Object relationshipEntity = mapper.getEntityFromRelationship(relationship, m, relation);

                            targetEntitiesMap.put(targetObjectId, relationshipEntity);
                        }

                        relationMap.put(relation.getJoinColumnName(kunderaMetadata), targetEntitiesMap);
                    }
                }
            }
        }
    }
private void populateRelations ( EntityMetadata m , Object entity , Map < String , Object > relationMap , Node node , Map < Long , Object > nodeIdToEntityMap ) { for ( Relation relation : m . getRelations ( ) ) { if ( relation != null ) { Class < ? > targetEntityClass = relation . getTargetEntity ( ) ; EntityMetadata targetEntityMetadata = KunderaMetadataManager . getEntityMetadata ( kunderaMetadata , targetEntityClass ) ; Field property = relation . getProperty ( ) ; if ( relation . getPropertyType ( ) . isAssignableFrom ( Map . class ) ) { Map < Object , Object > targetEntitiesMap = new HashMap < Object , Object > ( ) ; if ( isEntityForNeo4J ( targetEntityMetadata ) ) { for ( Relationship relationship : node . getRelationships ( Direction . OUTGOING , DynamicRelationshipType . withName ( relation . getJoinColumnName ( kunderaMetadata ) ) ) ) { if ( relationship == null ) { continue ; } Node endNode = relationship . getEndNode ( ) ; if ( endNode == null ) { continue ; } Object targetEntity = nodeIdToEntityMap . get ( endNode . getId ( ) ) ; if ( targetEntity == null ) { targetEntity = mapper . getEntityFromNode ( endNode , targetEntityMetadata ) ; } Object relationshipEntity = mapper . getEntityFromRelationship ( relationship , m , relation ) ; Field bidirectionalField = relation . getBiDirectionalField ( ) ; Map < Object , Object > sourceEntitiesMap = new HashMap < Object , Object > ( ) ; if ( bidirectionalField != null ) { for ( Relationship incomingRelationship : endNode . getRelationships ( Direction . INCOMING , DynamicRelationshipType . withName ( relation . getJoinColumnName ( kunderaMetadata ) ) ) ) { Node startNode = incomingRelationship . getStartNode ( ) ; Object sourceEntity = nodeIdToEntityMap . get ( startNode . getId ( ) ) ; if ( sourceEntity == null ) { sourceEntity = mapper . getEntityFromNode ( startNode , m ) ; nodeIdToEntityMap . put ( startNode . getId ( ) , sourceEntity ) ; } sourceEntitiesMap . put ( relationshipEntity , sourceEntity ) ; } PropertyAccessorHelper . set ( targetEntity , bidirectionalField , sourceEntitiesMap ) ; } Class < ? > relationshipClass = relation . getMapKeyJoinClass ( ) ; for ( Field f : relationshipClass . getDeclaredFields ( ) ) { if ( ! ReflectUtils . isTransientOrStatic ( f ) ) { if ( f . getType ( ) . equals ( m . getEntityClazz ( ) ) ) { PropertyAccessorHelper . set ( relationshipEntity , f , entity ) ; } else if ( f . getType ( ) . equals ( targetEntityClass ) ) { PropertyAccessorHelper . set ( relationshipEntity , f , targetEntity ) ; } } } targetEntitiesMap . put ( relationshipEntity , targetEntity ) ; } PropertyAccessorHelper . set ( entity , property , targetEntitiesMap ) ; } else { for ( Relationship relationship : node . getRelationships ( Direction . OUTGOING , DynamicRelationshipType . withName ( relation . getJoinColumnName ( kunderaMetadata ) ) ) ) { Node proxyNode = relationship . getEndNode ( ) ; String targetEntityIdColumnName = ( ( AbstractAttribute ) targetEntityMetadata . getIdAttribute ( ) ) . getJPAColumnName ( ) ; Object targetObjectId = proxyNode . getProperty ( targetEntityIdColumnName ) ; Object relationshipEntity = mapper . getEntityFromRelationship ( relationship , m , relation ) ; targetEntitiesMap . put ( targetObjectId , relationshipEntity ) ; } relationMap . put ( relation . getJoinColumnName ( kunderaMetadata ) , targetEntitiesMap ) ; } } } } }
Populates relationship entities into original entity.

@param m
the m
@param entity
the entity
@param relationMap
the relation map
@param node
the node
@param nodeIdToEntityMap
the node id to entity map