Hibernate Native SQL
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Adding scalars
- To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, we use scalars while creating Native Queries
A quick sample. Check out the JAVA class and SQL file
1
42
43
44
45
46
47
48
49
50
51
// {
List<Object[]> domain = session.createNativeQuery("SELECT * FROM domain")
.addScalar("domain_id", IntegerType.INSTANCE).addScalar("domainName", StringType.INSTANCE).list();
domain.stream().forEach(objects -> {
Integer id = (Integer) objects[0];
String name = (String) objects[1];
System.out.println(String.format("Info: Domain[ %d, %s ]", id, name));
});
}
Enter to Rename, Shift+Enter to Preview
1
Enter to Rename, Shift+Enter to Preview
- In this case, only domain_id and domainName would be returned back, though, we have requested for * from the table.
- Also, we would still be returned with an array of Object – List<Object[]>, but now it will not use the ResultSetMetadata to determine the type of columns, but will instead explicitly get the domain_id, and domainName column as respectively a Integer, and a String from the underlying resultset.
- Adding scalars to native Queries is preferable, considering the reduced overhead on the ResultSetmetaData and a marginal improvement on the performance.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content