PDA

View Full Version : Nested and Unformated Json returned for different queries using Hibernate



sachintaware
15 Mar 2012, 9:17 PM
Hello
I am working on a extjs app with springmvc and hibernate.I am facing problem with nested json and unformated Json string for different queries I use.

I use jackson and hibernate with springmvc and to get specific records(using select) I wrote a HQL like this


public List<CandidateStatus> getcandidateStatus() {

return hibernateTemplate.find("select candstat.candidate.firstName as firstName,candstat.candidate.lastName as lastName,candstat.candidate.email as email,candstat.statusTitle as statusTitle,candstat.requirement.client.clientName as clientName,candstat.requirement.reqTitle as reqTitle from CandidateStatus as candstat inner join candstat.candidate as candidate inner join candstat.requirement as requirement");

}


selecting specific data or columns.In the controller and service layer.
Controller:


public @ResponseBody Map<String,? extends Object> view() throws Exception {

try{
//List<Candidates> candidates = candidatesService.getCandidatesList();
List<CandidateStatus> candidatestatus = candidatesService.getCandidateStatusList();
return getMap(candidatestatus);



Service:


public List<CandidateStatus> getCandidateStatusList()
{
//return candidatesDAO.getCandidates();
return candidatesDAO.getcandidateStatus();
}


But with this query the Json I get is in this format


{
"data": [
[
29,
"Vikram",
"",
"bj.vikra@gmail.com",
"Shortlisted",
"HireCraft",
"Support Engineer"
],
[
39,
"Anchal",
"Chauhan",
"ariesanchl@gmail.com",
"Scheduled",
"HireCraft",
"Support Engineer"
],
[
40,
"Vivek",
"",
"vivek.kodanca@gmail.com",
"Shortlisted",
"HireCraft",
"Support Engineer"
]
],
"total": 7668,
"success": true
}

Only values....not the standard "name"=value pair form.How can bring it to the standard form?


Otherwise,if I use this query, without a select


public List<CandidateStatus> getcandidateStatus() {
return hibernateTemplate.find("from CandidateStatus candstat inner join candstat.candidate as candidate");

}

the JSON returned is this


{
"data": [
[
{
"id": 29,
"reqid": 27,
"statusTitle": "Shortlisted"
},
{
"id": 45,
"firstName": "Vikram",
"lastName": "",
"email": "bj.vikram@gmail.com"
}
],
[
{
"id": 71,
"reqid": 33,
"statusTitle": "Pending"
},
{
"id": 45,
"firstName": "Vikram",
"lastName": "",
"email": "bj.vikram@gmail.com"
}
]
],
"total": 7668,
"success": true
}

nested and with the corresponding names.I am not getting why the first query does not return the names of the columns?

Thanks
Sach

sachintaware
16 Mar 2012, 6:13 AM
Hello
I have come some way ahead and configured my classes to get this json format



{
"data": [
{
"id": 29,
"requirement": {
"id": 27,
"client": {
"id": 12,
"clientName": "HireCraft"
},
"clientId": 12,
"reqTitle": "Support Engineer"
},
"reqid": 27,
"resid": 45,
"candidate": {
"id": 45,
"firstName": "Vikram",
"lastName": "",
"email": "bj.vikram@gmail.com"
},
"statusTitle": "Shortlisted"
}],
"success":true,
"total":7668
}

this is offcourse a nested one.
Now I referred the docs for extjs4 and learnt that the reader can handle nested json's.

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.reader.Reader (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.reader.Reader)

Can anybody help me in configuring my reader for this json returned?

Thanks
Sach

sachintaware
19 Mar 2012, 10:26 PM
Hello folks

Chramer helped me lot for getting a extjs solution for this.But,it is not helpful performance wise.

So,I would like to have a Spring-Hibernate solution of converting the fetched data into JSON and then passing it to the grid.



"data": [
{
"id": 30,
"requirement": {
"id": 27,
"client": {
"id": 13,
"clientName": "ABC"
},
"clientId": 13,
"reqTitle": "Java"
},
"reqid": 27,
"resid": 45,
"candidate": {
"id": 45,
"firstName": "Suynil",
"lastName": "Seelam",
"email": "ss.seelam@gmail.com"
},
"statusTitle": "Joined"
}],
"success":true,
"total":768
}


This is how I get it.But I want to convert it to a flat JSON file


"data": [ {
"clientName": "ABC"
"firstName": "Suynil",
"lastName": "Seelam",
"email": "ss.seelam@gmail.com",
"reqTitle": "Java"}
]success:true,
total:768
}



like this.
Can anyone help me with this?born(forum member) had suggested me but I am not able implement it to expectation.I have posted the code in the previous posts.

Thanks
Sach

sachintaware
21 Mar 2012, 10:46 PM
I achieved this using Json array and Json object packages.Not Jackson
Though I am not sure if this is the optimal solution,it works for now atleast as a workaround.;)
Please let me know if I can do something further to optmize it



JSONArray jArray = new JSONArray();
while(iterator.hasNext())
{
JSONObject candDetails = new JSONObject();
astring = iterator.next();

candDetails.put("clientName",astring.getRequirement().getClient().getClientName());
candDetails.put("firstName",astring.getCandidate().getFirstName());
candDetails.put("lastName",astring.getCandidate().getLastName());
candDetails.put("email",astring.getCandidate().getEmail());
candDetails.put("phone",astring.getCandidate().getPhone());
jArray.add(candDetails);
}


Thanks
Sach