-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Join classes
PostPosted: Mon Jun 12, 2006 11:12 am 
Newbie

Joined: Mon Jun 12, 2006 8:44 am
Posts: 13
I have this 2 classes Cliente and Pedido. I would like to select only the Cliente that has Pedido. I'm trying to do this:

ICriteria criteria = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof(Cliente));
IClienteDao clienteDao = (new NHibernateDaoFactory().GetClienteDao());

criteria.Add(Expression.Eq("Ativo", "1"))
.SetFetchMode("Pedidos", FetchMode.Join)
.AddOrder(Order.Asc("NomeFantasia"))
.SetResultTransformer(CriteriaUtil.DistinctRootEntity);

But it's not working, it's returning all my Clientes either they have Pedido or not. What is wrong?


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="ADFinance.Comum.Cliente, ADFinance" table="T_ADF_MD_ClIE">
<id name="ID" column="cod_cliente" unsaved-value="0">
<generator class="native">
<param name="sequence">S_T_ADF_MD_ClIE</param>
</generator>
</id>

<property name="NomeFantasia" column="nome_fantasia" />
<property name="RazaoSocial" column="razao_social" />
<property name="Cnpj" column="cnpj" />
<property name="InscricaoEstadual" column="inscricao_estadual" />
<property name="Ativo" column="ativo" />
<property name="Email" column="email" />
<property name="CodAgencia" column="cod_agencia" />

<many-to-one name="ClientePrincipal"
class="ADFinance.Comum.Cliente, ADFinance" column="cliente_principal" />
<many-to-one name="Usuario"
class="ADFinance.Acesso.Usuario, ADFinance" column="cod_usuario" />

<set lazy="true" inverse="false" name="Telefones"
access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
<key column="cod_cliente" />
<one-to-many class="ADFinance.Comum.Telefone, ADFinance" />
</set>
<set lazy="true" inverse="false" name="Enderecos"
access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
<key column="cod_cliente" />
<one-to-many class="ADFinance.Comum.Endereco, ADFinance" />
</set>
<set lazy="true" inverse="false" name="Pedidos"
access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
<key column="cod_cliente" />
<one-to-many class="ADFinance.Faturamento.Pedido, ADFinance" />
</set>
</class>

</hibernate-mapping>

and

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="ADFinance.Faturamento.Pedido, ADFinance" table="T_ADF_PO_Pedido">
<id name="ID" column="cod_po" unsaved-value="0">
<generator class="native">
<param name="sequence">S_T_ADF_PO_Pedido</param>
</generator>
</id>

<property name="StatusFaturamento" column="cod_status_faturamento" />
<property name="TipoPedido" column="cod_tipo_pedido" />
<property name="CodPoOrigem" column="cod_po_origem" />
<property name="StatusOperacao" column="cod_status_operacoes" />
<property name="CodAp" column="cod_ap" />

<property name="NotaFiscalFornecedor" column="cod_notafiscal_fornecedor" />
<property name="NotaFiscalVeiculo" column="cod_notafiscal_veiculo" />
<property name="PorcentagemComissao" column="porcentagem_comissao" />
<property name="ValorComissao" column="valor_comissao" />
<property name="TipoCondicaoFaturamento" column="cod_condicao_faturamento" />
<property name="TipoRateio" column="cod_tipo_rateio" />
<property name="CodCliente" column="cod_cliente" />

<property name="CodProduto" column="cod_produto" />
<bag name="Itens" lazy="true" table="T_ADF_CP_PO_Item" inverse="false"
access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" >
<key column="cod_po" />
<one-to-many class="ADFinance.Faturamento.ItemPedido, ADFinance" />
</bag>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:23 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
you would need to tell NH that you want Cliente with NOT NULL Pedidos...

Code:
ICriteria criteria = NHibernateSessionManager.Instance.GetSession().CreateCriteria(typeof(Cliente));
IClienteDao clienteDao = (new NHibernateDaoFactory().GetClienteDao());

criteria.Add(Expression.Eq("Ativo", "1"))
    .Add(Expression.NotNullExpression("Pedido")) // Pedidio is NOT NULL
    .SetFetchMode("Pedidos", FetchMode.Join)
    .AddOrder(Order.Asc("NomeFantasia"))
    .SetResultTransformer(CriteriaUtil.DistinctRootEntity);


-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:44 am 
Newbie

Joined: Mon Jun 12, 2006 8:44 am
Posts: 13
There's no NotNullExpression method in th Expression class...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 3:06 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
sorry. i was looking at the CHM and not my code:

Quote:
NHibernate Class Library - v.1.0.2.0

NotNullExpression Class
An ICriterion that represents "not null" constraint.

For a list of all members of this type, see NotNullExpression Members.

System.Object
NHibernate.Expression.AbstractCriterion
NHibernate.Expression.NotNullExpression

public class NotNullExpression : AbstractCriterion

Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.

Requirements
Namespace: NHibernate.Expression
Assembly: NHibernate (in NHibernate.dll)


it really should be:
Code:
criteria.Add(Expression.Eq("Ativo", "1"))
    .Add(Expression.IsNotNull("Pedido")) // Pedidio is NOT NULL
    .SetFetchMode("Pedidos", FetchMode.Join)
    .AddOrder(Order.Asc("NomeFantasia"))
    .SetResultTransformer(CriteriaUtil.DistinctRootEntity);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 1:13 pm 
Newbie

Joined: Mon Jun 12, 2006 8:44 am
Posts: 13
it didnt work. Still returning all Clientes


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 1:17 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
can you post the generated SQL? i can't see how, if the column is NULL in the db, a Cliente with no Perdido would be returned in this situation.

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.