不存在主要 SDN 标签 ..(即以 _ 开头的标签)
No primary SDN label exists .. (i.e one starting with _)
Spring数据neo4j版本:3.3.0.RELEASE
Neo4j 版本:2.2.2
我 运行 当我试图查询暴露在 spring 数据存储库上的默认方法时,我 运行 出现了以下异常,即
personRepository.findAll(getDefaultPageable());
public static Pageable getDefaultPageable() {
BiFunction<Integer, Integer, Pageable> biFunction = PageRequest::new; // Method reference syntax in action
return biFunction.apply(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE);
}
Caused by: java.lang.IllegalStateException: No primary SDN label exists .. (i.e one starting with _)
at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:136)
at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:40)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102)
at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:77)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.projectTo(Neo4jEntityPersister.java:216)
at org.springframework.data.neo4j.support.Neo4jTemplate.projectTo(Neo4jTemplate.java:241)
at org.springframework.data.neo4j.support.conversion.EntityResultConverter.doConvert(EntityResultConverter.java:73)
at org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:44)
at org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:165)
at org.springframework.data.neo4j.conversion.QueryResultBuilder.underlyingObjectToObject(QueryResultBuilder.java:86)
at org.neo4j.helpers.collection.IterableWrapper$MyIteratorWrapper.underlyingObjectToObject(IterableWrapper.java:57)
at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)
at org.springframework.data.neo4j.repository.AbstractGraphRepository.subList(AbstractGraphRepository.java:371)
at org.springframework.data.neo4j.repository.AbstractGraphRepository.extractPage(AbstractGraphRepository.java:355)
at org.springframework.data.neo4j.repository.AbstractGraphRepository.findAll(AbstractGraphRepository.java:321)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:414)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:399)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy60.findAll(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy61.findAll(Unknown Source)
我真的不想在这里做任何复杂的事情,所以我很难理解我是否真的应该引入一个带有下划线前缀标签的自定义查询并为其使用注释@Query?来自 Spring 数据的默认方法不是只适用于 SDN 吗?
##更新##
我调整了密码查询,但仍然出现相同的错误。
used by: java.lang.IllegalStateException: No primary SDN label exists .. (i.e one starting with _)
at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:136)
at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:40)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102)
at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:77)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189)
at org.springframework.data.neo4j.support.Neo4jTemplate.createEntityFromState(Neo4jTemplate.java:224)
能否请您看一下 classes 并让我知道哪里和哪里做错了?
这是存储库 class
public interface PersonRepository extends GraphRepository<Person>, CypherDslRepository<Person> {
String PERSONS_WITH_SEVERAL_ACME_IDENTITY = "MATCH (p:Person)-[r:HAS_LOGIN]->(a:AcmeIdp) " +
"SET p:_Person, a:_AcmeIdp "+
"WITH p, count(p) AS numoutgoing " +
"WHERE numoutgoing > 1 " +
"MATCH (p:Person)-[r:HAS_LOGIN]->(a:AcmeIdp) " +
"SET p:_Person, a:_AcmeIdp "+
"RETURN p";
Iterable<Person> findByFirstNameAndLastName(@NonNull final String firstName,@NonNull final String lastName);
Page<Person> findByFirstNameLike(@NonNull final String firstName,@NonNull final Pageable pageable);
@Query(PERSONS_WITH_SEVERAL_ACME_IDENTITY)
Page<Person> findOnesWithSeveralAcmeIdentities(@NonNull final Pageable pageable); }
人物实体class
import com.example.analytics.model.AbstractEntity;
import com.example.analytics.model.relationship.Login;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.*;
import java.time.LocalDateTime;
import java.util.Set;
@NodeEntity(useShortNames = true)
public class Person extends AbstractEntity {
@GraphProperty(propertyName = "title", defaultValue = "")
private String title;
@GraphProperty(propertyName = "firstName", defaultValue = "")
private String firstName;
@GraphProperty(propertyName = "lastName", defaultValue = "")
private String lastName;
@GraphProperty(propertyName = "dateOfBirth", propertyType = String.class)
private LocalDateTime dateOfBirth;
@RelatedTo(type = "HAS_LOGIN", direction = Direction.OUTGOING, elementClass = AcmeIdentity.class)
private Set<Login> logins;
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
public LocalDateTime getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(final LocalDateTime dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Set<Login> getLogins() {
return logins;
}
public void setLogins(@NonNull final Set<Login> logins) {
this.logins = logins;
}
}
转换器
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class LocalDateTimeToStringConverter implements Converter<LocalDateTime, String> {
private static final Logger LOG = LoggerFactory.getLogger(LocalDateTimeToStringConverter.class);
@Value("${neo4j.dateTime.format:yyyy-MM-dd HH:mm:ss}")
private String dateTimeFormat;
@Override
public String convert(@NonNull final LocalDateTime source) {
LOG.debug("Converting LocalDateTime into String using the format: {}", dateTimeFormat);
return String.valueOf(source.format(DateTimeFormatter.ofPattern(dateTimeFormat)));
}
}
反向转换器
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
private static final Logger LOG = LoggerFactory.getLogger(StringToLocalDateTimeConverter.class);
@Value("${neo4j.dateTime.format:yyyy-MM-dd HH:mm:ss}")
private String dateTimeFormat;
/**
* Convert the source of type S to target type T.
*
* @param source the source object to convert, which must be an instance of S (never {@code null})
* @return the converted object, which must be an instance of T (potentially {@code null})
* @throws IllegalArgumentException if the source could not be converted to the desired target type
*/
@Override
public LocalDateTime convert(@NonNull final String source) {
LOG.debug("Converting String {} into LocalDateTime using the format: {}",source, dateTimeFormat);
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(dateTimeFormat));
}
}
主要配置class
import com.example.analytics.converter.LocalDateTimeToStringConverter;
import com.example.analytics.converter.StringToLocalDateTimeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@ComponentScan(basePackages = {"com.example.analytics"})
@PropertySource(value = "classpath:application.yml", ignoreResourceNotFound = false)
@Import({DatabaseConnectionConfiguration.class})
public class MainConfiguration extends Neo4jConfiguration {
@Autowired
private StringToLocalDateTimeConverter stringToLocalDateTimeConverter;
@Autowired
private LocalDateTimeToStringConverter localDateTimeToStringConverter;
private static final Logger LOG = LoggerFactory.getLogger(MainConfiguration.class);
public MainConfiguration() {
setBasePackage("com.example.analytics.model");
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
@DependsOn({ "stringToLocalDateTimeConverter", "localDateTimeToStringConverter" })
protected ConversionService neo4jConversionService() throws Exception {
LOG.debug("Adding custom converters to conversion service ...");
ConversionService conversionService = super.neo4jConversionService();
ConverterRegistry registry = (ConverterRegistry) conversionService;
registry.addConverter(stringToLocalDateTimeConverter);
registry.addConverter(localDateTimeToStringConverter);
LOG.debug("Addition of Converter from LocalDateTime to String and vice-versa has been completed!");
return conversionService;
}
}
使用存储库的服务片段
public Page<Person> findPersonsWithSeveralAcmeIdentitites(final Pageable pageable) {
LOG.debug("Fetching persons with several ACME identities");
if(pageable != null)
return personRepository.findOnesWithSeveralAcmeIdentities(pageable);
else
return personRepository.findOnesWithSeveralAcmeIdentities(getDefaultPageable());
}
然后您必须遍历您的节点并在每个节点上调用:
neo4jTemplate.postEntityCreation(node, MyEntity.class);
或者这也应该有效:
MATCH (n:User) set n:_User;
等等
我认为你过度 Java8 方法引用了!!!只需使用普通的旧 new
。否则您的代码将无法继续维护。
public static Pageable getDefaultPageable() {
return new PageRequest(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE);
}
Spring数据neo4j版本:3.3.0.RELEASE Neo4j 版本:2.2.2
我 运行 当我试图查询暴露在 spring 数据存储库上的默认方法时,我 运行 出现了以下异常,即
personRepository.findAll(getDefaultPageable());
public static Pageable getDefaultPageable() {
BiFunction<Integer, Integer, Pageable> biFunction = PageRequest::new; // Method reference syntax in action
return biFunction.apply(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE);
}
Caused by: java.lang.IllegalStateException: No primary SDN label exists .. (i.e one starting with _) at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:136) at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:40) at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36) at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26) at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102) at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165) at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142) at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:77) at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170) at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189) at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.projectTo(Neo4jEntityPersister.java:216) at org.springframework.data.neo4j.support.Neo4jTemplate.projectTo(Neo4jTemplate.java:241) at org.springframework.data.neo4j.support.conversion.EntityResultConverter.doConvert(EntityResultConverter.java:73) at org.springframework.data.neo4j.conversion.DefaultConverter.convert(DefaultConverter.java:44) at org.springframework.data.neo4j.support.conversion.EntityResultConverter.convert(EntityResultConverter.java:165) at org.springframework.data.neo4j.conversion.QueryResultBuilder.underlyingObjectToObject(QueryResultBuilder.java:86) at org.neo4j.helpers.collection.IterableWrapper$MyIteratorWrapper.underlyingObjectToObject(IterableWrapper.java:57) at org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47) at org.springframework.data.neo4j.repository.AbstractGraphRepository.subList(AbstractGraphRepository.java:371) at org.springframework.data.neo4j.repository.AbstractGraphRepository.extractPage(AbstractGraphRepository.java:355) at org.springframework.data.neo4j.repository.AbstractGraphRepository.findAll(AbstractGraphRepository.java:321) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:414) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:399) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy60.findAll(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) at com.sun.proxy.$Proxy61.findAll(Unknown Source)
我真的不想在这里做任何复杂的事情,所以我很难理解我是否真的应该引入一个带有下划线前缀标签的自定义查询并为其使用注释@Query?来自 Spring 数据的默认方法不是只适用于 SDN 吗?
##更新##
我调整了密码查询,但仍然出现相同的错误。
used by: java.lang.IllegalStateException: No primary SDN label exists .. (i.e one starting with _) at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:136) at org.springframework.data.neo4j.support.typerepresentation.LabelBasedNodeTypeRepresentationStrategy.readAliasFrom(LabelBasedNodeTypeRepresentationStrategy.java:40) at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36) at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26) at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102) at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165) at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142) at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:77) at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170) at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:189) at org.springframework.data.neo4j.support.Neo4jTemplate.createEntityFromState(Neo4jTemplate.java:224)
能否请您看一下 classes 并让我知道哪里和哪里做错了?
这是存储库 class
public interface PersonRepository extends GraphRepository<Person>, CypherDslRepository<Person> {
String PERSONS_WITH_SEVERAL_ACME_IDENTITY = "MATCH (p:Person)-[r:HAS_LOGIN]->(a:AcmeIdp) " +
"SET p:_Person, a:_AcmeIdp "+
"WITH p, count(p) AS numoutgoing " +
"WHERE numoutgoing > 1 " +
"MATCH (p:Person)-[r:HAS_LOGIN]->(a:AcmeIdp) " +
"SET p:_Person, a:_AcmeIdp "+
"RETURN p";
Iterable<Person> findByFirstNameAndLastName(@NonNull final String firstName,@NonNull final String lastName);
Page<Person> findByFirstNameLike(@NonNull final String firstName,@NonNull final Pageable pageable);
@Query(PERSONS_WITH_SEVERAL_ACME_IDENTITY)
Page<Person> findOnesWithSeveralAcmeIdentities(@NonNull final Pageable pageable); }
人物实体class
import com.example.analytics.model.AbstractEntity;
import com.example.analytics.model.relationship.Login;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.*;
import java.time.LocalDateTime;
import java.util.Set;
@NodeEntity(useShortNames = true)
public class Person extends AbstractEntity {
@GraphProperty(propertyName = "title", defaultValue = "")
private String title;
@GraphProperty(propertyName = "firstName", defaultValue = "")
private String firstName;
@GraphProperty(propertyName = "lastName", defaultValue = "")
private String lastName;
@GraphProperty(propertyName = "dateOfBirth", propertyType = String.class)
private LocalDateTime dateOfBirth;
@RelatedTo(type = "HAS_LOGIN", direction = Direction.OUTGOING, elementClass = AcmeIdentity.class)
private Set<Login> logins;
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
public LocalDateTime getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(final LocalDateTime dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Set<Login> getLogins() {
return logins;
}
public void setLogins(@NonNull final Set<Login> logins) {
this.logins = logins;
}
}
转换器
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class LocalDateTimeToStringConverter implements Converter<LocalDateTime, String> {
private static final Logger LOG = LoggerFactory.getLogger(LocalDateTimeToStringConverter.class);
@Value("${neo4j.dateTime.format:yyyy-MM-dd HH:mm:ss}")
private String dateTimeFormat;
@Override
public String convert(@NonNull final LocalDateTime source) {
LOG.debug("Converting LocalDateTime into String using the format: {}", dateTimeFormat);
return String.valueOf(source.format(DateTimeFormatter.ofPattern(dateTimeFormat)));
}
}
反向转换器
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
private static final Logger LOG = LoggerFactory.getLogger(StringToLocalDateTimeConverter.class);
@Value("${neo4j.dateTime.format:yyyy-MM-dd HH:mm:ss}")
private String dateTimeFormat;
/**
* Convert the source of type S to target type T.
*
* @param source the source object to convert, which must be an instance of S (never {@code null})
* @return the converted object, which must be an instance of T (potentially {@code null})
* @throws IllegalArgumentException if the source could not be converted to the desired target type
*/
@Override
public LocalDateTime convert(@NonNull final String source) {
LOG.debug("Converting String {} into LocalDateTime using the format: {}",source, dateTimeFormat);
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(dateTimeFormat));
}
}
主要配置class
import com.example.analytics.converter.LocalDateTimeToStringConverter;
import com.example.analytics.converter.StringToLocalDateTimeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@ComponentScan(basePackages = {"com.example.analytics"})
@PropertySource(value = "classpath:application.yml", ignoreResourceNotFound = false)
@Import({DatabaseConnectionConfiguration.class})
public class MainConfiguration extends Neo4jConfiguration {
@Autowired
private StringToLocalDateTimeConverter stringToLocalDateTimeConverter;
@Autowired
private LocalDateTimeToStringConverter localDateTimeToStringConverter;
private static final Logger LOG = LoggerFactory.getLogger(MainConfiguration.class);
public MainConfiguration() {
setBasePackage("com.example.analytics.model");
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
@DependsOn({ "stringToLocalDateTimeConverter", "localDateTimeToStringConverter" })
protected ConversionService neo4jConversionService() throws Exception {
LOG.debug("Adding custom converters to conversion service ...");
ConversionService conversionService = super.neo4jConversionService();
ConverterRegistry registry = (ConverterRegistry) conversionService;
registry.addConverter(stringToLocalDateTimeConverter);
registry.addConverter(localDateTimeToStringConverter);
LOG.debug("Addition of Converter from LocalDateTime to String and vice-versa has been completed!");
return conversionService;
}
}
使用存储库的服务片段
public Page<Person> findPersonsWithSeveralAcmeIdentitites(final Pageable pageable) {
LOG.debug("Fetching persons with several ACME identities");
if(pageable != null)
return personRepository.findOnesWithSeveralAcmeIdentities(pageable);
else
return personRepository.findOnesWithSeveralAcmeIdentities(getDefaultPageable());
}
然后您必须遍历您的节点并在每个节点上调用:
neo4jTemplate.postEntityCreation(node, MyEntity.class);
或者这也应该有效:
MATCH (n:User) set n:_User;
等等
我认为你过度 Java8 方法引用了!!!只需使用普通的旧 new
。否则您的代码将无法继续维护。
public static Pageable getDefaultPageable() {
return new PageRequest(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE);
}