2

I have simple 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 ?

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

It works if MyClass is an EntityBean :-(

1 Comment

Unless MyClass.class is annoted with @Entity it will not work
0

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....
}

1 Comment

If you can't make it an entity I think this is your only route. Without bringing in another framework as proposed in the other answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.