use of org.neo4j.ogm.exception.core.MappingException 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);
}
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class MappingContext method nativeId.
public Long nativeId(Object entity) {
ClassInfo classInfo = metaData.classInfo(entity);
if (classInfo == null) {
throw new IllegalArgumentException("Class " + entity.getClass() + " is not a valid entity class. " + "Please check the entity mapping.");
}
generateIdIfNecessary(entity, classInfo);
if (classInfo.hasIdentityField()) {
return EntityUtils.identity(entity, metaData);
} else {
final Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
if (primaryIndexValue == null) {
throw new MappingException("Field with primary id is null for entity " + entity);
}
LabelPrimaryId key = LabelPrimaryId.of(classInfo, primaryIndexValue);
Long graphId = primaryIdToNativeId.get(key);
if (graphId == null) {
graphId = EntityUtils.nextRef();
primaryIdToNativeId.put(key, graphId);
}
return graphId;
}
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class DomainInfo method registerDefaultFieldConverters.
private void registerDefaultFieldConverters(ClassInfo classInfo, FieldInfo fieldInfo) {
if (!fieldInfo.hasPropertyConverter() && !fieldInfo.hasCompositeConverter()) {
final String typeDescriptor = fieldInfo.getTypeDescriptor();
// Check if there's a registered set of attribute converters for the given field info and if so,
// select the correct one based on the features of the field
Function<AttributeConverters, Optional<AttributeConverter<?, ?>>> selectAttributeConverter = ac -> DomainInfo.selectAttributeConverterFor(fieldInfo, ac);
Optional<AttributeConverter<?, ?>> registeredAttributeConverter = ConvertibleTypes.REGISTRY.entrySet().stream().filter(e -> typeDescriptor.contains(e.getKey())).sorted(comparingInt((Map.Entry<String, ?> e) -> e.getKey().length()).reversed()).findFirst().map(Map.Entry::getValue).flatMap(selectAttributeConverter);
boolean isSupportedNativeType = typeSystem.supportsAsNativeType(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()));
// We can use a registered converter
if (registeredAttributeConverter.isPresent() && !isSupportedNativeType) {
fieldInfo.setPropertyConverter(registeredAttributeConverter.get());
} else {
// Check if the user configured one through the convert annotation
if (fieldInfo.getAnnotations().get(Convert.class) != null) {
// no converter's been set but this method is annotated with @Convert so we need to proxy it
Class<?> entityAttributeType = DescriptorMappings.getType(typeDescriptor);
String graphTypeDescriptor = fieldInfo.getAnnotations().get(Convert.class).get(Convert.GRAPH_TYPE, null);
if (graphTypeDescriptor == null) {
throw new MappingException("Found annotation to convert a " + (entityAttributeType != null ? entityAttributeType.getName() : " null object ") + " on " + classInfo.name() + '.' + fieldInfo.getName() + " but no target graph property type or specific AttributeConverter have been specified.");
}
fieldInfo.setPropertyConverter(new ProxyAttributeConverter(entityAttributeType, DescriptorMappings.getType(graphTypeDescriptor), this.conversionCallbackRegistry));
}
Class fieldType = DescriptorMappings.getType(typeDescriptor);
if (fieldType == null) {
throw new RuntimeException("Class " + classInfo.name() + " field " + fieldInfo.getName() + " has null field type.");
}
boolean enumConverterSet = false;
for (Class enumClass : enumTypes) {
if (fieldType.equals(enumClass)) {
setEnumFieldConverter(fieldInfo, enumClass);
enumConverterSet = true;
break;
}
}
if (!enumConverterSet && isEnum(fieldType)) {
LOGGER.debug("Setting default enum converter for unscanned class " + classInfo.name() + ", field: " + fieldInfo.getName());
setEnumFieldConverter(fieldInfo, fieldType);
}
}
}
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class DomainInfo method getClassInfo.
private ClassInfo getClassInfo(String fullOrPartialClassName, Map<String, ClassInfo> infos) {
// It is a fully, qualified name or at least matches to one.
if (infos.containsKey(fullOrPartialClassName)) {
return infos.get(fullOrPartialClassName);
}
Optional<String> foundKey = fqnLookup.computeIfAbsent(fullOrPartialClassName, k -> {
Pattern partialClassNamePattern = Pattern.compile(".+[\\\\.\\$]" + Pattern.quote(k) + "$");
String matchingKey = null;
for (String key : infos.keySet()) {
boolean isCandidate = partialClassNamePattern.matcher(key).matches();
if (isCandidate) {
ClassInfo candidate = infos.get(key);
String candidateNeo4jName = candidate.neo4jName() != null ? candidate.neo4jName() : key;
if (matchingKey != null) {
ClassInfo existingMatch = infos.get(matchingKey);
String previousMatchNeo4jName = existingMatch.neo4jName() != null ? existingMatch.neo4jName() : key;
boolean sameLabel = candidateNeo4jName.equals(previousMatchNeo4jName);
if (sameLabel) {
throw new MappingException("More than one class has simple name: " + fullOrPartialClassName);
}
}
if (matchingKey == null || candidateNeo4jName.equals(fullOrPartialClassName)) {
matchingKey = key;
}
}
}
return Optional.ofNullable(matchingKey);
});
return foundKey.map(infos::get).orElse(null);
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class PizzaIntegrationTest method shouldRaiseExceptionWhenAmbiguousClassLabelApplied.
// See #159
@Test
public void shouldRaiseExceptionWhenAmbiguousClassLabelApplied() {
Session sessionWithAmbiguousDomain = new SessionFactory(getDriver(), "org.neo4j.ogm.domain.pizza", "org.neo4j.ogm.domain.music").openSession();
Pizza pizza = new Pizza();
pizza.setName("Mushroom & Pepperoni");
List<String> labels = new ArrayList<>();
// We're adding the studio label, which is mapped to a type
labels.add("Studio");
pizza.setLabels(labels);
sessionWithAmbiguousDomain.save(pizza);
sessionWithAmbiguousDomain.clear();
try {
sessionWithAmbiguousDomain.load(Pizza.class, pizza.getId());
} catch (MappingException e) {
assertThat(e.getMessage()).isEqualTo("Multiple classes found in type hierarchy that map to: [Pizza, Studio]");
}
}
Aggregations