gpt4 book ai didi

java - 通过@Repository 保存时出现 neo4j 空指针异常

转载 作者:行者123 更新时间:2023-11-30 08:34:19 25 4
gpt4 key购买 nike

The sample project i'm working with can be found attached here - Spring Jira

这是我的配置

@EnableNeo4jRepositories(basePackages = "com.graph.repository")
public class DBConfig extends Neo4jConfiguration{
@Value("${neo4j.location}")
private String neo4jDatabaseLocation;

@Override
public SessionFactory getSessionFactory() {
return new SessionFactory(getConfiguration(), "com.graph.entity");
}

@Bean
public Configuration getConfiguration() {
Configuration configuration = new Configuration();
configuration.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
.setURI(neo4jDatabaseLocation);
return configuration;
}

@Bean
@Override
public Session getSession() throws Exception {
return getSessionFactory().openSession();
}
}

抽象实体

public abstract class Entity {
@GraphId
private Long id;

public Long getId() {
return id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || id == null || getClass() != o.getClass()) return false;

Entity entity = (Entity) o;

if (!id.equals(entity.id)) return false;

return true;
}

@Override
public int hashCode() {
return (id == null) ? -1 : id.hashCode();
}
}

这是我的实体

@NodeEntity(label = "Patient")
public class Patient extends Entity {
private String patientId;
private String patientName;
private String otherPatientId;
private String sex;
private String dateOfBirth;
private String patientIdIssuer;
@Relationship(type = "STUDY", direction = Relationship.UNDIRECTED)
private Set<Study> studies;

Getters and Setters...
}

Study 具有嵌套的实体/关系并且具有另一个嵌套的实体/关系。 1:N关系

这是我的仓库

@Repository
public interface PatientRepository extends GraphRepository<Patient> {
}

这是调用方法

public class Test() {
@Autowired
private PatientRepository patientRepository;

public void test() {
Patient patient = new Patient();
// set fields
patientRepository.save(patient); -> This is where I get NPE
}
}

堆栈跟踪:

Caused by: java.lang.NullPointerException: null
at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.nativeTransaction(EmbeddedDriver.java:180) ~[neo4j-ogm-embedded-driver-2.0.4.jar:na]
at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.newTransaction(EmbeddedDriver.java:148) ~[neo4j-ogm-embedded-driver-2.0.4.jar:na]
at org.neo4j.ogm.session.transaction.DefaultTransactionManager.openTransaction(DefaultTransactionManager.java:57) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.TransactionsDelegate.beginTransaction(TransactionsDelegate.java:37) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.Neo4jSession.beginTransaction(Neo4jSession.java:441) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.request.RequestExecutor.executeSave(RequestExecutor.java:84) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:75) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:44) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:425) ~[neo4j-ogm-core-2.0.4.jar:na]

有人可以告诉我我做错了什么吗??

注意:我之前使用带有 GraphDatabaseService 的 sdn.3.x 进行了此操作

最佳答案

看起来唯一缺少的是 Neo4jConfiguration 类上的 @Configuration 注释:

@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = "com.seyfert.matrix.graph.repository")
public class DBConfig extends Neo4jConfiguration{
...

关于java - 通过@Repository 保存时出现 neo4j 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com