Assign the result set rows with the same identifier to the Java list using Hibernate

I use Hibernate to retrieve some data from a database. The result returned by the SQL query that I use looks something like this:

SourceId | TargetId
1        | 10
1        | 11
1        | 12
2        | 13
2        | 14

Right now I have 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;
        }
}

Despite the fact that everything works fine, and I get the desired results, in some cases the result set is really huge (thousands of rows), so it takes several minutes to actually get the data. I know that Hibernate is not the best performance solution when it comes to really large result sets, but I try to avoid using raw JDBC.

targetId. , Set , 2 ,

sourceId = "1" 

, :

targetId = <"10", "11", "12">

- sourceId = "2".

- , , Hibernate? Java?

+3
2

, . u proc , , sourceId.

, .

+3

, Hibernate - , .

.

... ( )...

, postgres, mysql .....

setMaxResults setFirstResult . ( ).

JVM, (, , ).

0

Source: https://habr.com/ru/post/1796072/


All Articles