我有一个mysql查询,如下所示:
SELECT * FROM `HOST` WHERE `DATE_OF_SCAN` between
(SELECT subtime((Select max(`DATE_OF_SCAN`) from `HOST` ), '0 0:5:0'))
and
(Select max(`DATE_OF_SCAN`) from `HOST` );
基本上是从表中的最大日期范围和(最大日期 - 5分钟)返回主机。
我试图在jpql中重写它:
@Query("SELECT h FROM Host h WHERE h.date between "
+ "(SELECT subtime((Select max(ho.date) from Host ho ), 0 0:5:0)) "
+ "and (Select max(hos.date) from Host hos)")
public List<Host> findHosts();
但它经常抛出异常:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: 0 near line 1, column 126 [SELECT h FROM com.iie.model.Host h WHERE h.date between (SELECT subtime((Select max(ho.date) from com.iie.model.Host ho ), 0 0:5:0)) and (Select max(hos.date) from com.iie.model.Host hos)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:276)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:285)
最后。我试图通过&#39; 0 0:5:0&#39;作为参数,但它抛出一个异常,这不是一个合适的Joda DateTime格式。
主持人:
@Entity
@Table(name = "HOST")
public class Host {
@Id
@GeneratedValue
@Column(name = "HOST_ID")
private Long id;
@Column(name = "HOSTNAME")
private String hostname;
@Column(name = "IP")
private String ip;
@Column(name = "DATE_OF_SCAN")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime date;
public Host(Long id, String hostname, String ip, DateTime date) {
this.id = id;
this.hostname = hostname;
this.ip = ip;
this.date = date;
}
public Host(String hostname, String ip, DateTime date) {
this.hostname = hostname;
this.ip = ip;
this.date = date;
}
public Host() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public DateTime getDate() {
return date;
}
public void setDate(DateTime date) {
this.date = date;
}
答案 0 :(得分:0)
此部分无效SQL
SELECT h FROM Host h
我建议更改为显式列名
SELECT h.mycolumn FROM Host h