我遇到一个问题,如果我使用createAlias
Hibernate
我的datatable
重复值,但如果我不使用它,则不显示重复值
我的网页代码:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition
template="/template/common/pagelayout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ez="http://java.sun.com/jsf/composite"
xmlns:p="http://primefaces.org/ui"
xmlns:sec="http://www.springframework.org/security/facelets/tags">
<ui:define name="content">
<div id="content">
<f:metadata>
<f:event listener="#{emitenteListBean.helper.fillDefaults()}" type="preRenderView" />
</f:metadata>
<br />
<div class="form_title">
<span class="active"> <p:outputLabel value="Filtro de Notas Fiscais"/> </span>
</div>
<br />
<h:form id="list">
<p:messages id="messages"/>
<p:panelGrid styleClass="panelGridCenter noBorders">
<p:row>
<p:column styleClass="columnRight">
<p:outputLabel value="Emitente"/>
</p:column>
<p:column>
<p:selectOneMenu value="#{emitenteListBean.filter.emitente}" label="Emitente" converter="emitenteConverter" filter="true" filterMatchMode="contains">
<f:selectItem itemLabel="" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems var="emitente" value="#{emitenteListBean.listaEmitentes}" itemLabel="#{emitente.dadosEmpresa.cnpj} - #{emitente.dadosEmpresa.razaoSocial}"/>
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column styleClass="columnRight">
<p:outputLabel value="Numero da Nota Fiscal"/>
</p:column>
<p:column>
<p:inputText maxlength="10" size="15" value="#{emitenteListBean.filter.numeroNota}" onkeypress="mascara(this,soNumeros)"/>
</p:column>
</p:row>
<p:row>
<p:column styleClass="columnRight">
<p:outputLabel value="Data de Emissão da Nota Fiscal"/>
</p:column>
<p:column>
<p:calendar locale="pt_BR" pattern="dd/MM/yyyy" label="Data de Emissão da Nota Fiscal" value="#{emitenteListBean.filter.dataEmissaoNota}"/>
</p:column>
</p:row>
<f:facet name="footer">
<p:row>
<p:column colspan="2">
<div align="right">
<p:commandButton value="Filtrar" actionListener="#{emitenteListBean.buscar}" update="tabela messages" ajax="false"/>
</div>
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
<br/>
<div class="form_title">
<span class="active"> <p:outputLabel value="Notas Fiscais Cadastradas"/> </span>
</div>
<br/><br/>
<p:dataTable
id="tabela"
var="emitente"
value="#{emitenteListBean.lazyModel}"
emptyMessage="Nenhum emitente cadastrado"
lazy="true"
rows="10"
paginator="true"
paginatorPosition="bottom"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15">
<f:facet name="header">
<h:outputText value="Notas Fiscais por Emitente"/>
</f:facet>
<p:column style="width:2%">
<p:rowToggler/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Razão Social - CNPJ"/>
</f:facet>
<h:outputText value="#{emitente.dadosEmpresa.razaoSocial} - #{emitente.dadosEmpresa.cnpj}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Quantidade de Notas Fiscais"/>
</f:facet>
<h:outputText value="#{emitente.notasFiscais.size()}" />
</p:column>
<p:rowExpansion>
<p:dataTable
id="tabelaNotasFiscais"
value="#{emitente.notasFiscais}"
styleClass="noBorders"
var="notaFiscal"
lazy="true"
emptyMessage="Nenhuma nota fiscal está cadastrada."
rows="#{emitente.notasFiscais.size()}">
<p:column headerText="Numero da Nota Fiscal">
<h:outputText value="#{notaFiscal.numero}"/>
</p:column>
<p:column headerText="Série da Nota Fiscal">
<h:outputText value="#{notaFiscal.serie}"/>
</p:column>
<p:column headerText="Data de Emissão">
<h:outputText value="#{notaFiscal.dataEmissao}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</p:column>
</p:dataTable>
</p:rowExpansion>
</p:dataTable>
<br />
<br />
</h:form>
</div>
</ui:define>
</ui:composition>
我的代码:
@Override
protected Criteria setUpCriteria(Criteria criteria, FilterNfeHost filter) {
if (!NullUtil.isNull(filter) && filter instanceof EmitenteFilter) {
EmitenteFilter filtro = (EmitenteFilter) filter;
criteria.createAlias("pojo.notasFiscais", "nf");
return CriterionFactory.getInstance(criteria)
.eq("pojo.id", NullUtil.isNull(filtro.getEmitente()) ? null : filtro.getEmitente().getId())
.eq("nf.numero", filtro.getNumeroNota())
.eq("nf.dataEmissao", filtro.getDataEmissaoNota())
.getCriteria();
}
return criteria;
}
所以我的注册正在重复......
我理解这个问题,但我不知道如何解决这个问题,如果我有一个重复六次的寄存器,它会显示六次。
我有一对一的关系...
我的sql hibernate代码
Hibernate:
select
this_.id as id1_4_6_,
this_.cnae as cnae2_4_6_,
this_.dados_empresa_id as dados_em6_4_6_,
this_.inscricao_estadual_substituto_tributario as inscrica3_4_6_,
this_.inscricao_municipal as inscrica4_4_6_,
this_.nome_fantasia as nome_fan5_4_6_,
dadosempre3_.id as id1_2_0_,
dadosempre3_.cnpj as cnpj2_2_0_,
dadosempre3_.cpf as cpf3_2_0_,
dadosempre3_.endereco_id as endereco6_2_0_,
dadosempre3_.inscricao_estadual as inscrica4_2_0_,
dadosempre3_.razao_social as razao_so5_2_0_,
endereco4_.id as id1_5_1_,
endereco4_.bairro as bairro2_5_1_,
endereco4_.cep as cep3_5_1_,
endereco4_.complemento as compleme4_5_1_,
endereco4_.logradouro as logradou5_5_1_,
endereco4_.municipio as municipi6_5_1_,
endereco4_.numero as numero7_5_1_,
endereco4_.telefone as telefone8_5_1_,
endereco4_.uf as uf9_5_1_,
notasfisca1_.id as id1_6_2_,
notasfisca1_.arquivo_nota_fiscal_id as arquivo15_6_2_,
notasfisca1_.chave_nfe as chave_nf2_6_2_,
notasfisca1_.data_emissao as data_emi3_6_2_,
notasfisca1_.data_hora_entrada_ou_saida_produto as data_hor4_6_2_,
notasfisca1_.descricao_natureza_operacao as descrica5_6_2_,
notasfisca1_.destinatario_id as destina16_6_2_,
notasfisca1_.digest_value as digest_v6_6_2_,
notasfisca1_.emitente_id as emitent17_6_2_,
notasfisca1_.finalidade_emissao as finalida7_6_2_,
notasfisca1_.identificador_processo_emissao as identifi8_6_2_,
notasfisca1_.numero as numero9_6_2_,
notasfisca1_.serie as serie10_6_2_,
notasfisca1_.tipo_emissao as tipo_em11_6_2_,
notasfisca1_.tipo_operacao as tipo_op12_6_2_,
notasfisca1_.valor_icms as valor_i13_6_2_,
notasfisca1_.valor_nota_fiscal as valor_n14_6_2_,
arquivonot6_.id as id1_0_3_,
arquivonot6_.conteudo as conteudo2_0_3_,
arquivonot6_.data_registro as data_reg3_0_3_,
arquivonot6_.nome as nome4_0_3_,
destinatar7_.id as id1_3_4_,
destinatar7_.dados_empresa_id as dados_em4_3_4_,
destinatar7_.email as email2_3_4_,
destinatar7_.inscricao_suframa as inscrica3_3_4_,
emitente8_.id as id1_4_5_,
emitente8_.cnae as cnae2_4_5_,
emitente8_.dados_empresa_id as dados_em6_4_5_,
emitente8_.inscricao_estadual_substituto_tributario as inscrica3_4_5_,
emitente8_.inscricao_municipal as inscrica4_4_5_,
emitente8_.nome_fantasia as nome_fan5_4_5_
from
emitente this_
left outer join
dados_empresa dadosempre3_
on this_.dados_empresa_id=dadosempre3_.id
left outer join
endereco endereco4_
on dadosempre3_.endereco_id=endereco4_.id
inner join
nota_fiscal notasfisca1_
on this_.id=notasfisca1_.emitente_id
left outer join
arquivo_nota_fiscal arquivonot6_
on notasfisca1_.arquivo_nota_fiscal_id=arquivonot6_.id
left outer join
destinatario destinatar7_
on notasfisca1_.destinatario_id=destinatar7_.id
left outer join
emitente emitente8_
on notasfisca1_.emitente_id=emitente8_.id limit ?
那么问题是什么?
答案 0 :(得分:1)
通过创建条件,您正在使用类似条件的自然连接。尝试打印出实际执行的SQL查询(通过在config.xml中添加sql print为true),这样用户就可以看到实际执行的查询..
即,
用户希望翻译是直接的
但翻译发生了相反的情况,它与translatorfactory实现进行了协商,而这种实现并不像预期的那样。
建议:使用字段名称而不是别名。
答案 1 :(得分:1)
可以使用此结果转换器过滤唯一结果:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);