sql - To get Map as result set in Spring boot JPA -
i need sql native query results map alias name key , column value value in spring boot jpa example: query "select u.first_name firstname, u.last_name lastname user u" returns 2 rows. entity manager in jpa should returns, [{ "firstname": "xxx", "lastname": "yyy" }, { "firstname": "aaa", "lastname": "bbb" }]
you can create repository extending pojo class , formulate query method give desired output
users.java
@entity @table(name = "users") @entitylisteners(usersentitylistener.class) @namedqueries({ @namedquery(name = "user.findall", query = "select u user u")}) public class user extends domain implements serializable { private static final long serialversionuid = 1l; @basic(optional = false) @column(name = "first_name", nullable = false) private string firstname; @basic(optional = false) @column(name = "last_name", nullable = false) private string lastname; @column(name = "middle_name") private string middlename; @basic(optional = false) @column(name = "gender", nullable = false) private string gender; @basic(optional = false) @column(name = "mobile", nullable = false) private string mobile; @basic(optional = false) @column(name = "email", nullable = false) private string email; @basic(optional = false) @column(name = "username", nullable = false) private string username; @basic(optional = false) @column(name = "password", nullable = false) private string password; @column(name = "url") private string url; public user() { } //*************getters , setters******************/ }
userrepository.java
@repository @repositoryrestresource public interface userrepository extends jparepository<user,integer> { @query("select u.firstname,u.lastname users u ") list<buyingrequirement> find(); }
now can use method directly as:
@restcontroller @requestmapping("/api/v1") public class xyz{ @autowired userrepository ur; @getmapping(value = "/user") public list<users> getnameandlastname(){ list user= ur.find(); return user; } }
this give required result list in key value pair
Comments
Post a Comment