我有3个实体 - TUser,TRequest和TComment。 TUser到TRequest,关系1到很多。对TComment的请求,与许多人的关系。我想计算每个TUser的评估。最初我在TRequest中有一个评估专栏,我有这个查询正常工作:
Query query = em.createQuery("select hiredAgency, sum(r.assessment)/(count(r.assessment)), "
+ "count(r.assessment) "
+ "from TAgency hiredAgency join hiredAgency.executedRequests r "
+ "group by hiredAgency.tUserId, hiredAgency.address, hiredAgency.city, hiredAgency.information,"
+ "hiredAgency.website")
现在评估结果在TComment中。我尝试了以下查询:
Query query = em.createQuery("select r.hiredAgency, " +
"sum(u.assessment)/(count(u.assessment)), " +
"count(u.assessment) " +
"from TRequest r join r.requestComments u " +
"group by r.hiredAgency.tUserId, r.hiredAgency.address, r.hiredAgency.city, r.hiredAgency.information," +
"r.hiredAgency.website");
List<Object[]> list = query.getResultList();
但是Hibernate给了我错误:
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
..
Caused by: java.sql.SQLException: ORA-00979: not a GROUP BY expression
这是我的实体。 TRequest:
public class TRequest implements java.io.Serializable {
private Set<TComment> requestComments = new HashSet<TComment>(0);
private TAgency hiredAgency;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "HIRED_AGENCY_ID")
public TAgency getHiredAgency() {
return this.hiredAgency;
}
public void setHiredAgency(TAgency hiredAgency) {
this.hiredAgency = hiredAgency;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tRequest")
public Set<TComment> getRequestComments() {
return requestComments;
}
public void setRequestComments(Set<TComment> requestComments) {
this.requestComments = requestComments;
}
}
TComment:
public class TComment implements java.io.Serializable {
private int assessment; // +get and set methods
private TRequest tRequest;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "T_REQUEST_ID", nullable = false)
public TRequest gettRequest() {
return tRequest;
}
public void settRequest(TRequest tRequest) {
this.tRequest = tRequest;
}
}
TAgency:
public class TAgency implements java.io.Serializable {
private Long tUserId;
private TUser tUser;private String website;
private String city;
private String address;
private String information;
private Set<TRequest> executedRequests = new HashSet<TRequest>(0);
private Set<TAgencyEventType> tAgencyEventTypes = new HashSet<TAgencyEventType>(
0);
//+ get and set methods
@OneToMany(fetch = FetchType.LAZY, mappedBy = "hiredAgency")
public Set<TRequest> getExecutedRequests() {
return executedRequests;
}
public void setExecutedRequests(Set<TRequest> executedRequests) {
this.executedRequests = executedRequests;
}
}
ADDED
的DDL:
create table T_REQUEST
(
id NUMBER(19) not null,
hired_agency_id NUMBER(19)
)
create table T_COMMENT
(
id NUMBER(19) not null,
t_request_id NUMBER(19) not null,
assessment NUMBER(10)
)
create table T_AGENCY
(
t_user_id NUMBER(19) not null,
address VARCHAR2(150 CHAR),
city VARCHAR2(60 CHAR) not null,
information VARCHAR2(512 CHAR) not null,
website VARCHAR2(150 CHAR)
)
我想查询返回我的TAENCE对象(hiredAgency),从所有向其提出请求的人员收到的平均评估结果以及评估它的人数(给予评估)。
我无法理解我没有做的正确。