Search in sources :

Example 1 with IdDescription

use of org.springframework.data.neo4j.core.mapping.IdDescription in project spring-data-neo4j by spring-projects.

the class IdPopulatorTest method shouldIgnoreInternalIdGenerator.

@Test
void shouldIgnoreInternalIdGenerator() {
    IdDescription toBeReturned = IdDescription.forInternallyGeneratedIds(Constants.NAME_OF_ROOT_NODE);
    doReturn(toBeReturned).when(nodeDescription).getIdDescription();
    doReturn(nodeDescription).when(neo4jMappingContext).getRequiredPersistentEntity(Sample.class);
    IdPopulator idPopulator = new IdPopulator(neo4jMappingContext);
    Sample sample = new Sample();
    assertThat(idPopulator.populateIfNecessary(sample)).isSameAs(sample);
    verify(nodeDescription).getIdDescription();
    verify(neo4jMappingContext).getRequiredPersistentEntity(Sample.class);
    verifyNoMoreInteractions(nodeDescription, neo4jMappingContext);
}
Also used : IdDescription(org.springframework.data.neo4j.core.mapping.IdDescription) Test(org.junit.jupiter.api.Test)

Example 2 with IdDescription

use of org.springframework.data.neo4j.core.mapping.IdDescription in project spring-data-neo4j by spring-projects.

the class IdPopulator method populateIfNecessary.

Object populateIfNecessary(Object entity) {
    Assert.notNull(entity, "Entity may not be null!");
    Neo4jPersistentEntity<?> nodeDescription = neo4jMappingContext.getRequiredPersistentEntity(entity.getClass());
    IdDescription idDescription = nodeDescription.getIdDescription();
    if (idDescription == null) {
        if (nodeDescription.isRelationshipPropertiesEntity()) {
            return entity;
        } else {
            throw new IllegalStateException("Cannot persist implicit entity due to missing id property on " + nodeDescription.getUnderlyingClass() + ".");
        }
    }
    // Filter in two steps to avoid unnecessary object creation.
    if (!idDescription.isExternallyGeneratedId()) {
        return entity;
    }
    PersistentPropertyAccessor<?> propertyAccessor = nodeDescription.getPropertyAccessor(entity);
    Neo4jPersistentProperty idProperty = nodeDescription.getRequiredIdProperty();
    // Check existing ID
    if (propertyAccessor.getProperty(idProperty) != null) {
        return entity;
    }
    IdGenerator<?> idGenerator;
    // Get or create the shared generator
    // Ref has precedence over class
    Optional<String> optionalIdGeneratorRef = idDescription.getIdGeneratorRef();
    if (optionalIdGeneratorRef.isPresent()) {
        idGenerator = neo4jMappingContext.getIdGenerator(optionalIdGeneratorRef.get()).orElseThrow(() -> new IllegalStateException("Id generator named " + optionalIdGeneratorRef.get() + " not found!"));
    } else {
        idGenerator = neo4jMappingContext.getOrCreateIdGeneratorOfType(idDescription.getIdGeneratorClass().orElseThrow(() -> new IllegalStateException("Neither generator reference nor generator class configured.")));
    }
    propertyAccessor.setProperty(idProperty, idGenerator.generateId(nodeDescription.getPrimaryLabel(), entity));
    return propertyAccessor.getBean();
}
Also used : IdDescription(org.springframework.data.neo4j.core.mapping.IdDescription) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)

Aggregations

IdDescription (org.springframework.data.neo4j.core.mapping.IdDescription)2 Test (org.junit.jupiter.api.Test)1 Neo4jPersistentProperty (org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)1