views:

42

answers:

0

Hi

In the mapping-file I've defined a bag for Positionen.

  <class name="Animag.DomainModels.Models.Abrechnung, Animag.DomainModels" table="Abrechnungen" lazy="true">
    <id name="ID" column="ID">
      <generator class="native"/>
    </id>
    <version name="Version" column="Version"/>
    <many-to-one name="Kunde" column="KundeID" class="Animag.DomainModels.Models.Kunde" not-null="true"/>
    <many-to-one name="Abrechnungsart" column="AbrechnungsartID" class="Animag.DomainModels.Models.Abrechnungsart" not-null="true"/>
    <bag name="Positionen">
      <key column="AbrechnungID"/>
      <one-to-many class="Animag.DomainModels.Models.Position"/>
    </bag>


    <class name="Animag.DomainModels.Models.Position, Animag.DomainModels" table="Positionen" lazy="true">
      <id name="ID" column="ID">
        <generator class="native"/>
      </id>
      <version name="Version" column="Version"/>
      <property name="Positionsart" column="Positionsart" not-null="true"/>
      <many-to-one name="Abrechnung" column="AbrechnungID" class="Animag.DomainModels.Models.Abrechnung" not-null="false"/>
<many-to-one name="Gattung" column="GattungID" class="Animag.DomainModels.Models.Gattung" not-null="false"/>

They two mapping-files are not completely.

Now, I would like one Abrechnung with all Positionen and the Abrechnungsart and Gattung in this Abrechnung.

I have this criteria:

public DomainModels.Models.Abrechnung LoadAbrechnungWithPositionen(int abrechnungId, Positionsart positionsart) {
            using (var session = this.SessionManager.Session) {
                return session.CreateCriteria<DomainModels.Models.Abrechnung>()
                       .SetFetchMode("Positionen", NHibernate.FetchMode.Eager)
                       .CreateAlias("Positionen", "position")
                       .SetFetchMode("Abrechnungsart", NHibernate.FetchMode.Eager)
                       .SetFetchMode("position", NHibernate.FetchMode.Eager)
                       .SetFetchMode("position.Gattung", NHibernate.FetchMode.Eager)
                       .Add(Restrictions.Eq("ID", abrechnungId))
                       .Add(Restrictions.Eq("position.Positionsart", positionsart))
                       .SetResultTransformer(CriteriaSpecification.DistinctRootEntity)
                       .UniqueResult<DomainModels.Models.Abrechnung>();
            }
        }

On this way, I have no eager-loading for the Positionen. Why?

And on this way, I get this Exception: could not resolve property: Positionen.Positionsart of: Animag.DomainModels.Models.Abrechnung

 public DomainModels.Models.Abrechnung LoadAbrechnungWithPositionen(int abrechnungId, Positionsart positionsart) {
        using (var session = this.SessionManager.Session) {
            return session.CreateCriteria<DomainModels.Models.Abrechnung>()
                   .SetFetchMode("Positionen", NHibernate.FetchMode.Eager)
                   .SetFetchMode("Abrechnungsart", NHibernate.FetchMode.Eager)
                   .SetFetchMode("Positionen.Gattung", NHibernate.FetchMode.Eager)
                   .Add(Restrictions.Eq("ID", abrechnungId))
                   .Add(Restrictions.Eq("Positionen.Positionsart", positionsart))
                   .SetResultTransformer(CriteriaSpecification.DistinctRootEntity)
                   .UniqueResult<DomainModels.Models.Abrechnung>();
        }
    }

I've this property:

private IList<Position> _positionen;
public virtual IList<Position> Positionen {
            get { return _positionen; }
            set {
                _positionen = value;
                HasChange = true;
            }
        }

What's the best way to get my datas?