mysql - JSON data not include entity -
i developing restful api.i have 2 entities. user , userrole. when send request api user, gives me
{ "id": 11, "name": "mert", "username": "zbrave", "password": "$2a$10$n0eqniuikwcy9etq1rdau.xeelcyeo7kukkfoinisk/9f7gw6eb0w", "email": "asd", "photo": "", "enabled": true, "roles": [ { "id": 1, "role": "admin" } ], "online": false }
when send request userrole, returns
[ { "id": 1, "role": "admin" } ]
but in userrole entity, there user entity. json not return user entity.
public class userrole implements serializable { ... @manytoone(targetentity = user.class, cascade = cascadetype.all, fetch = fetchtype.eager) @joincolumn(name = "user_id") @jsonbackreference private user user; ... } public class user implements serializable { ... @onetomany(targetentity = userrole.class, mappedby = "user", cascade = cascadetype.all, fetch = fetchtype.eager) @jsonmanagedreference private list<userrole> roles; .. }
i can user roles user entity's json. not reverse. there wrong things ? help.
you using @jsonbackreference/ @jsonmanagedreference yeah expected behavior , side in jsonbackreference wont serialized, necessary avoid infinite recursion since jackson keep serializing each side of relation reference in other side, why need skip serializing 1 side
there nice solution this, mentioned in link , idea generate reference parent entity, if serializing user, have user->role->[reference user instead of user], need annotate entities @jsonidentityinfo
@jsonidentityinfo(generator=objectidgenerators.uuidgenerator.class, property="@id") public class userrole implements serializable { @jsonidentityinfo(generator=objectidgenerators.uuidgenerator.class, property="@id") public class user implements serializable {
so , remove @jsonbackreference/ @jsonmanagedreference
Comments
Post a Comment