org.springframework.data.neo4j.mapping.DefaultNeo4jIsNewStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-ogm-spring-data Show documentation
Show all versions of neo4j-ogm-spring-data Show documentation
Neo4j support for Spring Data
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.mapping;
import java.util.function.Function;
import org.neo4j.ogm.id.InternalIdStrategy;
import org.neo4j.ogm.metadata.ClassInfo;
import org.neo4j.ogm.metadata.MetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.support.IsNewStrategy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Implementation of a {@link IsNewStrategy} that follows our supported identifiers and generators. Entities will be
* treated as new:
*
* - when using internally generated (database) ids and the id property is {@literal null} or of a numeric primitive
* less than or equal {@literal 0},
* - when using externally generated values and the id is {@literal null},
* - when using assigned values without a version property or with a version property that is {@literal null}.
*
*
* An entity will not be treated as new
*
* - when using internally generated (database) ids and the id property has a non-null value greater than
* {@literal 0},
* - when using externally generated values and the id property is not {@literal null},
* - when using assigned values together with {@link org.springframework.data.annotation.Version @Version} which has
* already a value not equal to {@literal null} or {@literal 0}.
*
*
* @author Michael J. Simons
* @since 5.1.20
*/
class DefaultNeo4jIsNewStrategy implements IsNewStrategy {
private static final Logger log = LoggerFactory.getLogger(DefaultNeo4jIsNewStrategy.class);
static IsNewStrategy basedOn(Neo4jPersistentEntity> entity, MetaData ogmMetadata) {
Assert.notNull(entity, "Entity meta data must not be null.");
ClassInfo classInfo = ogmMetadata.classInfo(entity.getType());
boolean internallyGeneratedId = classInfo.hasIdentityField();
boolean externallyGeneratedId =
classInfo.idStrategyClass() != null && InternalIdStrategy.class != classInfo.idStrategyClass();
boolean assignedId = !internallyGeneratedId && classInfo.idStrategyClass() == null;
Class> valueType;
if (classInfo.hasIdentityField()) {
valueType = classInfo.identityField().type();
} else if (classInfo.hasPrimaryIndexField()) {
valueType = classInfo.primaryIndexField().type();
} else {
throw new IllegalStateException(
String.format("Required identifier property not found for %s!", entity.getType()));
}
if (externallyGeneratedId && valueType.isPrimitive()) {
throw new IllegalArgumentException(String.format("Cannot use %s with externally generated, primitive ids.",
DefaultNeo4jIsNewStrategy.class.getName()));
}
Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy