Created
February 13, 2017 07:57
-
-
Save gokman/b0b3027dcc659bf88925a12b1feb904a to your computer and use it in GitHub Desktop.
Spring Jpa - Query Result To Pojo
This file contains 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
//first prepare pojo for query | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
public class UserResponseVM implements Serializable{ | |
@JsonProperty("_id") | |
private UUID id; | |
private String name; | |
. | |
. | |
. | |
//in repository we have below query that have two columns in result | |
@Query("select u.id, u.name from User u") | |
List<Object[]> c(); | |
//in service layer execute repository query and map it to the UserResponseVM | |
public List<UserResponseVM> findUserIdAndNameOfUsers() { | |
List<UserResponseVM> userResponseVMList = queryResultMapper.mapResultToUserResponseVM(userRepository.findUserUuidAndVersionOfUsers()); | |
return userResponseVMList; | |
} | |
//our mapper | |
public List<UserResponseVM> mapResultToUserResponseVM(List<Object[]> result){ | |
List<UserResponseVM> userResponseVMList = new ArrayList<>(); | |
for(Object[] object: result){ | |
UserResponseVM userResponseVM = new UserResponseVM(); | |
userResponseVM.setId((UUID) object[0]); | |
userResponseVM.setName((Integer) object[1]); | |
userResponseVMList.add(userResponseVM); | |
} | |
return userResponseVMList; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment