当我使用Spring Data Neo4j(SDN)查找节点实体时,我遇到了一些奇怪的行为.如果我使用GraphRepository.findOne(long),它将返回具有该标识符的实体,即使该实体的类型不同.
这就是我(非常)简化的实体结构:
@NodeEntity
protected abstract class BaseEntity {
@GraphId
private Long id;
@JsonIgnore
@RelatedTo(type = RelationType.ENTITY_AUDIT)
private Audit audit;
}
@NodeEntity
public final class Person extends BaseEntity {
@Indexed(indexType = IndexType.FULLTEXT)
private String firstName;
@Indexed(indexType = IndexType.FULLTEXT)
private String lastName;
}
@NodeEntity
public class Audit extends BaseEntity {
@RelatedTo(type = RelationType.ENTITY_AUDIT, direction = Direction.INCOMING)
private BaseEntity parent;
private Long date;
private String user;
}
Run Code Online (Sandbox Code Playgroud)
对于每种实体类型,我都创建了这样的存储库:
@Repository
public interface PersonRepository extends GraphRepository<Person> {}
@Repository
public interface AuditRepository extends GraphRepository<Audit> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Neo4j配置一个@configuration类的Spring数据,但我找不到任何关于如何正确执行此操作的参考资料,而且我在下一个问题后遇到了一个问题.以下是我到目前为止拼凑的内容:
package reservation.configuration;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.repository.GraphRepositoryFactory;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext;
@Configuration
@EnableNeo4jRepositories("reservation.repository.neo4j")
public class Neo4jConfig {
private static final String STORE_ID = "helloworld";
@Bean
public GraphDatabaseService graphDatabaseService() {
return new EmbeddedGraphDatabase(STORE_ID);
}
@Bean
public Neo4jTemplate neo4jTemplate() {
return new Neo4jTemplate(graphDatabaseService());
}
@Bean
public GraphRepositoryFactory graphRepositoryFactory() {
return new GraphRepositoryFactory(neo4jTemplate(), neo4jMappingContext());
}
@Bean
public Neo4jMappingContext neo4jMappingContext() {
return new Neo4jMappingContext();
}
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序时的例外情况如下.有什么建议?
java.lang.IllegalStateException: No persistence exception translators found in …Run Code Online (Sandbox Code Playgroud) 我有一个已与嵌入式Neo4j 1.8.2以及Web管理员界面一起运行的项目。
现在,我更新了该项目以使其与最新的Neo4j 2.0.1一起运行。尽管在此过程中遇到了一些障碍(当我使用Spring Data Neo4j时),但最终一切进展顺利。
但是目前,我只能让网络管理员运行它。
任何建议将不胜感激。
这是我用于1.8版本的配置
(代码段中引用的配置类)
package com.example;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.MapConfiguration;
import org.neo4j.server.configuration.Configurator;
import org.neo4j.server.configuration.ThirdPartyJaxRsPackage;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Neo4jServerConfig implements Configurator {
private Configuration config;
public Neo4jServerConfig(Map<String, String> config) {
this.config = new MapConfiguration(config);
}
@Override
public Configuration configuration() {
return config;
}
@Override
public Map<String, String> getDatabaseTuningProperties() {
return null;
}
@Override
public Set<ThirdPartyJaxRsPackage> getThirdpartyJaxRsClasses() {
return new HashSet<>();
}
@Override
public Set<ThirdPartyJaxRsPackage> getThirdpartyJaxRsPackages() {
return new HashSet<>();
} …Run Code Online (Sandbox Code Playgroud) 使用Neo4j 1.9.9.我们运行的一些Cypher查询似乎过于缓慢.一些调查显示:
在我的硬件(MacBook Pro)上删除200k节点大约需要2-3秒,当我选择它们时:
START n=node(*) DELETE n
Run Code Online (Sandbox Code Playgroud)添加WHERE子句不会显着减慢它的速度
如果使用索引选择节点,则它具有类似的性能,例如
START n=node:__types__(className="com.e2sd.domain.Comment") DELETE n
Run Code Online (Sandbox Code Playgroud)除了在重复之前的测试时,它的速度是20倍或更慢,实际时间从80秒到几百秒不等.更奇怪的是,无论我是在同一个JVM中重复测试还是启动新程序,或者清除数据库中的所有节点并验证它是否为零节点都无关紧要.基于索引的删除在任何后续测试运行中都非常慢,直到我破坏了我的neo4j数据目录
rm -R target/neo4j-test/
Run Code Online (Sandbox Code Playgroud)我将在这里给出一些示例Scala代码.我很乐意根据需要提供更多细节.
for (j <- 1 to 3) {
log("Total nodes in database: " + inNeo4j( """ START n=node(*) RETURN COUNT(n) """).to(classOf[Int]).single)
log("Start")
inNeo4j(""" CREATE (x) WITH x FOREACH(i IN RANGE(1, 200000, 1) : CREATE ({__type__: "com.e2sd.domain.Comment"})) """)
rebuildTypesIndex()
log("Created lots of nodes")
val x = inNeo4j(
"""
START n=node:__types__(className="com.e2sd.domain.Comment")
DELETE n
RETURN COUNT(n)
""").to(classOf[Int]).single
log("Deleted x nodes: " + …Run Code Online (Sandbox Code Playgroud) 我使用DynamicProperties春数据的Neo4j 3.x中的 我在Spring Data Neo4j 4.0.0.M1(SDN4)中缺少这个类.我在SDN4中有一个新概念来存储动态属性值吗?
甲DynamicProperties上的属性@NodeEntity存储其所有属性动态底层节点本身上.
DynamicProperties成员的键/值对存储在节点上,其键前缀为DelegatingFieldAccessorFactory#getNeo4jPropertyName(Field)返回的属性名称.
NodeEntity
class Person {
String name;
DynamicProperties personalProperties = new DynamicPropertiesContainer();
}
Person p = new Person();
p.persist();
p.personalProperties.setProperty("ZIP", 8000);
p.personalProperties.setProperty("City", "Zuerich");
Run Code Online (Sandbox Code Playgroud)
导致具有以下属性的节点:
"personalProperties-ZIP" => 8000
"personalProperties-City" => "Zuerich"
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Spring Data Neo4j 3.3.0和Play 2.3.8使用tuxburner插件并使用neo4j 2.1.7 DB远程工作.
我看到新的spring数据版本支持neo4j 2.2,我想升级到它.我将依赖项更改为新版本,并收到以下错误:
BeanCreationException: Error creating bean with name 'restNeo4jConfig': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.data.neo4j.config.Neo4jConfiguration.setGraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService);
nested exception is java.lang.NoClassDefFoundError: org/neo4j/kernel/TransactionBuilder
Run Code Online (Sandbox Code Playgroud)
这似乎是一个老问题,应该在3.3.1中解决,因为neo4j 2.2改变了很多内部API,这个类不再存在.
也许我使用Spring Data的方式不再正确,需要更改.
我创建了一个示例播放应用程序来显示问题:https: //github.com/OlympusTeam/Olympus
我有一个我想用Neo4j存储的树数据结构.
父节点:CodeSet始终是树的根节点和子节点:Node,子节点本身可以具有相同类型的子节点.它们的关系类型:SUBTREE_OF如下:
父节点以红色显示,其本身具有以绿色显示的父节点.
一旦父节点和子节点有一些公共数据,我就创建了一个抽象类:
public abstract class AbstractNode {
private Long id;
@NotEmpty
private String code;
@Relationship(type = "SUBTREE_OF", direction = Relationship.INCOMING)
private Set<Node> children;
<getters & setters omitted>
}
Run Code Online (Sandbox Code Playgroud)
父节点的类:
public class CodeSet extends AbstractNode {
@Relationship(type = "SUBTREE_OF", direction = Relationship.OUTGOING)
private Application parent;
<getters and setters omitted>
}
Run Code Online (Sandbox Code Playgroud)
子节点的类:
public class Node extends AbstractNode {
@NotEmpty
private String description;
@NotEmpty
private String type;
@NotEmpty
private String name;
@NotNull
@Relationship(type = "SUBTREE_OF", …Run Code Online (Sandbox Code Playgroud) 我正在尝试连接两个数据源,MySQL并且Neo4j.我尝试了这个例子,但我有不同版本的依赖项.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>easy-notes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>easy-notes</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<neo4j-ogm.version>3.0.0</neo4j-ogm.version>
<spring-data-releasetrain.version>Kay-RELEASE</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!-- -->
<dependency> …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个Spring Data Neo4j 5.0.3 REST API应用程序,该应用程序与Neo4j 3.3.1因果集群连接,该集群包含3个核心节点(1个领导者和2个粉丝).无论好坏,我们还使用session.queryla SQL预处理语句向数据库提交了大量自定义cypher查询.当我们这样做时,我们注意到我们提交的自定义Cypher几乎都没有session.query被发送给任何只读的关注者.
我已经破解了代码并注意到,在Neo4jSession该query方法中,该方法总是创建一个带有类型的事务READ_WRITE.有没有办法解决这个问题,以便我们的查询正确分布在我们的集群中?
我也尝试用适当的方法标记,@Transactional(readOnly = true)但它似乎不起作用.当我进入时Neo4jTransactionManager,我在第218行看到以下内容:
private Transaction.Type getTransactionType(TransactionDefinition definition, Neo4jTransactionObject txObject) {
Transaction.Type type;
if (definition.isReadOnly() && txObject.isNewSessionHolder()) {
type = Transaction.Type.READ_ONLY;
} else if (txObject.transactionData != null) {
type = txObject.transactionData.type();
} else {
type = Transaction.Type.READ_WRITE;
}
return type;
}
Run Code Online (Sandbox Code Playgroud)
isNewSessionHolder第一个分支中的第二个条件是什么意思?在单个HTTP API调用的上下文中,我们至少调用数据库2次,因此通过第二个查询,我相信这个条件总是返回false.
鉴于这些观察结果,是否有任何简单的方法可以使我的查询被视为只读?
我正在从Java切换到Kotlin,这是一个简单的项目,但我正在解决一个转换问题。我在数据库上:
CREATE (:Meeting {on: '2018-10-09', location: "Oracle's offices"})
Run Code Online (Sandbox Code Playgroud)
以及在kotlin中(使用SpringBoot + Spring Data neo4J)
@NodeEntity
data class Meeting(
@Id @GeneratedValue var id: Long?,
@DateString("yyyy-MM-dd") var on: LocalDate?,
var location: String?,
@Relationship(value = "ON") var topics: List<Topic> = ArrayList(),
@Relationship(value = "IN", direction = Relationship.INCOMING) var
participants: List<Person> = ArrayList(),
@Relationship(value = "ON") var event: Event?
) : Comparable<Meeting> {
override fun compareTo(other: Meeting): Int {
return this.on!!.compareTo(other.on)
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我执行一个简单的MeetingRepository.findAll()时,我遇到了异常
java.time.format.DateTimeParseException: Text '2018-10-09' could not be parsed at index 4
at …Run Code Online (Sandbox Code Playgroud)