Skip to content

Instantly share code, notes, and snippets.

@chilversc
Created January 25, 2010 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/285752 to your computer and use it in GitHub Desktop.
Save chilversc/285752 to your computer and use it in GitHub Desktop.
NHibernate insert/update problem
<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" name="ValueMapping.Fob, ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Fob`">
<id type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="native" />
</id>
<property name="Something" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Something" />
</property>
</class>
</hibernate-mapping>
<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" name="ValueMapping.Foo, ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Foo`">
<id type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="native" />
</id>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<set name="Aliases">
<key>
<column name="Foo_id" />
</key>
<element type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="name" />
</element>
</set>
<set cascade="all-delete-orphan" name="Bars">
<key>
<column name="Foo_id" />
</key>
<composite-element class="ValueMapping.Bar, ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<property name="Rate" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Rate" />
</property>
<property name="Value" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Value" />
</property>
</composite-element>
</set>
<set cascade="all-delete-orphan" name="Fobs">
<key>
<column name="Foo_id" />
</key>
<one-to-many class="ValueMapping.Fob, ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</set>
</class>
</hibernate-mapping>
-- statement #1
INSERT INTO [Foo]
(Name)
VALUES ('bob' /* @p0 */)
select SCOPE_IDENTITY()
-- statement #2
INSERT INTO [Fob]
(Something)
VALUES ('You are standing in an open field west of a big white house' /* @p0 */)
select SCOPE_IDENTITY()
-- statement #3
INSERT INTO [Fob]
(Something)
VALUES ('It is very dark, you are likely to be eaten by a Grue' /* @p0 */)
select SCOPE_IDENTITY()
-- statement #4
INSERT INTO [Fob]
(Something)
VALUES ('You were eaten by a Grue' /* @p0 */)
select SCOPE_IDENTITY()
-- statement #5
UPDATE [Fob]
SET Foo_id = 1 /* @p0 */
WHERE id = 1 /* @p1 */
-- statement #6
UPDATE [Fob]
SET Foo_id = 1 /* @p0 */
WHERE id = 2 /* @p1 */
-- statement #7
UPDATE [Fob]
SET Foo_id = 1 /* @p0 */
WHERE id = 3 /* @p1 */
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
namespace ValueMapping
{
public class Program
{
private static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
if (Directory.Exists("schema"))
Directory.Delete("schema", true);
Directory.CreateDirectory("schema");
var cfg =
Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2005
.ConnectionString(x => x.Server(".").Database("sandbox").TrustedConnection())
.ProxyFactoryFactory<NHibernate.ByteCode.LinFu.ProxyFactoryFactory>()
)
.Mappings(x =>
x.FluentMappings
.Add<FooMapping>()
.Add<FobMapping>()
.ExportTo("schema"))
.BuildConfiguration();
new SchemaExport(cfg).Drop(false, true);
new SchemaExport(cfg).Execute(false, true, false);
var sf = cfg.BuildSessionFactory();
object id;
using (var session = sf.OpenSession())
using (var t = session.BeginTransaction())
{
var f = new Foo();
f.Name = "bob";
//f.Aliases.Add("fred");
//f.Aliases.Add("mary");
//f.Bars.Add(new Bar {Rate = 5, Value = 10});
//f.Bars.Add(new Bar {Rate = 20, Value = 3});
f.Fobs.Add(new Fob {Something = "You are standing in an open field west of a big white house"});
f.Fobs.Add(new Fob {Something = "It is very dark, you are likely to be eaten by a Grue"});
f.Fobs.Add(new Fob {Something = "You were eaten by a Grue"});
id = session.Save(f);
t.Commit();
}
using (var session = sf.OpenSession())
using (var t = session.BeginTransaction())
{
var f = session.Get<Foo>(id);
//f.Aliases.Remove("fred");
//f.Bars.Remove(new Bar {Rate = 20, Value = 3});
f.Fobs.Remove(f.Fobs.First());
t.Commit();
}
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Stop();
Thread.Sleep(1000);
}
}
public class Foo
{
public Foo()
{
Aliases = new HashSet<string>();
Bars = new HashSet<Bar>();
Fobs = new HashSet<Fob>();
}
public virtual string Name { get; set; }
public virtual ICollection<string> Aliases { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
public virtual ICollection<Fob> Fobs { get; set; }
}
public class Bar
{
public virtual int Value { get; set; }
public virtual int Rate { get; set; }
public bool Equals(Bar other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.Value == Value;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Bar)) return false;
return Equals((Bar) obj);
}
public override int GetHashCode()
{
return Value;
}
}
public class Fob
{
public virtual string Something { get; set; }
}
public sealed class FooMapping : ClassMap<Foo>
{
public FooMapping()
{
Id<int>("id").GeneratedBy.Native();
Map(x => x.Name);
HasMany(x => x.Aliases).AsSet().Element("name");
HasMany(x => x.Bars)
.AsSet()
.Cascade.AllDeleteOrphan()
.Component(
c =>
{
c.Map(x => x.Rate);
c.Map(x => x.Value);
}
);
HasMany(x => x.Fobs).AsSet().Cascade.AllDeleteOrphan();
}
}
public sealed class FobMapping : ClassMap<Fob>
{
public FobMapping()
{
Id<int>("id").GeneratedBy.Native();
Map(x => x.Something);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment