json - Retrieving details from multiple tables using SpringMVC -
i developing shopping application, in application have table tenant , in tenant table have have column binary_id primary key in binary table in database. when making request tenant table getting tenant table fields json. have @manytoone relation binary table tenant i.e tenant can have multiple records in binary. so, while making call postman client instead of getting tenant details, need binary records related tenant json.
now getting json follows when making call http://localhost:8080/sportsmvc/rest/tenant postman client
[ { "id": 2, "binaryid": "1002", "name": "altisarena" }, { "id": 9, "binaryid": "1001", "name": "agon"
} ]
but need responce json below:
[ { "id": 2, "name": "altisarena", "listofbinary": [ { "tenant_id": 2, "location": "location1", "description": "abc" }, { "tenant_id": 2, "location": "location2", "description": "abcd" } ] }, { "id": 9, "name": "agon", "listofbinary": [ { "tenant_id": 9, "location": "location3", "description": "desc1" }, { "tenant_id": 9, "location": "location4", "description": "desc2" } ] } ]
code snippets:
tenant entity:
@entity @table(name="tenant", catalog="db_sports" ) // define named queries here @namedqueries ( { @namedquery ( name="tenantentity.countall", query="select count(x) tenantentity x" ) } ) public class tenantentity implements serializable { private static final long serialversionuid = 1l; //---------------------------------------------------------------------- // entity primary key ( based on single field ) //---------------------------------------------------------------------- @id @generatedvalue(strategy=generationtype.auto) @column(name="id", nullable=false) private integer id ; @column(name="name", nullable=false, length=300) private string name ; //---------------------------------------------------------------------- // entity links ( relationship ) //---------------------------------------------------------------------- @manytoone @joincolumn(name="binary_id", referencedcolumnname="id") private swabinaryentity swabinary ;
swa_binary entity:
@entity @table(name="swa_binary", catalog="db_sports" ) // define named queries here @namedqueries ( { @namedquery ( name="swabinaryentity.countall", query="select count(x) swabinaryentity x" ) } ) public class swabinaryentity implements serializable { private static final long serialversionuid = 1l; //---------------------------------------------------------------------- // entity primary key ( based on single field ) //---------------------------------------------------------------------- @id @column(name="id", nullable=false, length=100) private string id ; @column(name="file_location", nullable=false, length=400) private string filelocation ; @column(name="description", nullable=false, length=200) private string description ;
tenantrestcontroller:
@requestmapping( value="/tenant", method = requestmethod.get, produces = mediatype.application_json_value) @responsestatus(httpstatus.ok) @responsebody public list<tenant> findall() { return tenantservice.findall(); }
tenantserviceimpl:
@override public list<tenant> findall() { list<tenantentity> entities = tenantpersistence.loadall(); list<tenant> beans = new arraylist<tenant>(); for(tenantentity entity : entities) { beans.add(tenantservicemapper.maptenantentitytotenant(entity)); } return beans; }
tenantservicemapper:
public tenant maptenantentitytotenant(tenantentity tenantentity) { if(tenantentity == null) { return null; } //--- generic mapping tenant tenant = map(tenantentity, tenant.class); //--- link mapping ( link swabinary ) if(tenantentity.getswabinary() != null) { tenant.setbinaryid(tenantentity.getswabinary().getid()); } return tenant; }
can please to solve issue.
thanks in advance.
maybe misunderstood model, seems it's bit wrong. in need json have tenant have multiple binaries, in jpa model it's vise versa , tenant have 1 binary.
in tenantentity shouldn't ?:
@onetomany private list<swabinaryentity> swabinary
Comments
Post a Comment