How to fluent configure optimistic locking and dynamic updates with automapping?

727 views
Skip to first unread message

Sebastian P.R. Gingter

unread,
Oct 17, 2009, 10:55:49 AM10/17/09
to Fluent NHibernate
Hello,

I'm new here and currently making my first steps with fluent
NHibernate.

I have a quite simple model right now and want to add dynamic updates
and optimistic locking with my automapped model.

To make things easier, I created a NHibernateBaseClass providing the
Id and Version property:

NHibernateBaseClass = public abstract class
public
property Id: Integer read protected write; virtual;
property Version: DateTime read protected write; virtual;
end;

(The Language used is Oxygene btw., so don't get disturbed by that
pascal look ;-) ).


My current configuration is like that:

class method DatabaseConfiguration.CreateSessionFactory:
ISessionFactory;
begin
exit Fluently.Configure()
.Database(
MySQLConfiguration.Standard.ConnectionString(cs -> cs
.Server('localhost')
.Database('NHibernateTEST')
.Username('NHibernateTEST')
.Password('NHibernateTEST')))
.Mappings(m ->
m.AutoMappings
.Add(
AutoMap.AssemblyOf<Customer>()
.IgnoreBase<NHibernateBusinessLogic.PersistenceBase.NHibernateBaseClass>
()
.Where(t -> t.Namespace =
'NHibernateBusinessLogic.BusinessObjects')
))
.ExposeConfiguration(new Action<Configuration>(BuildSchema))
.BuildSessionFactory();
end;

class method DatabaseConfiguration.BuildSchema(config: Configuration);
begin
config.SetProperty('hbm2ddl.auto', 'create');
end;


Now, how can I add dynamic updates and optimistic locking that uses
the Version datetime field introduced on the base class?

Thanks in advance.

Regards,

Sebastian

Sebastian P.R. Gingter

unread,
Oct 19, 2009, 7:12:41 AM10/19/09
to Fluent NHibernate
Hi,

okay, there is new information on this issue.

I figured out how it _should_ work when adding dynamic updates and
optimistic locking with a version field.
I use overrides on the Automappings to manage this. But when I add the
DateTime property called Version to the Version override, I get a
runtime error: "Error: Common Language Runtime detected an invalid
program."

When I extract the overrides into a seperate override class for my
business object, then I'll get an error also at the
Version mapping, where it states that System.DateTime cannot be used
as a return value for System.Object.

Now I'm really lost at this one.



First configuration version (Invalid Program):

class method DatabaseConfiguration.CreateSessionFactory:
ISessionFactory;
begin
exit Fluently.Configure()
.Database( getDB(DBType.MSSql) ) // a normal DB config
.Mappings(m ->
m.AutoMappings
.Add(
AutoMap.AssemblyOf<Customer>()
.IgnoreBase<NHibernateBusinessLogic.PersistenceBase.NHibernateBaseClass>
() // exclude the base class
.Where(t -> t.Namespace =
'NHibernateBusinessLogic.BusinessObjects') // my BO namespace
.Override<Customer>(map -> map.DynamicUpdate())
.Override<Customer>(map -> map.Version(x -> x.Version)) //
<-- this is the problematic line
))
.ExposeConfiguration(new Action<Configuration>(BuildSchema))
.BuildSessionFactory();
end;

class method DatabaseConfiguration.BuildSchema(config: Configuration);
begin
config.SetProperty('hbm2ddl.auto', 'create');
end;


Second one with external overrides:
m.AutoMappings
.Add(
AutoMap.AssemblyOf<Customer>()
.IgnoreBase<NHibernateBusinessLogic.PersistenceBase.NHibernateBaseClass>
()
.Where(t -> t.Namespace =
'NHibernateBusinessLogic.BusinessObjects')
.UseOverridesFromAssemblyOf<CustomerOverrides>()
))

where my CustomerOverrides looks like this:

CustomerOverrides = public class (IAutoMappingOverride<Customer>)
public
method &Override(mapping: AutoMapping<Customer>);
end;

implementation

method CustomerOverrides.&Override(mapping: AutoMapping<Customer>);
begin
mapping.DynamicUpdate();
mapping.Version(x -> x.Version); // System.DateTime cannot be used
for System.Object as a return value
end;




I have this base class:

NHibernateBaseClass = public abstract class
public
property Id: Integer read protected write; virtual;
property Version: DateTime read protected write; virtual;
end;

and some classes derived from that, like my Customer:

Customer = public class (NHibernateBaseClass)
public
property Name: String; virtual;
property Description: String; virtual;
property Projects: IList<Project> := new List<Project>; virtual;
property Users: IList<User> := new List<User>(); virtual;
end;

For test reasons you can leave out the lists of Projects and Users.
They're not important at all.

Regards,

Sebastian

Sebastian P.R. Gingter

unread,
Oct 19, 2009, 12:18:03 PM10/19/09
to Fluent NHibernate
Hi,

sorry, never mind. Got that sorted out by myself.
When comparing that to the very same thing in C# (where it worked)
this seem to be a bug in the Delphi Prism compiler.

Regards,

Sebastian
Reply all
Reply to author
Forward
0 new messages