views:

89

answers:

2

Hello all,

I'm using FluentNHibernate and Linq To Nhibernate, with these entities (only the relevant parts):

public class Player : BaseEntity<Player>
{
    private readonly IList<PlayerInTeam> allTeams = new List<PlayerInTeam>();

    public IEnumerable<Team> Teams
    {
        get
        {
            return from playerInTeam in allTeams
                   where playerInTeam.Roster.Match == null
                   select playerInTeam.Roster.Team;
        }
    }
}

public class PlayerInTeam : BaseEntity<PlayerInTeam>
{
    public int PlayerNumber { get; set; }
    public Player Player { get; set; }
    public Position Position { get; set; }
    public Roster Roster { get; set; }
}

public class Roster : BaseEntity<Roster>
{
    private readonly IList<PlayerInTeam> players = new List<PlayerInTeam>();

    public Team Team { get; set; }
    public IEnumerable<PlayerInTeam> Players { get { return players; } }
}

public class Team : BaseEntity<Team>
{
    private readonly IList<Roster> allRosters = new List<Roster>();

    public Team(string name, Sex sex)
    {
        allRosters.Add(new Roster(this));
    }

    public Roster DefaultRoster
    {
        get { return allRosters.Where(r => r.Match == null).First(); } 
    }
}

and the matching mappings:

public class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
        HasMany<PlayerInTeam>(Reveal.Member<Player>("allTeams"))
            .Inverse()
            .Cascade.AllDeleteOrphan()
            .Access.CamelCaseField();
    }
}

public class PlayerInTeamMap : ClassMap<PlayerInTeam>
{
    public PlayerInTeamMap()
    {
        References(pit => pit.Player)
            .Not.Nullable();

        References(pit => pit.Roster)
            .Not.Nullable();
    }
}

public class RosterMap : ClassMap<Roster>
{
    public RosterMap()
    {
        References(tr => tr.Team)
            .Not.Nullable();

        HasMany<PlayerInTeam>(Reveal.Member<Roster>("players"))
            .Inverse()
            .Cascade.AllDeleteOrphan()
            .Access.CamelCaseField();
    }
}

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        HasMany<Roster>(Reveal.Member<Team>("allRosters"))
            .Inverse()
            .Cascade.AllDeleteOrphan()
            .Access.CamelCaseField();
    }
}

I have this repository method:

public IEnumerable<Player> PlayersNotInTeam(Team team)
{
    return from player in Session.Linq<Player>()
            where !player.Teams.Contains(team)
            select player;
}

Which gives me this exception: NHibernate.QueryException: could not resolve property: Teams of: Emidee.CommonEntities.Player [.Where(NHibernate.Linq.NhQueryable`1[Emidee.CommonEntities.Player], Quote((player, ) => (Not(.Contains(player.Teams, p1, )))), )]

I've looked inside the hbm file, generated by Fluent NHibernate, concerning Player, and here is what I get:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Emidee.CommonEntities.Player, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" table="Players">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="increment" />
    </id>
    <bag access="field.camelcase" cascade="all-delete-orphan" inverse="true" name="allTeams" mutable="true">
      <key>
        <column name="Player_id" not-null="true" />
      </key>
      <one-to-many class="Emidee.CommonEntities.PlayerInTeam, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" />
    </bag>
  </class>
</hibernate-mapping>

Indeed, I don't have a Teams property for Player in that file.

But shouldn't my mapping take care of this? Where do you think the problem is?

Thanks in advance

Mike

A: 

The hbm.xml file is your mapping. So, as long as you do not inform NHibernate that your Player class has a Teams property, NHibernate doesn't know anything about it.

Next to that, why do you have an hbm.xml mapping file, and a mapping using Fluent ? (It seems that the hbm.xml file has priority/precedes the Fluent NH mapping).

Frederik Gheysels
I ask FluentNhibernate to generate the files for me. So this is *Fluent NH's mapping* ;)
Mike
+1  A: 

Technically, it's not possible to look inside a method body in C-sharp. So FluentNhibernate can not do that as well. You have to apply a different strategy to model your entities and/or generate the mapping yourself.

Paco
What would you recommend me to do?
Mike
`public IList<Player> PlayersNotInTeam(Team team){ return Session.Linq<Roster>() .Where(roster => roster.Team != team) .Select(roster => roster.Players) .Select(playerInTeam => playerInTeam.Player) .Distinct() .ToList()}` EDIT: I cannot format the code in comment?I can more options. The best option depends on how your applications uses your domain. That is why I cannot recommend what's the best option for you.
Paco
Thanks for your code, but I couldn't make it work.I've then resorted to HQL, and after a little bit of researches, I could make it.
Mike