问题描述
假设我有这个(简化)类客户
Id(v => v.numero_cliente,numero_cliente)
HasMany (v => v.Acionamentos).Cascade.All()。LazyLoad()
类Movimentacao
References(v => v.Cliente ,id_Fornecedor)
类别Acionamento
参考资料(v => v.Cliente,numero_cliente)
流利的nHibernate会产生错误的SQL,例如:
如果我尝试获取Acionamentos,那么它会抛出一个不正确的SQL:
SELECT * FROM Acionamentos WHERE id_Fornecedor = p0
但是在我的Acionamento Mapping中,如果我在所有的引用中总是使用相同的列名称numero_cliente,那么我们可以使用no来替换所有引用中的列名称numero_cliente,而不是 id_Fornecedor 问题发生。但是恐怕我不能保证客户端类的所有列名在所有表上都是相同的。
有人知道该怎么做做?如果你想在这里确切的SQL是:
无法初始化集合:
[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 =?
上面的错误试图获得Cliente.Acionamentos
以下是HBM XML:
Sistema.Clientes.Cliente.hbm.xml:
< id name = numero_clientetype =System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089>
< column name =numero_cliente/>
< generator class =assigned/>
< / id>
< bag cascade =alllazy =truename =Acionamentosmutable =true>
< key>
< column name =id_Fornecedor/><! - oopps,this should be numero_cliente - >
< / key>
< / bag>
Sistema.CRM.Acionamento.hbm.xml:
< many-to-one class =Sistema.Clientes.Cliente,Sistema,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = nullname =Cliente >
< column name =numero_cliente/>
< /多对一> b
$ b pre> <多对一的类=Sistema.Clientes.Cliente,Sistema,版本= 1.0.0.0,Culture = neutral,PublicKeyToken = nullname =Clientenot-实测值= 忽略 >
< column name =id_Fornecedor/>
< /多对一>
添加KeyColumn!
Id(v => v.numero_cliente,numero_cliente)
HasMany v)> v.Acionamentos).KeyColumn(numero_cliente)。Cascade.All()。LazyLoad()
在添加之后,生成的HBM变成:
< bag cascade =alllazy = truename =Acionamentosmutable =true>
< key>
< column name =numero_cliente/>
< / key>
< / bag>
没有更多的SQL错误发生。
我很高兴我现在能够使用它!我真的不想使用EF
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>
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
这篇关于流利的NHibernate 1.1:当多个列名映射在不同的类上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!