1
votes

Suppose I have this (simplified)

Class Cliente
Id(v => v.numero_cliente, "numero_cliente")
HasMany(v => v.Acionamentos).Cascade.All().LazyLoad()

Class Movimentacao
References(v => v.Cliente, "id_Fornecedor")

Class Acionamento
References(v => v.Cliente, "numero_cliente")

Fluent nHibernate will generate wrong SQL, for example:
If i try to get Acionamentos,then it will throw an incorrect SQL:

SELECT * FROM Acionamentos WHERE id_Fornecedor=p0

But on my Acionamento Mapping i set an reference to a column named numero_cliente and not to id_Fornecedor

If I use always the same column name "numero_cliente" on all References, no problem happens. But i am afraid i will not be able to guarantee that all column names for the Client class will be the same on all tables.

Does somebody knows what to do? Can the Fluent NHibernate team see this and post an comment here?

If you want the exact SQL here is:
could not initialize a collection: [Sistema.Clientes.Cliente.Acionamentos#019012938/07][SQL: SELECT acionament0_.id_Fornecedor as id7_1_, acionament0_.id_Acionamento as id1_1_, acionament0_.id_Acionamento as id1_6_0_, acionament0_.DataHora as DataHora6_0_, acionament0_.Tipo as Tipo6_0_, acionament0_.Descricao as Descricao6_0_, acionament0_.numero_cliente as numero5_6_0_, acionament0_.id_Usuario as id6_6_0_ FROM clientes.acionamento acionament0_ WHERE acionament0_.id_Fornecedor=?

The error above throwns when trying to get the Cliente.Acionamentos


Below is the HBM XML:

Sistema.Clientes.Cliente.hbm.xml:

<id name="numero_cliente" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <column name="numero_cliente" /> 
    <generator class="assigned" /> 
</id>
<bag cascade="all" lazy="true" name="Acionamentos" mutable="true">
    <key>
        <column name="id_Fornecedor" /><!-- oopps, this should be numero_cliente --> 
    </key>
   <one-to-many class="Sistema.CRM.Acionamento, Sistema, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
</bag>

Sistema.CRM.Acionamento.hbm.xml:

<many-to-one class="Sistema.Clientes.Cliente, Sistema, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Cliente">
    <column name="numero_cliente" /> 
</many-to-one>

Estoque.Movimentacao.hbm.xml:

<many-to-one class="Sistema.Clientes.Cliente, Sistema, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Cliente" not-found="ignore">
    <column name="id_Fornecedor" /> 
</many-to-one>
1
Wrong how? This is not the bug tracking site for Fluent NHibernate. If you're having a problem with the wrong SQL being generated, please post what SQL is being generated and what you want it to be so that maybe users can help you fix the issue.Brandon
You're going to have to add more detail. There really isn't anything here to go on. "wrong SQL" could mean anything.user1228
Maybe our valuable friend @James-Gregory or @paul-batum could give an light on this.Tony
Fornecedor and DataHora - I hardly dare run those through Google Translate...Ian Nelson
Can you paste the code from your generated xml mapping files. That should help find your problem. blog.yodersolutions.com/2009/07/…Khaja Minhajuddin

1 Answers

1
votes

With the help steps you provided and some pray I solved it, adding the KeyColumn!

Id(v => v.numero_cliente, "numero_cliente")
HasMany(v => v.Acionamentos).KeyColumn("numero_cliente").Cascade.All().LazyLoad()

after adding that, then the generated HBM was changed to:

<bag cascade="all" lazy="true" name="Acionamentos" mutable="true">
    <key>
        <column name="numero_cliente" /> 
    </key>
    <one-to-many class="Sistema.CRM.Acionamento, Sistema, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
</bag>

and no more SQL errors happened.

I am happy that I will be able to use it now! I really did not want to use EF