How can I make nhibernate cascade save a set of objects with circular references?

Always perform your INSERT/UPDATE inside a transaction. Here's a working example using SQLite.

Always perform your INSERT/UPDATE inside a transaction. Here's a working example using SQLite: using System; using System. Data; using System.IO; using FluentNCfg; using FluentNCfg.

Db; using FluentNMapping; using N class Program { static void Main(string args) { if (File. Exists("data. Db3")) { File.

Delete("data. Db3"); } using (var factory = CreateSessionFactory()) { // Create schema and insert sample data using (var connection = factory. ConnectionProvider.GetConnection()) { ExecuteQuery("create table users(usr_id integer primary key, other_id int, usr_name string)", connection); } using (var session = factory.OpenSession()) using (var tx = session.

BeginTransaction()) { User u1 = new User() { Name = "User1" }; User u2 = new User() { Name = "User2" }; u1. Other = u2; u2. Other = u1; session.

Save(u1); tx.Commit(); } // Verify database state after inserts with ADO. NET using (var connection = factory. ConnectionProvider.GetConnection()) using (var command = connection.CreateCommand()) { command.

CommandText = "select usr_id, other_id, usr_name from users"; using (var reader = command.ExecuteReader()) { while (reader.Read()) { Console. WriteLine("usr_id: {0}, other_id: {1}, usr_name: {2}", reader. GetInt32(0), reader.

GetInt32(1), reader. GetString(2)); } } } } } private static ISessionFactory CreateSessionFactory() { return Fluently.Configure() . Database( SQLiteConfiguration.Standard.

UsingFile("data. Db3").ShowSql() ) . Mappings( m => m.FluentMappings.

AddFromAssemblyOf() ). BuildSessionFactory(); } static void ExecuteQuery(string sql, IDbConnection connection) { using (var command = connection.CreateCommand()) { command. CommandText = sql; command.ExecuteNonQuery(); } } } public class User { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual User Other { get; set; } } public class UserMap : ClassMap { public UserMap() { WithTable("users"); Id(x => x.

Id, "usr_id"); Map(x => x. Name, "usr_name"); References(x => x. Other) .

ColumnName("other_id") .Cascade.All(); } }.

Thanks darin. This is what I get for making stupid assumptions about sessions. – fostandy Jul 22 '09 at 7:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions