Search code examples
jsonspring-bootspring-data-neo4j-4request-mapping

NodeEntity wont persist


I have the following NodeEntity:

@NodeEntity(label = "Book")
public class Book{
  private Long id;
  private String content;

  @Relationship(direction = Relationship.OUTGOING, type="WRITTEN_BY")
  private User author;
}

where User is

@NodeEntity(label = "User)
public class User{
  private Long id;
  private String username;
}

and the BookRepository

@Repository
public interface BookRepository extends GraphRepository<Book> {
}

I've build a simple Rest-Controller to store a Book in the DB.

@Controller
@ResponseBody
@RequestMapping(path = "/books", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@Transactional
public class BookController {

  @Autowired
  private BookRepository bookRepo;

  @RequestMapping(method = RequestMethod.POST)
  public Book createBook(@RequestBody Book book) {
    return bookRepo.save(book);
  }

When I now POST a Book the JSON

{
  "content":"Test content",
  "author":{
    "username":"Test-Username"
  }
}

to the controller, two things happen that confuse me: First, the author in the book-object is null, although both the Book and the User have their default constructor. Secoundly: The Book doesn't get persisted. There is no error, I just get the same book-object returned (with the author still null) but still with a null id. Querying MATCH n RETURN n on the neo4j client also yields nothing.

I tried removing the User object from the `Book, thinking the fault was there, but I still get the same behavior. What am I doing wrong?


Solution

  • Sometimes talking about a problem alone solves it... I've forgotten to put the package that contained the Book into the

    @Bean
    public SessionFactory getSessionFactory() {
      return new SessionFactory(/*Package of Book should have been here*/);
    }
    

    in the Application