1

我有两个类之间的多对多关系:锦标赛和玩家

我在映射中设置了 Cascade.SaveUpdate,但是在保存 Tournament 实例时,Players 不会保存到 Players 表中。Nhibernate 只在链接表中写入父键列和子键列。数据库是 SQLite。

这些是映射

 public TournamentMap()
    {
        WithTable("Tournaments");
        Id(x => x.Name);
        Map(x => x.Start).Access.AsLowerCaseField();
        Map(x => x.End).Access.AsLowerCaseField();

        HasManyToMany<Player>(x => x.Players)
            .WithTableName("TournamentsPlayers")
            .WithParentKeyColumn("tournamentName")
            .WithChildKeyColumn("playerName").AsSet()
            .Access.AsLowerCaseField()
            .Cascade.SaveUpdate().LazyLoad();

        References(x => x.Type).Access.AsLowerCaseField()
            .TheColumnNameIs("typeName")
            .Cascade.SaveUpdate();
    }

  public class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
        WithTable("Players");
        Id(x => x.Name);

        HasManyToMany<Player>(x => x.Tournaments)
            .Access.AsLowerCaseField()
            .WithTableName("TournamentsPlayers")
            .WithParentKeyColumn("playerName")
            .WithChildKeyColumn("tournamentName").AsSet()
            .Cascade.SaveUpdate().IsInverse().LazyLoad();
    }
}
4

2 回答 2

5

也许晚了一点(或很多),但尽管如此,

我将您的示例简化为如下所示:

public class Tournament
{
    public virtual Guid Id { get; private set; }

    private readonly ISet<Player> players;

    public Tournament()
    {
        players = new HashedSet<Player>();
    }

    public virtual ISet<Player> Players
    {
        get { return players; }
    }

    public virtual void AddPlayer(Player player)
    {
        players.Add(player);
    }
}

public class Player 
{
    public virtual Guid Id { get; private set; }

    private readonly ISet<Tournament> tournaments;

    public Player()
    {
        tournaments = new HashedSet<Tournament>();
    }

    public virtual ISet<Tournament> Tournaments
    {
        get { return tournaments; }
    }

    public virtual void AddTournament(Tournament tournament)
    {
        tournaments.Add(tournament);
    }
}

public class TournamentMap : ClassMap<Tournament>
{
    public TournamentMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany<Player>(x => x.Players)
            .AsSet().Access.AsLowerCaseField()
            .Cascade.SaveUpdate();
    }
}
public class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();

        HasManyToMany<Tournament>(x => x.Tournaments)
            .Access.AsLowerCaseField()
            .AsSet()
            .Cascade.SaveUpdate().Inverse();
    }
}

第一个测试看起来像:

   [Test]
    public void CanSavePlayerAttachedToTournament()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(tournament);
        Session.Flush();
    }

并产生以下 sql:

NHibernate:插入“锦标赛”(Id)值(@p0);@p0 = '65559bba-8603-4874-8a8f-9bf4018596df' NHibernate:插入“玩家”(ID)值(@p0);@p0 = '549e10​​7c-1339-4fab-8960-9bf4018596e8' NHibernate: 插入 PlayerToTournament (Tournament_id, Player_id) 值 (@p0, @p1); @p0 = '65559bba-8603-4874-8a8f-9bf4018596df',@p1 = '549e10​​7c-1339-4fab-8960-9bf4018596e8'

第二个测试看起来像:

    [Test]
    public void CanSaveTounamentAttachedToPlayer()
    {
        Player player = new Player();
        Tournament tournament = new Tournament();
        player.AddTournament(tournament);
        tournament.AddPlayer(player);
        Session.Save(player);
        Session.Flush();
    }

并产生以下 sql:

NHibernate:插入“播放器”(Id)值(@p0);@p0 = '35c078c5-1102-4c63-91ca-9bf40185971c' NHibernate:插入“锦标赛”(Id)值(@p0);@p0 = '367898cf-5835-4e1b-9d7d-9bf40185971c' NHibernate: 插入 PlayerToTournament (Tournament_id, Player_id) 值 (@p0, @p1); @p0 = '367898cf-5835-4e1b-9d7d-9bf40185971c',@p1 = '35c078c5-1102-4c63-91ca-9bf40185971c'

于 2009-04-24T03:44:47.837 回答
-2

始终在交易中保存。

干杯

于 2009-10-22T06:36:54.213 回答