Reputation: 681
I'm encountering a problem about the way jackson serialize an object.
I have a classpublic class Solution {
private int id;
private String description;
private User user;
private Date createdDate;
private Date lastModifiedDate;
private int isActive;
}
I only wanted to select id and description from database:
public List<Solution> getSolutionForSearching() {
String hql = "SELECT id,description FROM Solution";
Session session = solutionDao.getSession();
Query query =session.createQuery(hql);
return query.list();
}
And I expected the response is an array of objects
[ { id: 35, description: "12" }, { id: 36, description: "1a" }]
but the obtained result is an array of arrays
[ [35, "12" ], [ 36, "1a" ]]
How to serialize the response data to match the requirements?
Upvotes: 1
Views: 232
Reputation: 9162
You hql query does not return instances of type Solution
. Instead it returns int, String pairs. So the Jackson cannot map them to the correct form.
You should either return complete objects Solution
from the database, or you map the results of the query to Solution
object before you pass it to Jackson.
Currently your Solution
object is very small, so you don't have to worry about the performance. Just modify the hql query like this:
String hql = "SELECT solution FROM Solution solution";
Upvotes: 3