【问题标题】:Reuse component in projection (NHibernate)在投影中重用组件(NHibernate)
【发布时间】:2010-12-01 13:42:09
【问题描述】:

是否可以在投影中重用组件映射?

这是 Vendor 实体的映射:

   <class name="Vendor" table="vendor">
     ...
     <property name="Name" column="Name" />
     <component name="Address" class="MyProject.Address, MyAssembly" >
       <property name="Street" column="street" />
       <property name="City" column="City" />
     </component>
   </class>

对于报告,我想在数据传输对象中检索这些供应商,但重用地址组件(因为有很多字段和一些有用的格式化行为)。

public class VendorDTO
{
    public string Name;
    public Address Address;

}

public class Address
{
    public string Street;
    public string City;
    public string SomeUsefulBehavour();
}

如果不将 Address 拆分到它自己的表中,这是否可能?

谢谢!

【问题讨论】:

  • 您要查询吗? “在投影中重用组件映射”是什么意思?我不明白这个问题...
  • 可能的提示 - 您可以编写自己的 ResultTranslators;您可以尝试使用 HQL 构造 new Address();我没有尝试过,所以只是提示。
  • 是的,我希望查询类似于:select new VendorDTO(vendor.Name, vendor.Address) from Vendor vendor 感谢您的提示。
  • 选择新的 VendorDTO(vendor.Name, vendor.Address) 不起作用?
  • 我认为您应该能够使用 AliasToBeanResultTransformer 实现这一目标

标签: nhibernate components dto projection


【解决方案1】:

我相信这应该'正常工作':

Session.QueryOver<Vendor>()
    .SelectList(builder =>
        builder.Select(x => x.Name)
            .Select(x => x.Address))
    .TransformUsing(Transformers.AliasToBean<VendorDTO>())
    .List<VendorDTO>();

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多