我是部分UI开发人员,现在我需要转向Hibernate开发。今天我在尝试使用UNION ALL时遇到了HQL问题,这里是HQL:
SELECT COUNT(DISTINCT users.userId) AS totalSize FROM (SELECT DISTINCT
d1.sponsor.id AS userId FROM Dating d1 WHERE d1.invitee.id = ? UNION ALL
SELECT DISTINCT d2.invitee.id AS userId FROM Dating d2 WHERE d2.sponsor.id = ?)
AS users
它显示如下错误:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 55
然后我尝试将此HQL转换为原始SQL:
SELECT COUNT(DISTINCT users.userId) AS totalSize FROM (SELECT DISTINCT
d1.sponsorId AS userId FROM mmy_dating d1 WHERE d1.inviteeId = 6 UNION
ALL SELECT DISTINCT d2.inviteeId AS userId FROM mmy_dating d2 WHERE
d2.sponsorId = 6) AS users;
它通常显示正确的结果,所以我在想Hibernate是否不支持UNION ALL语法?
提前致谢。
答案 0 :(得分:6)
答案 1 :(得分:0)
想分享,在我的情况下,我发现了一种避开这种情况的情况。 这里唯一的规则是具有相同的类型,在这种情况下String,对应于返回列表,可以添加所需数量的表:
public List<String> findByCPForCNPJ(String query){
TypedQuery<String> ccpf = manager.createQuery("select cpf from PessoaFisica where cpf like :pCpf", String.class);
ccpf.setParameter("pCpf", "%" + query + "%");
List<String> lista1 = ccpf.getResultList();
TypedQuery<String> ccnpj = manager.createQuery("select cnpj from PessoaJuridica where cnpj like :pCnpj", String.class);
ccnpj.setParameter("pCnpj", "%" + query + "%");
lista1.addAll(ccnpj.getResultList());
return lista1;
}
我希望我能为所有人做出贡献,祝你好运......