SpringBoot + Hibernate + Restful:格式响应 [英] SpringBoot+Hibernate+Restful : format response
问题描述
我是Hibernate的新手,并试图创建一个将返回当前位置的API。
因此在DaoImpl中,我写了一个查询
public List< Location> getCurrentLocation(){
return sessionFactory.getCurrentSession()。createQuery(select l.employee.id,max(l.locationTime)as currentTime,l.longtitude,l.latitude,l.date from Location l group by l.employee.id)。list();
在控制器中
@RequestMapping(value =currentLocation,method = RequestMethod.GET)
public ResponseEntity< List< Location>> getCurrentLocation(){
List< Location>位置;
尝试{
location = locationService.getCurrentLocation();
} catch(Exception e){
System.out.println(e.getMessage());
返回新的ResponseEntity< List< Location>>(HttpStatus.NO_CONTENT);
}
返回新的ResponseEntity< List< Location>>(location,HttpStatus.OK);
}
当我调用API时,我收到了这个响应
[[11,07:30:00,106.634756,10.826307,2017-11-23],[15,07:00:00 ,106.632142,10.826456,2017-11-24]]
我只想问为什么我不能获取属性名称。我仍然不明白。
是否有任何人可以解释这一点,以及如何获得属性名称
,例如 ['employeename':11,'time':07:30:00 ,'longitude':106.634756,'latitude':10.826307,'workday':2017-11-23]
请帮助我
简单地说:您使用像 l.employee.id
这样的投影。
对于这个查询
或 Hibernate返回 从位置loc选择loc <$ p
来自Location
(相同的简短版本)
List< Location>
,但是当你开始使用投影时,Hibernate会返回 List< Object []>
。
list()
返回一个简单的 List
(非泛型),所以你有一个未经检查的转换
List< Object []> - >你可以做什么
$ b
-
使用变换器将查询结果转换为
Map
并返回Map
作为终端的结果。
-
添加类
LocationDto
并将结果映射到DTO
使用其他转换器。 使用some.package.LocaitionDto $> c $ c>构造函数直接在查询中(请不要忘记一个包,请)( hql中的new关键字是如何工作的?)。
别忘了请为每个投影添加别名。
java.lang.ClassCastException:[Ljava.lang.Object;无法转换为className
I am newbie with Hibernate and trying to create an API which will return current position. So in DaoImpl I write a query
public List<Location> getCurrentLocation() {
return sessionFactory.getCurrentSession().createQuery("select l.employee.id, max(l.locationTime) as currentTime, l.longtitude , l.latitude,l.date from Location l group by l.employee.id").list();
}
In controller
@RequestMapping(value = "currentLocation", method = RequestMethod.GET)
public ResponseEntity<List<Location>> getCurrentLocation() {
List<Location> location;
try {
location = locationService.getCurrentLocation();
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<List<Location>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Location>>(location, HttpStatus.OK);
}
and when I call that API I receive this response
[[11,"07:30:00",106.634756,10.826307,"2017-11-23"],[15,"07:00:00",106.632142,10.826456,"2017-11-24"]]
I just want to ask why I can't get the attribute name. I still don't get it.
Is there any one can explain this, and How can I get the attribute name
for example ['employeename':11,'time':"07:30:00",'longtitude':106.634756,'latitude':10.826307,'workday':"2017-11-23"]
Please help me
That's simply: you use projections like l.employee.id
.
For this query
select loc from Location loc
or from Location
(the same short version)
Hibernate returns List<Location>
, but when you begin to use projections, Hibernate returns List<Object[]>
. That list you have as a response.
list()
returns a simply List
(not generic), so you have an unchecked conversion
List<Object[]> -> List<Location>
What can you do
Convert query result to a
Map
using transformer and return thatMap
as a result from the endpoint.Add class
LocationDto
and map result to the list ofDTO
using other transformer.Use a
some.package.LocaitionDto
constructor directly in the query (don't forget a package, please) (how does the new keyword in hql work?).
Don't forget to add alias to each projection, please.
Spring Hibernate get selected columns
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to className
How to transform a flat result set using Hibernate
这篇关于SpringBoot + Hibernate + Restful:格式响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!