0 Replies Latest reply on May 5, 2015 2:39 PM by Shahin Askari

    Can I use RowMapper pattern in JPA?

    Shahin Askari Newbie

      I am working on standardizing a legacy code base and new development to both use EntityManager for persistence.  In order to succeed, I need to provide the ability to break out into native SQL and process results manually.  JPA EntityManager almost provides this functionality through:


      em.createNativeQuery("select ... from my_table where ...")


      But what I am missing is the ability to do Spring style RowMappers.  I can see that there are a few mechanisms that are close.  I can of-course map entities using annotations, xml and so on but I need to put code into the processing of each row in the result set so this is not an option.  I can see that I can pass in SQLResultSetMapping, but as best as I can understand, this supports again only meta data mapping.  Ideally what I need is the following:


      em.createNativeQuery("select ... from my_table where ...",
        new RowMapper {
          public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
               MyObject o = new MyObject();
              o.setMyCustomProperty( rs.get...() );
              return b;


      Does the equivalent to the above exist?  Can I construct a SQLResultSetMapping which takes full control of the mapping like the above.  If not, can I at least get the data source underlying the EM so that I can wrap it using JdbcTemplate?


      DataSource ds = em.getDataSource();
      JdbcTemplate t = new JdbcTemplate(ds);
      // use RowMapper