Friday, 15 March 2013

spring data rest - After successfully adding child entity with parent reference, child does not show under parent resource -



spring data rest - After successfully adding child entity with parent reference, child does not show under parent resource -

i have 2 entities, shelf , book. shelf can have multiple books (the relationship bi-directional). i've exposed both of these jparepositories.

here's issue:

i create shelf posting { "name":"sci-fi" } /shelves.(success) i create book shelf posting { "name":"mybook", "shelf":"localhost:8080/shelves/1" } /books. (success) when book created @ /books/1, has right link parent shelf. but when go shelves/1/books, empty result, { }!

any ideas might missing?

right i've constructed workaround explicitly adding book shelf in beforecreate event, seems should totally unnecessary. (it prepare problem, however.)

@handlebeforecreate(book.class) public void handlecreate(book book) { // why necessary? book.getshelf().getbooks().add(book); }

here entity classes:

@entity public class book { @id @generatedvalue(strategy = generationtype.auto) private long id; private string name; @manytoone private shelf shelf; public shelf getshelf() { homecoming shelf; } public void setshelf(shelf shelf) { this.shelf = shelf; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashcode()); homecoming result; } @override public boolean equals(object obj) { if (this == obj) homecoming true; if (obj == null) homecoming false; if (getclass() != obj.getclass()) homecoming false; book other = (book) obj; if (name == null) { if (other.name != null) homecoming false; } else if (!name.equals(other.name)) homecoming false; homecoming true; } } @entity public class shelf { @id @generatedvalue(strategy = generationtype.auto) private long id; private string name; @onetomany private list<book> books = new arraylist<book>(); public list<book> getbooks() { homecoming books; } public void setbooks(list<book> books) { this.books = books; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashcode()); homecoming result; } @override public boolean equals(object obj) { if (this == obj) homecoming true; if (obj == null) homecoming false; if (getclass() != obj.getclass()) homecoming false; shelf other = (shelf) obj; if (name == null) { if (other.name != null) homecoming false; } else if (!name.equals(other.name)) homecoming false; homecoming true; } }

i'm using spring boot 1.1.8.

in self entity, add together mappedby = "self" property @onetomany annotation in books:

@onetomany(mappedby = "self") private list<book> books = new arraylist<book>();

this auto populate list of books books reference self matches.

spring-data-rest

No comments:

Post a Comment