我想知道hot可以通过查询从Group返回一个对象。
我在我的存储库界面
中有这个查询@Query(value = "SELECT data, objectId, stampo, min(rendimento) as rendimento from rendimenti where objectId=:objectId "
+ "and stampo=:mold group By data, objectId, stampo order by data DESC LIMIT 0,1", nativeQuery = true)
Wrapper findByObjectAndMold(@Param("objectId") int objecId, @Param("mold") String mold);
此查询应返回单个Object(或无),在DB上运行它。 我创建了一个Wrapper对象来获取结果
public class Wrapper {
@Column(name="rendimento")
private Performance performance;
private int objectId;
@Column(name="stampo")
private String mold;
@Column(name="data")
private Date date;
//getters and setters
}
我收到此异常
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unknown entity: javax.persistence.Tuple; nested exception is org.hibernate.MappingException: Unknown entity: javax.persistence.Tuple] with root cause
org.hibernate.MappingException: Unknown entity: javax.persistence.Tuple
所以我认为查询无法映射到我的包装器......
所以我用这种方式改变了查询方法
@Query(value = "SELECT data, objectId, stampo, min(rendimento) as rendimento from rendimenti where objectId=:objectId "
+ "and stampo=:mold group By data, objectId, stampo order by data DESC LIMIT 0,1", nativeQuery = true)
Object[] findByObjectAndMold(@Param("objectId") int objecId, @Param("mold") String mold);
然后我尝试采用数组元素
Object[] o = repository(object.getObjectId(), mold);
logger.debug("Wrapper is " + o.toString());
if(o != null){
Date date = (Date) o[0];
logger.debug("La data è : " + date);
float performance = (float) o[3];
但是,我得到一个投射错误..
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.Date
这是正确的方法吗?
答案 0 :(得分:0)
ERROR: syntax error at or near "PERFORM"
LINE 39: PERFORM pg_notify(notification_channel, json_build_object(...
和/或您不能使用Object[]
然后创建构造函数,那么操作Entities
JPQL
s。最好放置默认的非arg构造函数,因为其他一些东西可能需要它。
Object
然后,如果使用public Wrapper(Object o1, Object o2 ... ) { //means NOT varargs but as
//many args you need, all as objects
this.performance = (Performance)o1;
this.id = (Integer)o2;
...
}
,则可以使用原始对象[]轻松转换Java8
。
Stream
只需检查costructor中的args与查询List<Wrapper> wrappers = Arrays.stream(yourObjectArray)
.map( arr -> new Wrapper(arr[0], arr[1], ...)) // the rs possible args
.collect(Collectors.toList());
获取的顺序相同。当然你也可以改变Object[]
内的顺序,但在我看来它只会搞砸。
.map()
NEW
然后就像创建JPQL public Wrapper(Performance o1, Integer o2 ... ) { //means NOT varargs but as
//many args you need, all as real types they are
this.performance = o1;
this.id = o2;
...
}
一样简单 - 例如 -
TypedQuery