NullPointerException用于findAll()方法的TopLevelTransaction.markAsRollbackOnly Neo4J-Boot

时间:2014-05-05 19:59:59

标签: spring neo4j spring-boot

我正在尝试跟随Josh Long - Philip Sorst example以使用Neo4J作为db执行Angular SPA-Rest身份验证和CRUD操作(出色的工作。非常感谢你们)。但是我很早就陷入了困境,我怀疑这不是我的错。请帮助Neo4J-Angular-Spring爱好者。我的代码可以找到here,只需克隆并运行mvn spring-boot:run

就很容易

现在问题是我只为GraphRepository的findAll()方法获得以下异常。

Caused by: java.lang.NullPointerException: null
    at org.neo4j.kernel.TopLevelTransaction.markAsRollbackOnly(TopLevelTransaction.java:93)
    ... 88 common frames omitted

我会复制一些代码:

Neo4JConfig.java

@Configuration
@EnableNeo4jRepositories(basePackages = "demo.repository.neo4j")
public class Neo4JConfig extends Neo4jConfiguration {

  public Neo4JConfig() {
     setBasePackage("demo.model.neo4j");
  }

  @Bean(destroyMethod = "shutdown")
  public GraphDatabaseService graphDatabaseService() {      
     return new GraphDatabaseFactory().newEmbeddedDatabase("data/demo.db");             
  }

  @Bean
  public Neo4jTemplate neo4jTemplate() {
    return new Neo4jTemplate(graphDatabaseService());
  }
}

NewsEntry.java

@NodeEntity
public class NewsEntry {

  @GraphId
  private Long id;
  private String content;
  public NewsEntry() {}
    public NewsEntry(String b) {
     this.content = b;
    }

  public Long getId() {
      return this.id;
  }

  public String getContent() {
      return this.content;
  }

  public void setId(Long id) {
      this.id = id;
  }

  public void setContent(String content) {
      this.content = content;
  }
}

NewsEntryRepository.java

public interface NewsEntryRepository extends GraphRepository<NewsEntry> {   
}

NewsEntryController.java

@RestController
class NewsController {

 @Autowired
     private NewsEntryRepository newsEntryRepository;

 @RequestMapping("/news")
 List<NewsEntry> entries() {

 List<NewsEntry> list = new ArrayList<NewsEntry>();
 Iterable<NewsEntry> results = newsEntryRepository.findAll();
 for (NewsEntry r : results) {
   list.add(r);
 }
 return list;
  } 

  @RequestMapping(value = "/news/{id}", method = RequestMethod.DELETE)
  void remove(@PathVariable Long id) {        
      this.newsEntryRepository.delete(id);
      return;
 }

 @RequestMapping(value = "/news/{id}", method = RequestMethod.GET)
  NewsEntry entry(@PathVariable Long id) {
      return this.newsEntryRepository.findOne(id);
  }

  @RequestMapping(value = "/news/{id}", method = RequestMethod.POST)
  NewsEntry update(@PathVariable Long id, @RequestBody NewsEntry news) {
      NewsEntry old  = this.newsEntryRepository.findOne(id);
      old = news;        
      return this.newsEntryRepository.save(old);
  }

  @RequestMapping(value = "/news", method = RequestMethod.POST)
  NewsEntry add(@RequestBody NewsEntry news) {
      this.newsEntryRepository.save(new NewsEntry(news.getContent()));
      return news;
  }  
}

3 个答案:

答案 0 :(得分:2)

OK解决方案受到启发https://github.com/mstahv/bootiful-neo4j-with-vaadin/blob/master/src/main/java/org/vaadin/neo4j/AppService.java。在使用控制器中的存储库之前,他正在实现使用@Transactional注释的@Service。

答案 1 :(得分:0)

您不必覆盖:

  @Bean
  public Neo4jTemplate neo4jTemplate() {
    return new Neo4jTemplate(graphDatabaseService());
  }

您可以尝试添加注释:

@EnableTransactionManagement

到您的配置?

答案 2 :(得分:0)

有些东西告诉我,解决方案隐藏在某个地方here。我会试一试,让你知道。

确定仅当您将Spring-data-neo4j的版本更改为2.3.3.RELEASE时才有效。如果您使用的是最新版本,则会遇到与上述相同的问题。我想我应该开个问题。

但那不是解决方案,因为我想使用Neo4J Server Community 2.0.3打开图表进行可视化。另外我不明白这个解决方案beanfactories,注入而不是autowired ???。但是我会在我的github repo中为这个解决方案创建另一个分支。