I'm doing a REST API that consumes data from a mySQL database and returns it in JSON format.
I use hibernate, so I have object entities "connected" to the database with their respective notations @Entity, Id etc. and these are returned perfectly.
But apart, I have objects that are formed from queries but I do not want to store them in the database, so they do not have any notations or a table in the database.
Problem? These objects are returned in JSON in the following way (Those that do not have notations):
[
[
4,
"media",
"status",
1497736800000,
1503180000000
],
[
5,
"alta",
"status",
1497736800000,
1503180000000
]
]
When the others are returned like this, in "key" format: value:
[
{
"id_team": 1,
"date": 1497887607000,
"level": 0,
"id_project": 1
},
{
"id_team": 1,
"date": 1497987784000,
"level": 0,
"id_project": 1
},
]
The method that returns the "incorrect" form is this:
public List<TechnicalDependency> getUserTecDep(int id_project, int id_factory, int id_team, String id_user)
{
return (List<TechnicalDependency>)manager.createQuery("SELECT id_task, priority, status, init_date, duedate "
+ "FROM Task "
+ "WHERE assignee = '"+ id_user +"'").getResultList();
}
The method that returns values in JSON as I wish is this:
public List<Team_Level> getTeamLevels(int id_team, int id_project, String date1, String date2)
{
return (List<Team_Level>) manager.createQuery(
"FROM Team_Level"
+ "WHERE date >= '"+date1+"' "
+ "AND date <= '"+date2+"' "
+ "AND id_team ='"+id_team+"' "
+ "AND id_project = "+ id_project).getResultList();
}
The methods are the same, so the error I understand is in the way of mapping to JSON. The problem is that later I can not (or do not know) map it back to object if I do not have the key-value.