简体   繁体   English

HQL休眠许多表

[英]HQL Hibernate many tables

I'm new to hibernate, and i'm trying to make a list out of 4 tables, but it's not working. 我是休眠的新手,我正在尝试从4个表中列出一个列表,但是它不起作用。

public List<DocumentoAssinanteTO> listAssinanteSemImagemByDocument(DocumentoTO documento, UsuarioDepartamentoTO ud) {
    StringBuilder hql = new StringBuilder();
    hql.append(" SELECT DA.id, ");
    hql.append(" DOC.id,");
    hql.append(" UD.id, ");
    hql.append(" D.id, ");
    hql.append(" U.id, ");
    hql.append(" U.nome,");
    hql.append(" FROM ").append(DocumentoAssinanteTO.class.getName()).append(" DA ");
    hql.append(" INNER JOIN DA.documento DOC ");
    hql.append(" INNER JOIN DA.usuarioDepartamento UD ");
    hql.append(" INNER JOIN UD.usuario              U");
    hql.append(" INNER JOIN UD.departamento         D");
    hql.append(" WHERE DOC = :idDocumento AND UD = :idUserDep ");
    hql.append(" AND U.assinatura IS NULL ");


    Query query = queryTransform(hql.toString());
    query.setLong("idDocumento", documento.getId());
    query.setLong("idUserDep", ud.getId());

    return query.list();
}

maybe it's the JOIN part, don't know if i should use INNER, LEFT or just JOIN 也许这是JOIN的一部分,不知道我应该使用INNER,LEFT还是JOIN

I want to create a hql like this sql 我想创建一个像这样的sql的hql

 SELECT docass.id_documento_assinante,doc.id,
        docass.id_user_depto,u.id,u.nome
FROM DCF_DOCUMENTO_ASSINANTE as docass
JOIN DCF_CONTENT as doc ON doc.id = docass.id_documento
JOIN DCF_USUARIO_DEPARTAMENTO as userDep ON userDep.id = docass.id_user_depto
JOIN DCF_USUARIOS as u  ON u.id = userDep.id_usuario
WHERE u.id_anexo_assinatura is null

First of all your problem is not fully cleared, define your database models here first, and from your query i have come to know that 首先,您的问题尚未完全解决,请首先在此处定义您的数据库模型,然后从您的查询中我知道

 hql.append(" DOC.id, "); 

the last Coma will not be added here hql.append(" DOC.id "); 最后一个逗号不会在此处添加hql.append(“ DOC.id”);

Here 这里

hql.append(" FROM ").append(DocumentoAssinanteTO.class.getName()).append(" DA ");

you don't need to get name of class by calling class.getname() method you just call here the bean or name of Model. 您不需要通过调用class.getname()方法来获取类的名称,您只需在此处调用Model的bean或名称即可。

and there is lot of problem in your inner joins. 而且内部联接中存在很多问题。

Ok, i got it working, made some adjusts 好吧,我开始工作了,做了一些调整

public List listAssinanteSemImagemByDocument(DocumentoTO documento) { StringBuilder hql = new StringBuilder(); 公共列表listAssinanteSemImagemByDocument(DocumentoTO documento){StringBuilder hql = new StringBuilder(); hql.append(" SELECT DA.id, "); hql.append(“ SELECT DA.id,”); hql.append(" DOC.id,"); hql.append(“ DOC.id,”); hql.append(" UD.id, "); hql.append(“ UD.id,”); hql.append(" U.id, "); hql.append(“ U.id,”); hql.append(" U.nome "); hql.append(“ U.nome”); hql.append(" FROM ").append(DocumentoAssinanteTO.class.getName()).append(" DA "); hql.append(“ FROM”).append(DocumentoAssinanteTO.class.getName())。append(“ DA”); hql.append(" JOIN DA.documento DOC"); hql.append(“ JOIN DA.documento DOC”); hql.append(" JOIN DA.usuarioDepartamento UD"); hql.append(“ JOIN DA.usuarioDepartamento UD”); hql.append(" JOIN UD.usuario U"); hql.append(“ JOIN UD.usuario U”); hql.append(" WHERE DOC = :idDocumento "); hql.append(“ WHERE DOC =:idDocumento”); hql.append(" AND U.assinatura IS NULL "); hql.append(“ AND U.assinatura IS NULL”);

    Query query = queryTransform(hql.toString());
    query.setLong("idDocumento", documento.getId());

    return query.list();

}

the real problem was here the last comma. 真正的问题是最后一个逗号。 hehe 呵呵

hql.append(" U.nome,"); hql.append(“ U.nome,”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM