Skip to content

Instantly share code, notes, and snippets.

@EliaRohana
Created May 24, 2016 06:54
Show Gist options
  • Select an option

  • Save EliaRohana/2abb9471a3852a871d5c3387d982117f to your computer and use it in GitHub Desktop.

Select an option

Save EliaRohana/2abb9471a3852a871d5c3387d982117f to your computer and use it in GitHub Desktop.
JPA 2.1 Result Set Mapping
<?xml version="1.0"?>
<entity-mappings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_1.xsd"
version="2.1">
<sql-result-set-mapping name="userTodoDetail">
<constructor-result target-class="main.java.example.model.UserToTodoDesc">
<column name="name"/>
<column name="description"/>
<column name="summary"/>
</constructor-result>
</sql-result-set-mapping>
<named-native-query name="userTodoDetailQuery" result-set-mapping="userTodoDetail">
<query>select u.name,t.description, t.summary from t_user u join todo t on u.id = t.user_id</query>
</named-native-query>
</entity-mappings>
List<UserToTodoDesc> userTodos = userRepository.findUserTodos();
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
*
* @return all users details in a list of UserToTodoDesc
* @see UserToTodoDesc
* @see <a href="http://google.com">http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html</a>
* @see <a href="http://google.com">https://eclipse.org/eclipselink/api/2.1/javax/persistence/SqlResultSetMapping.html</a>
*/
//this is an example of using SqlResultSetMapping a JPA 2.1 feature
@Query(name = "userTodoDetailQuery", nativeQuery = true)
List<UserToTodoDesc> findUserTodos();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment