2013-06-17 6 views
4

나는 아이 엔티티 섹션의 컬렉션을 포함하는 다음 부모 개체 부서자 NHibernate는 : 부모 속성에 자식 개체를 투사하면 예외를 발생

public class Department 
{ 
    private Iesi.Collections.Generic.ISet<Section> _sections; 
    public Department() 
    { 
     _sections = new HashedSet<Section>(); 
    } 
    public virtual Guid Id { get; protected set; } 
    public virtual string Name { get; set; } 
    public virtual ICollection<Section> Sections 
    { 
     get { return _sections; } 
    } 
    public virtual int Version { get; set; } 
} 

public partial class Section 
{ 
    public Section() 
    { 
     _employees = new HashedSet<Employee>(); 
    } 
    public virtual Guid Id { get; protected set; } 
    public virtual string Name { get; set; } 
    public virtual Department Department { get; protected set; } 
    public virtual int Version { get; set; } 
} 

나는 다음과 같은 DTO

로 변환 (평평)하고 싶은이
public class SectionViewModel 
{ 
    public string DepartmentName { get; set; } 
    public string SectionName { get; set; } 
} 

다음 코드를 사용하십시오.

SectionModel sectionModel = null; 
Section sections = null; 
var result = _session.QueryOver<Department>().Where(d => d.Company.Id == companyId) 
      .Left.JoinQueryOver(x => x.Sections,() => sections) 
      .Select(
        Projections.ProjectionList() 
         .Add(Projections.Property<Department>(d => sections.Department.Name).WithAlias(() => sectionModel.DepartmentName)) 
         .Add(Projections.Property<Department>(s => sections.Name).WithAlias(() => sectionModel.SectionName)) 
        ) 
      .TransformUsing(Transformers.AliasToBean<SectionModel>()) 
      .List<SectionModel>(); 

그러나 나는 다음과 같은 예외가 점점 오전 : 나는 심지어 다음 LINQ 표현

 var result = (from d in _session.Query<Department>() 
         join s in _session.Query<Section>() 
          on d.Id equals s.Department.Id into ds 
         from sm in ds.DefaultIfEmpty() 
         select new SectionModel 
          { 
           DepartmentName = d.Name, 
           SectionName = sm.Name ?? null 
          }).ToList(); 

매핑을 시도 Domain.Section

: Department.Name의 : 재산 확인할 수 없음

public class DepartmentMap : ClassMapping<Department> 
{ 
    public DepartmentMap() 
    {   
     Id(x => x.Id, m => m.Generator(Generators.GuidComb)); 
     Property(x => x.Name, 
      m => 
      { 
       m.Length(100); 
       m.NotNullable(true); 
      }); 

     Set(x => x.Sections, 
        m => 
        { 
         m.Access(Accessor.Field); 
         m.Inverse(true); 
         m.BatchSize(20); 
         m.Key(k => { k.Column("DeptId"); k.NotNullable(true); }); 
         m.Table("Section"); 
         m.Cascade(Cascade.All | Cascade.DeleteOrphans); 
        }, 
        ce => ce.OneToMany()); 
    } 
} 


public class SectionMap : ClassMapping<Section> 
{ 
    public SectionMap() 
    { 
     Id(x => x.Id, m => m.Generator(Generators.GuidComb)); 
     Property(x => x.Name, 
      m => 
      { 
       m.Length(100); 
       m.NotNullable(true); 
      }); 
     ManyToOne(x => x.Department, 
       m => 
       { 
        m.Column("DeptId"); 
        m.NotNullable(true); 
       }); 
    } 
} 

하지만이 메서드 또는 작업을 구현하지 않습니다을 throw합니다.

내가 잘못했거나 누락 된 부분에 대한 안내를 구하십시오.

+0

섹션에서 부서로 명시 적으로 참여해야합니다 ... 아니면 '부서명. 이름'을 선택할 수도 있습니다. SQL이 어떻게 생겼는지 알고 있습니까? –

+0

다음은 예상되는 SQL'select d.Name을 deptName으로, s.Name을 SectionName으로, Dept d를 왼쪽 조인 섹션 s에 d.id = s.deptid 순서로 d.Name로 오름차순으로 정렬합니다.이름 오름차순 ' – kagundajm

답변

2

같은 LINQ 쿼리를 시도하십시오. QueryOver에 대해 기억해야 할 것은 SQL로 직접 변환된다는 것입니다. 다음 SQL을 쓸 수 없습니다 :

select [Section].[Department].[Name] 

오른쪽? 따라서 QueryOver에서 동일한 작업을 수행 할 수 없습니다. 나는 당신이 시작 Department 개체에 대한 별명을 작성하고 프로젝션 목록에있는 것을 사용하는 것이 : 나는 당신이 order by 절을하고 싶은 당신의 코멘트에 주목

Department department; 
Section sections;  

var result = _session.QueryOver<Department>(() => department) 
    .Where(d => d.Company.Id == companyId) 
    .Left.JoinQueryOver(x => x.Sections,() => sections) 
    .Select(
      Projections.ProjectionList() 
       .Add(Projections.Property(() => department.Name).WithAlias(() => sectionModel.DepartmentName)) 
       .Add(Projections.Property(() => sections.Name).WithAlias(() => sectionModel.SectionName)) 
      ) 
    .TransformUsing(Transformers.AliasToBean<SectionModel>()) 
    .List<SectionModel>(); 

. 도움이 필요하면 알려주고 아마도 도움을 얻을 수 있습니다.

희망 하시겠습니까?

+0

이것은 작동합니다. 나는 내가 잘못 가고있는 곳을 본다. 예, '주문하기'를 원하지만 처리 할 수 ​​있습니다. 인내와 문제를 가져 주셔서 감사합니다. – kagundajm

+0

@CallMeKags : 문제 없습니다! QueryOver는 때때로 알아 내기가 어려울 수 있음을 알고 있습니다. –

0

이제 3.3.3으로 고정 될 수 있습니다. 당신이 3.3.3을 사용하지 않는 경우 예측

확실하지 않음으로 컬렉션을 포함하는 추가 기능을하지만이 문제는 특히 경우 만 -

  • 새로운 기능에 대한
  • [NH-2986]를 봐 업그레이드하고 체크 아웃하십시오.

    끄트머리는 JIRA

+0

3.3.3으로 업그레이드되었지만 문제가 지속됩니다. 어쩌면 문제는 쿼리의 구조입니다. – kagundajm

0

당신이 NHibernate에 부모 개체를 통해 자식 숙박 시설의 아동에 액세스하는 방법을 알고하지 않습니다

from d in Departments 
from s in d.Sections 
select new SectionModel 
{ 
DepartmentName = d.Name, 
SectionName = s == null ? String.Empty : s.Name 
} 
+0

throw됩니다 ** 지정된 메서드는 지원되지 않습니다 ** 예외입니다. – kagundajm

+0

실행중인 특정 linq 쿼리는 무엇입니까? 당신의 매핑은 어떻게 생겼습니까? NH 3.0 이상을 사용해 문제없이 쿼리를 실행했습니다. – Fran

+0

죄송합니다! 내 실수. 나는 부서에서 d에서 잘못된 방법을 부르고 있었다. 작동하지만 섹션이없는 부서가 있으므로 왼쪽 조인에 관심이있는 동안 내부 조인을 반환합니다. – kagundajm