java - Spring Data Rest projection for Embedded Entities -
lets assume have following entities:
@entity public class registration { @manytoone private student student; //getters, setters } @entity public class student { private string id; private string username; private string name; private string surname; //getters, setters } @projection(name="minimal", types = {registration.class, student.class}) public interface registrationprojection { string getusername(); student getstudent(); }
i'm trying create following json representation, when use http://localhost:8080/api/registrations?projection=minimal
dont need user data come along:
{ "_links": { "self": { "href": "http://localhost:8080/api/registrations{?page,size,sort,projection}", } }, "_embedded": { "registrations": [ { "student": { "username": "user1" } } ], "_links": { "self": { "href": "http://localhost:8080/api/registrations/1{?projection}", } } } }
however projection have created exception(it works without getusername() statement. have defined interface in wrong way...but correct way this?
edit: exception following
invalid property 'username' of bean class [com.test.registration]: not find field property during fallback access! (through reference chain: org.springframework.hateoas.pagedresources["_embedded"] ->java.util.unmodifiablemap["registrations"]->java.util.arraylist[0]->org.springframework.data.rest.webmvc.json.["content"] ->$proxy119["username"])</div></body></html>
as exception said username
not member of registration
entity. why failed. think understand that.
first @ if want expose projection registration
should first change following line:
@projection(name="minimal", types = {registration.class, student.class})
by setting types = {registration.class, student.class}
asked spring data rest apply projection on registration.class
, student.class
. , can cause issue because depending of type should not have same member/methods. in practice types
must share common ancestor.
otherwise main problem should try virtual projections following thing (i didn't try on sample should work, hope):
@projection(name="minimal", types = {registration.class}) public interface registrationprojection { @value("#{target.getstudent().getusername()}") string getusername(); }
i don't know if familiar spel previous code create getusername
this.getstudent().getusername()
because target
bound on instance object (see documentation).
Comments
Post a Comment