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();
}
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;
}
}
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));
}
}
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;
}
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);
}
Aggregations