migrating jdbc queries to jpa native queries

View: New views
4 Messages — Rating Filter:   Alert me  

migrating jdbc queries to jpa native queries

by ymajoros :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 Hi,

 Is there a simple way to migrate code like this to jpa/eclipselink?

// get connection, statement
 ResultSet rs = statement.executeQuery(maCommande);
  while (rs.next()) {
                    String code = rs.getString("code_etude");
   // ...
  }

 In order to use jpa-provided connection pools, to have all statements
closed in all cases without try-finally blocks, ...

 Is there a simple solution? Native queries return an array with unnamed
columns, we really want to use methods like
rs.getString("my_column_name"). We can't use SqlResultSetMapping, as the
queries are too complex to be mapped.

 Yannick
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: migrating jdbc queries to jpa native queries

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

EclipseLink provides a Query hint that allows the results to be returned as a List of Maps (DatabaseRecords) instead of a List of Object[].

"eclipselink.result-type"="Map"

See, org.eclipse.persistence.ResultType , QueryHints

ymajoros wrote:
 Hi,

 Is there a simple way to migrate code like this to jpa/eclipselink?

// get connection, statement
 ResultSet rs = statement.executeQuery(maCommande);
  while (rs.next()) {
                    String code = rs.getString("code_etude");
   // ...
  }

 In order to use jpa-provided connection pools, to have all statements
closed in all cases without try-finally blocks, ...

 Is there a simple solution? Native queries return an array with unnamed
columns, we really want to use methods like
rs.getString("my_column_name"). We can't use SqlResultSetMapping, as the
queries are too complex to be mapped.

 Yannick

Re: migrating jdbc queries to jpa native queries

by ymajoros :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

James Sutherland a écrit :
> EclipseLink provides a Query hint that allows the results to be returned as a
> List of Maps (DatabaseRecords) instead of a List of Object[].
>
> "eclipselink.result-type"="Map"
>
> See, org.eclipse.persistence.ResultType , QueryHints
>
>  
So, I guess I need to write this:

        Query query = entityManager.createNativeQuery(sql);
        query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
        List<Map> results = query.getResultList();
        for (Map result : results) {
         }

What will the result types be? Is everything a String, then?
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: migrating jdbc queries to jpa native queries

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The key will be a String of the column name (actually a DatabaseField, but String works as well).  The value will be Object, or whatever type the database returns for the column.

ymajoros wrote:
James Sutherland a écrit :
> EclipseLink provides a Query hint that allows the results to be returned as a
> List of Maps (DatabaseRecords) instead of a List of Object[].
>
> "eclipselink.result-type"="Map"
>
> See, org.eclipse.persistence.ResultType , QueryHints
>
>  
So, I guess I need to write this:

        Query query = entityManager.createNativeQuery(sql);
        query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
        List<Map> results = query.getResultList();
        for (Map result : results) {
         }

What will the result types be? Is everything a String, then?