-
-
Save W3SS/4acc0eec21a990420e32ec675d7c4855 to your computer and use it in GitHub Desktop.
Java JPA Native Query using Tuple and return List of Jackson JSON Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Needs Hibernate 5.2.11.Final | |
*/ | |
public List<ObjectNode> getQuery( | |
Integer anno, | |
Integer settimana) { | |
Query q = em.createNativeQuery( | |
"NATIVE SQL....",Tuple.class); | |
List<Tuple> results = q.getResultList(); | |
List<ObjectNode> json = _toJson(results); | |
return json; | |
} | |
private List<ObjectNode> _toJson(List<Tuple> results) { | |
List<ObjectNode> json = new ArrayList<ObjectNode>(); | |
ObjectMapper mapper = new ObjectMapper(); | |
for (Tuple t : results) | |
{ | |
List<TupleElement<?>> cols = t.getElements(); | |
ObjectNode one = mapper.createObjectNode(); | |
for (TupleElement col : cols) | |
{ | |
one.put(col.getAlias(), t.get(col.getAlias()).toString()); | |
} | |
json.add(one); | |
} | |
return json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment