1

i try to play around with spring-boot and spring-data-neo4j. Sadly i get the following exception if i try to save a node:

org.springframework.data.mapping.model.MappingException: Unknown persistent entity de.hilbert.Stock

My Code looks like the following:

my 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>

...

my configuration:

@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();
    }
}

my repository:

@Repository
public interface StockRepository extends GraphRepository<Stock>{}

my entity:

@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;
    }
}

my "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!";
    }
}

The exception is thrown at "stockRepository.save(stock);".

I have tried some solutions i found:

https://github.com/spring-projects/spring-data-neo4j/issues/161 Unknown persistent entity Error" after upgrade to 3.0.1.Release

But nothing helped. Has anybody else an idea or see the point where are im blind?

1 Answer 1

3

It looks good if your entity has the right package (not shown).

Can you try to use 3 different packages for entity, controller and repository?

Note:

  • Your Stock entity doesn't have an empty constructor.
  • you don't need the Neo4jMappingContext(Impl) bean
1
  • Hi michael and thanks for your fast reaction. Stock is in the correct packege: de.hilbert.Stock. i Puttet now the controller, the repository and the entity in separate sub-packages as shown: de.hilbert.controller.HelloController, de.hilbert.repositories.StockRepository and de.hilbert.entities.Stock. The result is the same. Further i add a empty constructor to Stock ... no changes. BUT NOW: I removed Neo4jMappingContext and now it works! Thank you a lot michael!
    – hilbert
    Commented Nov 2, 2014 at 16:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.