use of org.neo4j.ogm.id.UuidStrategy 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);
}
}
Aggregations