【问题标题】:Nhibernate projection with child collection带有子集合的休眠投影
【发布时间】:2011-05-27 15:16:49
【问题描述】:

使用 NHibernate 2.1,我正在尝试将实体及其子集合投影到 DTO 中。我的实体看起来像这样..

public class Application
{
  public int Id {get;set;}
  public string Name {get;set;}
  public List<ApplicationSetting> Settings {get;set;}
  // A bunch of other properties that I don't want in the DTO
}

public class ApplicationSetting
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Code {get;set;}
   // A bunch of other properties that I don't want in the DTO
}

我的 DTO 看起来像这样..

public ApplicationDto
{
      public int Id {get;set;}
      public string Name {get;set;}
      public List<ApplicationSettingDto> Settings {get;set;}
}

public class ApplicationSettingDto
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Code {get;set;}
}

我选择 JUST 应用程序和项目的代码就是这个(使用 Nhibernate 2.1 和 nhLambdaExtensions)

  var applicationAlias = new Application();

  var criteria = Session
    .Add<Application>(a => a.Id == id);

      int? Id = null;
  string Name = null;

  criteria
    .SetProjection
    (
      Projections.Distinct(
        Projections.ProjectionList()
          .Add(LambdaProjection.Property<Application>(a => a.Id).As(() => Id))
          .Add(LambdaProjection.Property<Application>(a => a.Name).As(() => Name))
        )
    );

  criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ApplicationDto)));

  var contract = criteria.UniqueResult<ApplicationDto>();

我的问题是,如何将 ApplicationSettings 实体中的一些属性仅投影到 ApplicationSettingsDto 子集合?

【问题讨论】:

  • 你将无法做你想做的事。投影是数据的扁平化表示,而不是实体。所以你不能在一个投影中拥有一对多的表示。

标签: nhibernate nhibernate-criteria nhibernate-projections


【解决方案1】:

我认为您可能需要执行 MutiQuery 并将 DTO 父母和孩子自己聚集在一起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2014-02-26
    • 2011-04-11
    • 1970-01-01
    • 2015-07-17
    • 2017-05-25
    相关资源
    最近更新 更多