Search in sources :

Example 96 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class LoadByInstancesDelegate method findCommonClassInfo.

/**
 * @param objects
 * @param <T>
 * @return A class info that is part of the hierarchy of the distinct object types contained in {@code objects}.
 */
private <T> ClassInfo findCommonClassInfo(Collection<T> objects) {
    MetaData metaData = session.metaData();
    Set<ClassInfo> infos = objects.stream().map(// 
    Object::getClass).distinct().map(// 
    metaData::classInfo).map(// 
    LoadByInstancesDelegate::getRootClassInfo).collect(toSet());
    if (infos.size() != 1) {
        throw new MappingException("Can't find single supertype for " + infos);
    }
    return infos.iterator().next();
}
Also used : MetaData(org.neo4j.ogm.metadata.MetaData) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException)

Example 97 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class LoadOneDelegate method lookup.

private <T, U> T lookup(Class<T> type, U id) {
    Object ref;
    ClassInfo typeInfo = session.metaData().classInfo(type.getName());
    FieldInfo primaryIndex = typeInfo.primaryIndexField();
    if (typeInfo.annotationsInfo().get(RelationshipEntity.class) == null) {
        if (primaryIndex == null) {
            ref = session.context().getNodeEntity((Long) id);
        } else {
            ref = session.context().getNodeEntityById(typeInfo, id);
        }
    } else {
        if (primaryIndex == null) {
            // Coercing to Long. identityField.convertedType() yields no parameterised type to call cast() with.
            // But we know this will always be Long.
            ref = session.context().getRelationshipEntity((Long) id);
        } else {
            ref = session.context().getRelationshipEntityById(typeInfo, id);
        }
    }
    try {
        return type.cast(ref);
    } catch (ClassCastException cce) {
        logger.warn("Could not cast entity {} for id {} to {}", ref, id, type);
        return null;
    }
}
Also used : RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 98 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class IdentityMap method remember.

/**
 * constructs a 64-bit hash of this object's node properties
 * and maps the object to that hash. The object must not be null
 *
 * @param object   the object whose persistable properties we want to hash
 * @param entityId the native id of the entity
 */
void remember(Object object, Long entityId) {
    ClassInfo classInfo = metaData.classInfo(object);
    if (metaData.isRelationshipEntity(classInfo.name())) {
        this.relEntityHashes.put(entityId, hash(object, classInfo));
        this.snapshotsOfRelationshipEntities.put(entityId, EntitySnapshot.basedOn(metaData).take(object));
    } else {
        this.nodeHashes.put(entityId, hash(object, classInfo));
        this.snapshotsOfNodeEntities.put(entityId, EntitySnapshot.basedOn(metaData).take(object));
    }
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 99 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class IdentityMap method remembered.

/**
 * determines whether the specified has already
 * been memorised. The object must not be null. An object
 * is regarded as memorised if its hash value in the memo hash
 * is identical to a recalculation of its hash value.
 *
 * @param object   the object whose persistable properties we want to check
 * @param entityId the native id of the entity
 * @return true if the object hasn't changed since it was remembered, false otherwise
 */
boolean remembered(Object object, Long entityId) {
    // Bail out early if the native id is null...
    if (entityId == null) {
        return false;
    }
    ClassInfo classInfo = metaData.classInfo(object);
    boolean isRelEntity = metaData.isRelationshipEntity(classInfo.name());
    Map<Long, Long> hashes = isRelEntity ? relEntityHashes : nodeHashes;
    // ... or a little later when the hashes in question doesnt contain the entities id
    if (!hashes.containsKey(entityId)) {
        return false;
    }
    long actual = hash(object, classInfo);
    long expected = hashes.get(entityId);
    return actual == expected;
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 100 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class MappingContext method replaceNodeEntity.

public void replaceNodeEntity(Object entity, Long identity) {
    removeNodeEntity(entity, false);
    ClassInfo classInfo = metaData.classInfo(entity);
    if (classInfo.hasPrimaryIndexField()) {
        final Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
        LabelPrimaryId key = LabelPrimaryId.of(classInfo, primaryIndexValue);
        primaryIdToNativeId.put(key, identity);
    }
    addNodeEntity(entity, identity);
    deregisterDependentRelationshipEntity(entity);
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Aggregations

ClassInfo (org.neo4j.ogm.metadata.ClassInfo)145 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)100 Test (org.junit.Test)76 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 MetaData (org.neo4j.ogm.metadata.MetaData)8 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NodeBuilder (org.neo4j.ogm.cypher.compiler.NodeBuilder)5 MappingException (org.neo4j.ogm.exception.core.MappingException)5 RowModel (org.neo4j.ogm.model.RowModel)5 Collection (java.util.Collection)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Optional (java.util.Optional)4 CypherQuery (org.neo4j.ogm.cypher.query.CypherQuery)4 Member (org.neo4j.ogm.domain.forum.Member)4 Satellite (org.neo4j.ogm.domain.satellites.Satellite)4 Collections (java.util.Collections)3