C# 언어를 사용하여 NHibernate를 사용하는 웹 응용 프로그램을 개발 중입니다. 그러나 몇 가지 필드만으로 네이티브 MySQL 쿼리를 작성한 다음 매핑 할 수는 없습니다. 나는 모든 괜찮 내 응용 프로그램을 실행하면NHibernate 및 네이티브 SQL을 사용하여 테이블에서 몇 개의 열만 선택
var session = this.GetFactory().OpenSession();
var query = session.CreateSQLQuery("SELECT * FROM `rule` WHERE currency = :currency AND `range` = :range");
query.SetParameter("currency", this.SearchRoot.Currency.Id);
query.SetParameter("range", this.SearchRoot.Range.Id);
query.AddEntity(typeof(Rule));
var rules = query.List<Rule>();
:
<class name="Rule" table="rule">
<id name="Id" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="Name" column="name" type="String" not-null="false"></property>
<property name="Description" column="description" type="String" not-null="false"></property>
<property name="Shops" column="shops" type="String" not-null="false"></property>
<property name="Channels" column="channels" type="String" not-null="false"></property>
<property name="Currency" column="currency" type="int" not-null="false"></property>
<property name="Range" column="range" type="int" not-null="false"></property>
<property name="Created" column="created" type="DateTime" ></property>
<property name="Modified" column="modified" type="DateTime" ></property>
</class>
내 기본 쿼리는 다음과 같습니다처럼
내 hbm.xml 보인다. 그런 다음
var session = this.GetFactory().OpenSession();
var query = session.CreateSQLQuery("SELECT id, shops, channels FROM `rule` WHERE currency = :currency AND `range` = :range");
query.SetParameter("currency", this.SearchRoot.Currency.Id);
query.SetParameter("range", this.SearchRoot.Range.Id);
query.AddEntity(typeof(Rule));
var rules = query.List<Rule>();
나는 다음과 같은 오류 있어요 :
예외 정보를 그러나이 특정 경우에 난 그냥 아이디, 상점, 나는 다음과 같이 변경했다 있도록 채널을 필요로하는 모든 필드가 필요하지 않습니다 : System.IndexOutOfRangeException : 결과에서 지정한 열을 찾을 수 없습니다. 이름 :
나는 NHibernate가 항상 테이블의 필드와 클래스 속성을 일치 시키려고한다는 것을 알고 있습니다. 네이티브 쿼리에 대한 설명서를 읽었습니다.
그러나 나는 어떤 샘플 또는이 특정 사건에 관련된 무언가를 찾을 수 없습니다.
어떤 도움이 필요합니까?
고맙습니다. 트랜스포머가이 경우의 해결책입니다. – afym