I'm using Hibernate to retrieve some data from a database. The result set returned by the SQL query I'm using, is something like the following:
SourceId | TargetId
1 | 10
1 | 11
1 | 12
2 | 13
2 | 14
Right now I'm having something like the following class:
@NamedNativeQuery(name = "myQuery", resultSetMapping = "myMapping",
query = "select source.id id1, target.id id2
+ "from someTable source "
+ "left join associationTable association on source.id=association.sourceId "
+ "left join someTable target on association.targetId=target.id "
+ "where source.id in(?1)")
@SqlResultSetMapping(name = "myMapping",
entities = @EntityResult(entityClass = DBCalculationElement.class, fields = {
@FieldResult(name = "targetId", column = "targetId"),
@FieldResult(name = "sourceId", column = "sourceId") }))
public class MyClass {
private String sourceId;
private String targetId;
public String getSourceId() {
return sourceId;
}
public String getTargetId() {
return targetId;
}
}
Even though everything is working fine and I'm getting the results I need, in some cases the result set is really huge (thousands of rows) so it takes several minutes to actually get the data. I'm aware that Hibernate is not the best solution performance-wise when it comes to really big result sets, but I'm trying to avoid using raw JDBC.
One workaround would be to have a list of strings as targetId. This way, and using the above resultSet as an example, I would get 2 objects, one with
sourceId = "1"
which also has a list of targetIds containing the following:
targetId = <"10", "11", "12">
and something similar for the second object with sourceId="2".
Does anyone know how can I do this using Hibernate? How can I map several rows to a list in Java?
Once, i solved this problem by writing a stored procedure. u can write stored proc to retun comma separated values of target id for each source id and in your main query use distinct to get unique sourceId.
Hope, will work for u also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With