开发者

How can mapped result from SQL native query to java POJO class (no entity)

I have si开发者_开发问答mple java pojo and it's no entity.

class MyClass {
    // fields, getter, setter and etc...
}

Also I have DAO with some function for execute native SQL query (createNativeQuery)

How can mapped result from SQL native query to MyClass without @Entity ?


If the bean field names are the same as the DB table's column names, you can use Spring JDBC's org.springframework.jdbc.core.BeanPropertyRowMapper<T>.

You call org.springframework.jdbc.core.simple.SimpleJdbcOperations.queryForObject(String, RowMapper<T>, Object...)) with the BeanPropertyRowMapper object and it calls all the setters for you, using reflection.


If its JPA, I'd used: Query query = getEntityManager().createNativeQuery(sql.toString(), MyClass.class);

It works if MyClass is an EntityBean :-(


You can simply issue a query and call the getters/setters in your POJO class. Pseudo-code:

get connection
ResultSet rs = execute query
if (rs.next()) {
  setField1(rs.getString("field1"));
  etc....
}


You can use EclipseLink query redirector. The following link explains it. The author has also provided code which is pretty generic and works quite well.

http://onpersistence.blogspot.in/2010/07/eclipselink-jpa-native-constructor.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜