0

NHibernate에 익숙하며 v4.0.30319를 사용하고 있습니다. 공식 웹 사이트에 나타나지 않는 것 같습니다 (왜 이해가 안되나요?) 그래서 저는 좋은 문서를 찾는 데 어려움을 겪고 있습니다.NHibernate 4 왼쪽 외부 조인 데이터베이스에서 하나의 쿼리로

내가 원하는 것은 간단합니다. 내가 3 개 테이블이 있습니다

  • 클라이언트
  • ClientAnswer
  • 질문 표 질문에 그래서

나는 클라이언트 나 클라이언트가을에서 테이블에 가입 ClientAnswer에, 질문이 각 질문 (있는 경우)에 대한 고객의 답변이 있습니다. 고객이 ClientAnswer에 행이 없으므로 아직 질문에 응답하지 않았을 수 있습니다.

나는 매핑 FluentNHibernate를 사용하고 있는데 다음과 같이이다 :

public ClientMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.Name).Not.Nullable(); 
     Map(x => x.Notes).Nullable(); 
     Map(x => x.IsDeleted).Not.Nullable(); 
    } 


public QuestionMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.Description).Not.Nullable(); 
     Map(x => x.IsDeleted).Not.Nullable(); 
    } 

public ClientAnswerMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.Value).Not.Nullable(); 
     Map(x => x.IsDeleted).Not.Nullable(); 

     References<Client>(x => x.Client, "ClientId"); 
     References<Driver>(x => x.Question, "QuestionId"); 
    } 

내가 CustomThing의 목록을 반환하는 함수를 구현하기 위해 각각의 CustomThing 질문과 특정 클라이언트의 답변을 포함됩니다 싶습니다 (존재하는 경우는 null).

선택 * 질문 Q 에서 왼쪽 외부 조인 ClientAnswer 캘리포니아 에 ca.QuestionId =

IEnumerable<CustomThing> GetQuestionsAndAnswers(int clientId) 

SQL에서

public class CustomThing 
{ 
    public Question Question { get; set; } 
    public ClientAnswer ClientAnswer { get; set; } //it could be null 
} 

로 간단하게 내 사용자 정의 개체 인 내가 좋아하는 뭔가를 할 수 q.Id

그런 다음 ClientAnswers의 필터가 ClientId가 아닌 클라이언트를 필터링하여 찾고있는 클라이언트가 아닌 경우 CustomThing의 열거에있는 ults. 편집을 할

, 내가 SQL :

당신을 감사하지 괜찮아, 내가 어떤 제안에 열려입니다

(내가 제대로 설명 희망) : 나는 순간이에 있습니다. 그러나 이것은 매우 비효율적이며 데이터베이스에 대한 다중 액세스 권한을 가지고 있습니다. 그러나 경우에 그것은 내가 좋아하는 것이 무엇의 아이디어를 가지고 보여줍니다

public IEnumerable<CustomThing> GetQuestionsAndAnswers(int clientId) 
    { 
     IList<Question> allQuestions = _session.QueryOver<Question>() 
      .WhereNot(x => x.IsDeleted).List(); 

     IList<CustomThing> customThings= new List<CustomThing>(); 

     foreach (Question q in allQuestions) 
     { 
      CustomThing customThing= new CustomThing(); 
      customThing.Question = q; 
      customThing.ClientAnswer= GetClientAnswer(clientId, q.Id); 

      customThings.Add(customThing); 
     } 

     return customThings; 
    } 

    private ClientAnswer GetClientAnswer(int clientId, int questionId) 
    { 
     var clientAnswer = _session.QueryOver<ClientAnswer>() 
      .Where(x => x.Client.Id == clientId) 
      .Where(x => x.Question.Id == questionId) 
      .WhereNot(x => x.IsDeleted).SingleOrDefault(); 

     return clientAnswer; //can be null if the client did not answer this question 
    } 

답변

0

당신이 당신의 질문 클래스는 다시 ClientAnwsers 클래스에 연결하면 당신처럼, 당신의 allQuestions 쿼리에 ClientAnwsers 가입 왼쪽 수에 가방을 추가하는 경우 위의 SQL 쿼리에서.

저는 실제로 FluentNHibernate를 사용하지 않기 때문에 정확히 어떻게 표시 할 수 없습니다. 하지만 어쩌면이 세 링크는 도움 :

NHibernate Mapping - Set

Fluent mapping - HasMany/one-to-many

NOtherDev: Mapping-by-Code - Set and Bag