7

나는 Patient이라는 엔티티 객체를 가지고 있는데,이 엔티티는 VisitsCollection 타입 인 Visits이라는 속성을 가지고있다.NHibernate 커스텀 콜렉션 타입

VisitsCollections은 하위 클래스 인 IList<Visit>이지만 자동 주문, 일부 유효성 검사, 알림 등과 같은 일부 맞춤 로직을 컬렉션에 추가합니다. 이 컬렉션에 추가 된 개체에 일부 데이터를 추가하고 투명하게 다른 서류를 수행으로

나는 사용자 지정 컬렉션 형식을 사용하는 필요 .

는 지금은 자 NHibernate에 그 매핑 할, 그래서 만들었습니다

<list name="Visits" lazy="true" fetch="select"> 
    <key foreign-key="PatientId" /> 
    <index column="Timestamp" /> 
    <one-to-many class="Visit" not-found="ignore"/> 
</list> 

나는 예외를 받고 있어요 :

NHibernate.Collection '형식의 개체를 캐스팅 할 수 없습니다. PersistentList '...'VisitsCollection '

visits 속성에 액세스 할 때마다.

나는 또한이 방법으로 매핑 할 시도했다 :

<list name="Visits" lazy="true" fetch="select" collection-type="VisitsCollection"> 
    <key foreign-key="PatientId" /> 
    <index column="Timestamp" /> 
    <one-to-many class="Visit" not-found="ignore"/> 
</list> 

을하지만 여전히, 나는이 예외를 받고 있어요 :

사용자 정의 유형은 UserCollectionType를 구현하지 않습니다 ..... VisitsCollection

컬렉션 클래스는 나는 그것이 DAL-agnos되고 싶어 프레임 워크의 일부로서 내가 어떤 NHibernate에 유형에서 내 VisitsCollection을 상속하지 않으려는

tic (데이터베이스와 함께 많은 시나리오에서 사용됨).

내 코드의 구조를 보존하면서이를 매핑하는 방법에 대한 아이디어가 있습니까?

미리 감사드립니다.

답변

6

주로 게으름이기 때문에 사용자 지정 컬렉션 형식을 사용하지 않습니다. NHibernate는 내가 믿는 IUserCollectionType을 사용하기를 원한다. 약간의 배관이 필요하다.

그보다 먼저 내 생각은 확장 방법을 discussed by Billly McCafferty으로 사용하는 것입니다. 그러나 당신은 코드가 이렇게 작성된 것입니다 ...

또는 컬렉션을 discussed here by Colin Jack과 같은 구성 요소로 매핑 할 수 있습니다. 시나리오에 따라 더 쉬울 수 있습니까?

또한 확인하십시오 (SO thread).

1

나는 맞춤식 컬렉션을 사용하지 않기로 동의합니다. 어쨌든 구성 요소를 통해이 작업을 수행 할 수 있습니다.

<component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core"> 
<set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" > 
    <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" /> 
    <!--This is used to set the type of the collection items--> 
    <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/> 
</set> 

How to map NHibernate custom collection with fluentNHibernate?

0

그냥 참조를 위해, 여기에 우리가 또는 사용자 지정 컬렉션 형식을 생성하지 말아야 여부 FluentNHibernate

이럴 별도의 주제

이다 사용하여 할 수있는 방법입니다
public class PatientOverride : IAutoMappingOverride<Patient> 
{ 
     public void Override(AutoMapping<Patient> mapping) 
     { 
      mapping.Component(
       x => x.Visits, 
       part => 
       { 
        part.HasMany(Reveal.Member<VisitsCollection, IEnumerable<Visit>>("backingFieldName")) // this is the backing field name for collection inside the VisitsCollection class 
        .KeyColumn("PatientId") 
        .Inverse(); // depends on your use case whether you need it or not 
       }); 
     } 
} 
관련 문제