Fluent NHibernate 1.1:当在不同的类上使用多个列名映射时
假设我有这个(简化的)
类客户
Id(v => v.numero_cliente, "numero_cliente")
HasMany(v => v.Acionamentos).Cascade.All().LazyLoad()
类 Movimentacao
参考文献(v => v.Cliente, "id_Fornecedor")
行动类
References(v => v.Cliente, "numero_cliente")
Fluent nHibernate 会生成错误的 SQL,例如:
如果我尝试获取 Acionamentos,那么它将抛出错误的 SQL:
SELECT * FROM Acionamentos WHERE id_Fornecedor=p0
但在我的 Acionamento 映射中,我设置了对名为 numero_cliente 的列的引用 而不是 id_Fornecedor
如果我在所有引用上始终使用相同的列名称“numero_cliente”,则不会发生问题。但我担心我无法保证 Client 类的所有列名称在所有表上都相同。
有人知道该怎么做吗? Fluent NHibernate 团队可以看到这个并在这里发表评论吗?
如果你想要确切的 SQL,这里是:
无法初始化集合: [Sistema.Clientes.Cliente.Acionamentos#019012938/07][SQL:选择 acionament0_.id_Fornecedor 作为 id7_1_、acionament0_.id_Acionamento 作为 id1_1_、acionament0_.id_Acionamento 作为 id1_6_0_、acionament0_.DataHora 作为 DataHora6_0_ , acionament0_.Tipo 为 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 时会抛出上述错误,
下面是 H BM 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过您提供的帮助步骤和一些祈祷我解决了它,添加了 KeyColumn!
添加后,生成的 HBM 更改为:
并且不再发生 SQL 错误。
我很高兴现在就能使用它!我真的不想用EF
With the help steps you provided and some pray I solved it, adding the KeyColumn!
after adding that, then the generated HBM was changed to:
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