Search in sources :

Example 1 with IdStrategy

use of org.neo4j.ogm.id.IdStrategy in project neo4j-ogm by neo4j.

the class MappingContext method generateIdIfNecessary.

private static void generateIdIfNecessary(Object entity, ClassInfo classInfo) {
    if (classInfo.idStrategyClass() == null || InternalIdStrategy.class.equals(classInfo.idStrategyClass())) {
        return;
    }
    if (classInfo.idStrategy() == null) {
        throw new MappingException("Id strategy " + classInfo.idStrategyClass() + " could not be instantiated " + "and wasn't registered. Either provide no argument constructor or register instance " + "with SessionFactory");
    }
    FieldInfo primaryIndexField = classInfo.primaryIndexField();
    Object existingUuid = classInfo.readPrimaryIndexValueOf(entity);
    if (existingUuid == null) {
        IdStrategy strategy = classInfo.idStrategy();
        Object id = strategy.generateId(entity);
        if (strategy instanceof UuidStrategy && primaryIndexField.isTypeOf(String.class)) {
            id = id.toString();
        }
        primaryIndexField.writeDirect(entity, id);
    }
}
Also used : IdStrategy(org.neo4j.ogm.id.IdStrategy) InternalIdStrategy(org.neo4j.ogm.id.InternalIdStrategy) UuidStrategy(org.neo4j.ogm.id.UuidStrategy) InternalIdStrategy(org.neo4j.ogm.id.InternalIdStrategy) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) MappingException(org.neo4j.ogm.exception.core.MappingException)

Aggregations

MappingException (org.neo4j.ogm.exception.core.MappingException)1 IdStrategy (org.neo4j.ogm.id.IdStrategy)1 InternalIdStrategy (org.neo4j.ogm.id.InternalIdStrategy)1 UuidStrategy (org.neo4j.ogm.id.UuidStrategy)1 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)1