1
我尝试使用spring-boot和spring-data-neo4j来玩。可悲的是,我得到下面的异常,如果我试图保存节点:org.springframework.data.mapping.model.MappingException:保存neo4j节点的未知持久化实体
org.springframework.data.mapping.model.MappingException: Unknown persistent entity de.hilbert.Stock
我的代码如下所示:
我的pom.xml: ...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>1.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.1.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.1.8.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>1.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>de.hilbert.Application</start-class>
<java.version>1.8</java.version>
</properties>
...
我的配置:
@Configuration
@ComponentScan(basePackages = {"de.hilbert"})
@EnableAutoConfiguration
@EnableNeo4jRepositories(basePackages = {"de.hilbert"})
public class Application extends Neo4jConfiguration {
public Application() {
setBasePackage("de.hilbert");
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("target/neo4j.db");
}
public Neo4jMappingContext neo4jMappingContext() {
return new Neo4jMappingContext();
}
}
我的存储库:
@Repository
public interface StockRepository extends GraphRepository<Stock>{}
我的实体:
@NodeEntity
public class Stock {
@GraphId
private Long id;
@Indexed(indexType = IndexType.FULLTEXT, indexName = "symbolIdx", unique = true)
private String symbol;
private String price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
我 “buissness” -code:
@RestController
public class HelloController {
@Autowired
StockRepository stockRepository;
@Autowired
GraphDatabase graphDatabase;
@RequestMapping("/")
public String index() throws IOException {
Transaction tx = graphDatabase.beginTx();
try {
Stock stock = new Stock();
stock.setSymbol("SYMBOL");
stock.setPrice("1");
stockRepository.save(stock);
tx.success();
} finally {
tx.close();
}
return "Greetings from Spring Boot!";
}
}
异常被抛出在 “stockRepository.save(股票);”。
我已经尝试了一些解决方案,我发现:
https://github.com/spring-projects/spring-data-neo4j/issues/161 Unknown persistent entity Error" after upgrade to 3.0.1.Release
但没有任何帮助。有其他人有想法或看到我盲目的地步吗?
喜迈克尔,感谢您的快速反应。 – hilbert 2014-11-02 15:56:23
嗨迈克尔,感谢你的快速反应。 股票包装正确:'de.hilbert.Stock'。我现在将控制器,存储库和实体放在单独的子包中,如图所示:'de.hilbert.controller.HelloController','de.hilbert.repositories.StockRepository'和'de.hilbert.entities.Stock'。结果是一样的。此外,我添加一个空的构造函数Stock ...没有变化。 但现在: 我删除了 'Neo4jMappingContext' 现在它工作! 谢谢你很多michael! – hilbert 2014-11-02 16:09:40