2009-01-10 85 views
1

我有兩個類之間的許多一對多的關係:比賽和球員Cascade.SaveOrUpdate(SQLite的)流利,NHibernate的

我設置Cascade.SaveUpdate的映射,但保存賽實例時,運動員將不會保存到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(); 
    } 
} 
+0

請顯示您用於將玩家和錦標賽保存到數據庫的代碼 – sirrocco 2009-01-11 12:46:13

回答

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:INSERT INTO「Tournament」(Id)VALUES(@ p0); NHibernate:INSERT INTO「Player」(Id)VALUES(@ p0); @ p0 ='65559bba-8603-4874-8a8f-9bf4018596df' NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 ='549e10​​7c-1339-4fab-8960-9bf4018596e8' @ 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:INSERT INTO「Player」(Id)VALUES(@ p0); NHibernate:INSERT INTO「Tournament」(Id)VALUES(@ p0); @ p0 ='35c078c5-1102-4c63-91ca-9bf40185971c' NHibernate:INSERT INTO PlayerToTournament(Tournament_id,Player_id)VALUES(@ p0,@ p1); @ p0 ='367898cf-5835-4e1b-9d7d-9bf40185971c' @ p0 ='367898cf-5835-4e1b-9d7d-9bf40185971c',@ p1 ='35c078c5-1102-4c63-91ca-9bf40185971c'

-2

總是在交易中進行保存。

乾杯