Created
May 24, 2016 06:54
-
-
Save EliaRohana/2abb9471a3852a871d5c3387d982117f to your computer and use it in GitHub Desktop.
JPA 2.1 Result Set Mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<UserToTodoDesc> userTodos = userRepository.findUserTodos(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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