我正在开发一个肥皂网服务。我使用带有namedquery注释的JPA进行数据查询。但是在我的查询中使用像 sum 这样的聚合函数与group by结合导致了一些例外。我的代码如下所示。
@Entity
@Cacheable(false)
@Table(name = "criteriatable")
@XmlRootElement
@NamedQueries(
{
@NamedQuery(name = "Criteriatable.findAll", query = "SELECT c FROM Criteriatable c"),
@NamedQuery(name = "Criteriatable.findById", query = "SELECT c FROM Criteriatable c WHERE c.id = :id"),
@NamedQuery(name = "Criteriatable.findByCriteriaid", query = "SELECT c FROM Criteriatable c WHERE c.criteriaid = :criteriaid"),
@NamedQuery(name = "Criteriatable.findBySubcriteriaid", query = "SELECT c FROM Criteriatable c WHERE c.subcriteriaid = :subcriteriaid"),
@NamedQuery(name = "Criteriatable.findByProjectid", query = "SELECT c FROM Criteriatable c WHERE c.projectid = :projectid"),
@NamedQuery(name = "Criteriatable.findAllCriteriatableGroup", query = "SELECT c.criteriagroupname AS groupname, SUM(c.weight) AS groupweight FROM Criteriatable c WHERE c.projectid = :projectid GROUP BY c.criteriagroupname"),
@NamedQuery(name = "Criteriatable.SumAllWeightByProjectid", query = "SELECT SUM(c.weight) AS TotalProjectWeight FROM Criteriatable c WHERE c.projectid = :projectid")
})
public class Criteriatable implements Serializable
{
@Basic(optional = false)
@NotNull
@Column(name = "weight")
private double weight;
@Basic(optional = false)
@NotNull
@Column(name = "status")
private int status;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "criteriagroupname")
WS方法
@WebMethod(operationName = "getAllCriteriaGroupByProjectid")
public List<Criteriatable> getAllCriteriaGroupByProjectid(@WebParam(name = "projectid") int projectid)
{
EntityManager em = emf.createEntityManager();
String find = "Criteriatable.findAllCriteriatableGroup";
TypedQuery<Criteriatable> query = em.createNamedQuery(find, Criteriatable.class);
query.setParameter("projectid", projectid);
return query.getResultList();
}
我收到的错误是:
SEVERE: Error occured
javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class [Ljava.lang.Object; nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super class is known to this context.]
我需要帮助。感谢。
答案 0 :(得分:0)
我认为问题是您从网络方法返回List
。尝试返回自己的包装类,将其中的列表作为字段。像这样的东西
public class CriteriatablesWrapper {
private List<Criteriatable> criteriatableList;
// getter and setter
}
并将服务方法签名更改为
@WebMethod(operationName = "getAllCriteriaGroupByProjectid")
public CriteriatablesWrapper getAllCriteriaGroupByProjectid(@WebParam(name = "projectid") int projectid) {
...
}
答案 1 :(得分:0)
您的查询Criteriatable.findAllCriteriatableGroup
未返回Criteriatable
个对象。
它返回[groupName,groupweight]元组列表。
请记住,通用类型信息在运行时不可用,因此运行时的List<Criteriatable>
集合将为List<Object[]>
(实际为List<Object[2]>
,但您无法将其写入方式)。
这随后使JAXB映射失败。
您需要为结果定义POJO:
@XmlRootElement
public class GroupTotal {
private String groupName;
private double groupTotal;
public GroupTotal(String groupName, double groupTotal) {
this.groupName = groupName;
this.groupTotal = groupTotal;
}
// getters and setters ...
}
修改您的查询:
SELECT NEW your.package.GroupTotal (c.criteriagroupname, SUM(c.weight))
FROM Criteriatable c WHERE c.projectid = :projectid GROUP BY c.criteriagroupname
最后执行:
TypedQuery<GroupTotal> query = em.createNamedQuery(find, GroupTotal.class);
我还没有尝试过,但它应该让你指向正确的方向。