通过@Repository保存时neo4j空指针异常

时间:2016-08-10 22:25:25

标签: java neo4j spring-data-neo4j-4

  

我正在使用的示例项目可以在这里找到 -   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...
}

研究具有嵌套的实体/关系,并且具有另一个嵌套的实体/关系。 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

1 个答案:

答案 0 :(得分:2)

看起来唯一缺少的是protected void Button1_Click(object sender, EventArgs e) { SqlConnection cnn = new SqlConnection(); cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString; cnn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from TableName"; cmd.Connection = cnn; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds, " TableName "); SqlCommandBuilder cb = new SqlCommandBuilder(da); DataRow drow = ds.Tables["TableName"].NewRow(); drow["id"] = TextBox1.Text; drow["Name"] = TextBox2.Text; drow["Designation"] = TextBox3.Text; drow["Mobile_No"] = TextBox4.Text; drow["Address"] = TextBox5.Text; ds.Tables["TableName "].Rows.Add(drow); da.Update(ds, " TableName "); string script = @"<script language=""javascript""> alert('Information have been Saved Successfully.......!!!!!.'); </script>;"; Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script); } 课程上的@Configuration注释:

Neo4jConfiguration