我尝试使用spring-boot和spring-data-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” - 代码:
@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(stock);”。
中抛出异常我尝试了一些解决方案:
https://github.com/spring-projects/spring-data-neo4j/issues/161 Unknown persistent entity Error" after upgrade to 3.0.1.Release
但没有任何帮助。有其他人有想法或看到我盲目的地方吗?
答案 0 :(得分:3)
如果您的实体拥有正确的包裹(未显示),那就好了。
您可以尝试为实体,控制器和存储库使用3个不同的包吗?
注意:
Stock
实体没有空构造函数。Neo4jMappingContext(Impl)
bean