I am trying to configure op-locking using fluent nhibernate.
There is a lot of info out there but none seems to fit the scenario I'm in. My class and map are as follows (edited for brevity):
Entity:
public class EmailGroup : CRUDDomainObject<EmailGroup>
{
public virtual string Id { get; set; }
public virtual MailServer Server { get; set;}
public virtual string FromAddress { get; set;}
public virtual string ToAddress { get; set;}
public virtual long Version { get; set; }
}
Map:
public class EmailGroupMap : ClassMap<EmailGroup>
{
public const string TABLE_ID = "EMAILGROUP";
public const string FIELD_ID = "EMAILID";
public const string FIELD_MAIL_SERVER = "MAILSERVID";
public const string FIELD_FROM_ADDRESS = "EMLFROM";
public const string FIELD_TO_ADDRESS = "EMLTO";
public const string FIELD_VERSION = "VERSION";
public EmailGroupMap()
{
Table(TABLE_ID);
Id(x => x.Id)
.Column(FIELD_ID)
.Not.Nullable()
.GeneratedBy.Assigned()
.Length(12);
References(x => x.Server)
.Column(FIELD_MAIL_SERVER)
.NotFound.Ignore();
Map(x => x.FromAddress)
.Column(FIELD_FROM_ADDRESS)
.Not.Nullable()
.Length(120);
Map(x => x.ToAddress)
.Column(FIELD_TO_ADDRESS)
.Not.Nullable()
.Length(1000);
Version(X => X.Version)
.Column(FIELD_VERSION)
.Generated.Always()
.UnsavedValue("0")
.Access.Property();
DynamicUpdate();
OptimisticLock.Version();
}
}
All looks well to me here, but when I load the entity and modify it, the version number is not incremented. Likewise if I manually increment the version, while a session is open, I get no StaleObjectException.
Does this config look valid to the more experienced eye? If so what else could I be missing?
UPDATE:
After implementing a database managed timestamp the version column is (of course) being incremented. However NHibernate doesn't treat the row as optimistically locked. I captured the update query from the SQL server to check the where clause (truncated for brevity):
exec sp_executesql N'UPDATE [EMAILGROUP]
SET [EMLDESC] = #EMLDESC, [MAILSERVID] = #MAILSERVID, [EMLFROM] = #EMLFROM, [EMLTO] = #EMLTO, [EMLCC] = #EMLCC, [EMLBCC] = #EMLBCC
WHERE [EMAILID] = #EMAILID'
Why did you specify Generated.Always()? That tells NHibernate that this isn't a real column but instead calculated by the database. Documentation: http://nhibernate.info/doc/nh/en/index.html#mapping-generated
Remove that and it should work.
The most typical scneario for Version and SQL Server (not sure if this is your case) is the sql type timestamp (obsolete) or better rowversion. This should be mapped to C# byte[]. So these changes should solve it:
1) Version column on the server must be of type rowversion (or timestamp). Such a column is automatically updated on any changes related to current row. only one such column can exist per table
2) The entity should look like this
public class EmailGroup : CRUDDomainObject<EmailGroup>
{
...
public virtual byte[] Version { get; set; }
3) the fluent mapping code should remain as it is. It should be a job of a fluent mapper to do the tricks behind. what we need to achieve is something like this:
<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob">
<column name="Version" not-null="false" sql-type="timestamp"/>
</version>
Please, see more here: http://ayende.com/blog/3946/nhibernate-mapping-concurrency
Related
To start, yes, there are tons of similar questions here on Stack Overflow, but I've browsed them all and the problems that plague most of them look correct on my issue.
Basically, I'm trying to access an Object's Object via my query, and that could be the problem: is that not allowed? I can access the Id field, always, but any other member variable cannot be accessed.
Here are my Objects:
public class Logfiles
{
public virtual int Id { get; set; }
public virtual bool IsActive { get; set; }
public virtual DateTime LastReference { get; set; }
}
public class LogMerges
{
public virtual int Id { get; set; }
public virtual DateTime CreationDate { get; set; }
public virtual Logfiles Logfile { get; set; }
}
And here are my mappings:
class LogfilesMap : ClassMap<Logfiles>
{
public LogfilesMap()
{
Not.LazyLoad();
Table("logfiles");
Id(x => x.Id, "id").Not.Nullable().Unique().GeneratedBy.Identity().UnsavedValue(0);
Map(x => x.IsActive, "is_active").Not.Nullable().Default("true");
Map(x => x.LastReference, "last_reference").Not.Nullable();
}
}
class LogMergesMap : ClassMap<LogMerges>
{
public LogMergesMap()
{
Not.LazyLoad();
Table("logmerges");
Id(x => x.Id, "id").Not.Nullable().Unique().GeneratedBy.Identity().UnsavedValue(0);
Map(x => x.CreationDate, "creation_date").Not.Nullable();
References(x => x.Logfile, "logfile_id");
}
}
My table and columns have the names:
logfiles - id, is_active, last_reference
logmerges - id, creation_date, logfile_id
My code that I'm using to query:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.IsActive == true);
IEnumerable<LogMerges> logmerges = query.List().OrderBy(c => c.CreationDate);
Performing the Query causes the error and generates:
NHibernate.QueryException: could not resolve property: Logfile.IsActive of: LogMerges
The only thing I can guess is that I'm not allowed to do the "log.Logfile.IsActive" chain of objects in these queries. It compiles just fine and I can't see why I wouldn't be able to do this. If I change it to:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.Id == 0);
...the query goes through. However, if I try to access the other member variable:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.LastReference == DateTime.Now);
...I get a similar "could not resolve property" error message.
So if it turns out that I CANNOT do the Object chain of "log.Logfile.IsActive", what's the proper way to implement it? Do I need to perform a bunch of JOINs to do this?
Thanks in advance for any help!
Yes, you do need do need to join on Logfile. Nhibernate is ultimately going to turn your QueryOver query into SQL, so accessing a property on a referenced table doesn't make sense. In other words, you couldn't write the SQL:
select
*
from
logmerges
where
logmerges.logfile.last_reference = ...
Obviously you'd get a syntax error here--you'd need to join to LogFile:
select
*
from
logmerges
inner join logfiles on logfiles.id = logmerges.logfile_id
where
logfiles.last_reference = ...
And so therefore you can't write QueryOver like that either.
var query = session.QueryOver<LogMerges>()
.JoinQueryOver(lm => lm.LogFile)
.Where(lf => lf.LastReference == DateTime.Now)
.List<LogMerges>();
I have the following classes:
public class PhoneModel
{
public virtual ModelIdentifier SupportModels
}
public class ModelIdentifier
{
public virtual string Name
public virtual IList<string> Values
}
This is how i mapped it:
mapping.Component(x => x.SuppoertedModel, y =>
{
y.Map(x => x.Name, "FAMILY_ID");
y.HasMany(x => x.Values).Element("VALUE").Table("SUPPORTEDMODULS")
}
2 tables were created:
PhoneModel
column "FAMILY_ID"
SUPPORTEDMODELS
column "VALUE", "PHONE_MODEL_ID"
The problem is that when I am adding values, it will not save it to the SUPPORTEDMODELS table:
var pm = new PhoneModel();
pm.SupportedModels.Name = "11"
pm.SupportedModels.Values.Add("34");
you are missing HasMany(x => x.Values).Cascade.AllDeleteOrphan()
maybe session.Flush() or transaction.Commit() is missing in the test code
btw: if the order of the Values is not important then change it to ICollection<string> and HasMany().AsSet() since this allows NH to optimise some things and code can not rely on the order which is not relyable when using sql.
Using NHibernate you can set an identity seed like so:
<column name="Column1" not-null="true" sql-type="int IDENTITY(1,1000)"/>
The FluentNHibernate IdentityPart has CustomType and SqlCustomType methods, neither does it for me though. Is there a way to fluently set an identity seed?
More info:
When I do this: Map(x => x.Id).Column("CustomerId").CustomSqlType("int IDENTITY(1,1000)");
I get this error: The entity 'Customer' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
When I do this: Id(x => x.Id).Column("CustomerId").CustomSqlType("int IDENTITY(1,1000)");
I get this error: More than one column IDENTITY constraint specified for column 'CustomerId', table 'Customer'
Using FluentNHibernate 1.2.0.712.
I was able to duplicate that xml by doing something like this:
Map(x => x.LoginName, "Column1").CustomSqlType("int IDENTITY(1,1000)");
Edit:
If you can't achieve what you are wanting maybe you should explicitly map this using xml for now.
There is the article at the link below about implementing custom identity generator (see: Part 1: Inheriting from TableGenerator class) but the example throws the exception for SQLite database ("SQLite errorr no such table: hibernate_unique_key"). Thus as regard SQLite there is no possibility to gain current id key from a table. It uses class TableGenerator from NHibernate API (NHibernate.Id);
http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx
To avoid the exception I implemented another solution (especially the way of getting current Id). It takes advantage of Fluent-NHibernate API (GeneratedBy.Custom()). Look at the following source code:
public class MyAutoincrement<T> : IIdentifierGenerator where T : IId
{
#region IIdentifierGenerator Members
public object Generate(ISessionImplementor session, object obj)
{
NHibernate.ISession s = (NHibernate.ISession)session;
int seedValue = 1000;
int maxId = -1;//start autoincrement from zero! (fluent nhibernate start from 1 as default)
List<T> recs = s.Query<T>().ToList<T>();
if (recs.Count > 0)
{
maxId = recs.Max(x => x.getId());
}
return seedValue + maxId + 1;
}
#endregion
}
//Interface for access to current Id of table
public interface IId
{
int getId();
}
//Entity
public class MyEntity : IId
{
public virtual int Id { get; protected set; }
public virtual string MyField1 { get; set; }
public virtual string MyField2 { get; set; }
#region IId Members
public virtual int getId()
{
return this.Id;
}
#endregion
}
//Entity Mapping
public class MyEntityMap : ClassMap<MyEntity>
{
public MyEntityMap()
{
Id(x => x.Id).GeneratedBy.Custom<MyAutoincrement<MyEntity>>();
Map(x => x.MyField1);
Map(x => x.MyField1);
}
}
It works with SQLite database and involves custom identity seed.
Regards
Bronek
I have a class that has an enum type indicating whether the message type is Email or Sms. The enum type is defined:
public enum ReminderType
{
Email = 1,
Sms = 2
}
The class that utilizes this type looks like:
public class Reminder : EntityBase
{
public virtual string Origin { get; set; }
public virtual string Recipient { get; set; }
public virtual ReminderType Type { get; set; }
public virtual Business Business { get; set; }
public virtual DateTime Created { get; set; }
public Reminder()
{
Created = DateTime.UtcNow;
}
}
When I try to persist an entity of type Reminder to the database however, I get the following error:
System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value 'Email' to data type int.
The backing field is of type int, so I'm not sure why NHibernate is trying to map the string representation by default. I'm using Fluent NHibernate, and the relevant mapping code is:
mappings.Override<Reminder>(map =>
{
map.Map(x => x.Type).Column("Type")
});
I'm pretty sure the default behavior of NHibernate is to map enums as ints, so why is it not doing so in this case? I'm using SQL Server 2005, if that matters.
I am doing the same thing and got it working like so...
In my case EmployeeType is the enum class
Map(x => x.EmployeeType, "EmployeeType_Id").CustomType(typeof (EmployeeType));
I don't know why this person keeps posting and then deleting their comment or answer, but the link they provided () does answer my question. I opted not to go with a full blow class definition for the convention, but rather, an inline convention in the mappings code, like so:
var mappings = AutoMap.AssemblyOf<Business>()
.Where(x => x.IsSubclassOf(typeof(EntityBase)))
.IgnoreBase(typeof(EntityBase))
.Conventions.Add
(
ConventionBuilder.Id.Always(x => x.GeneratedBy.Identity()),
ConventionBuilder.HasMany.Always(x => x.Cascade.All()),
ConventionBuilder.Property.Always(x => x.Column(x.Property.Name)),
Table.Is(o => Inflector.Pluralize(o.EntityType.Name)),
PrimaryKey.Name.Is(o => "Id"),
ForeignKey.EndsWith("Id"),
DefaultLazy.Always(),
DefaultCascade.All(),
ConventionBuilder.Property.When(
c => c.Expect(x => x.Property.PropertyType.IsEnum),
x => x.CustomType(x.Property.PropertyType))
);
The last convention builder statement did the trick. I'm curious as to why Fluent NHibernate's default is to map enums as strings now. That doesn't seem to make much sense.
You should never map Enum as int in NHibernate. It becomes a reason of having a ghost updates.
The best way to it is just not setting a type property in XML mappings. To achieve that in Fluent NHibernate you can use .CustomType(string.Empty).
Some additional info you can find here.
How can I get fluent nhibernate to create a varbinary field in a sql server 2005 table that uses a field size of varbinary(max)? At the moment I always get a default of varbinary(8000), which isn't big enough as i'm going to be storing image files.
I've tried using CAstle.ActiveRecord but havent had any success yet.
[ActiveRecord]
public class MyFile : Entity
{
public virtual string FileName { get; set; }
public virtual string FileType { get; set; }
public virtual int FileVersion { get; set; }
public virtual int FileLength { get; set; }
[Property(ColumnType = "BinaryBlob", SqlType = "VARBINARY(MAX)")]
public virtual byte[] FileData { get; set; }
}
Been failing at finding a solution for hours now, so thanks in advance
czk
I'm not sure why your ActiveRecord example is not working, but there you might try setting the length of the column.
With Fluent NHibernate, you should be able to do
Map(x => x.FileData)
.WithLengthOf(2147483647)
I was having a similar issue with SQL FileStream and Fluent NHibernate where my BLOB writes were truncating at 8000 bytes. The following syntax finally fixed the problem:
Map(x => x.Bytes)
.CustomSqlType("VARBINARY (MAX) FILESTREAM")
.Length(2147483647)
.Not.Nullable();
In the mapping use:
Map(x => x.FileData).CustomSqlType("VARBINARY(MAX)");
You need an auto-mapping override:
public class MyFileMapOverride : IAutoMappingOverride<MyFile>
{
public void Override( AutoMapping<MyFile> mapping )
{
mapping.Map( x => x.FileData ).Length( int.MaxValue );
}
}
Since you're using Castle, you can tell it to wire up NHibernate with your mappings in your NHibernateInstaller:
public void Install( IWindsorContainer container, IConfigurationStore store )
{
container.Register( Component.For<ISessionFactory>()
.UsingFactoryMethod( k => BuildSessionFactory() )
.Named( "MySessionFactory" ) );
// Do other stuff...
}
private ISessionFactory BuildSessionFactory()
{
var mappings = AutoMap.AssemblyOf<MyFile>()
.IgnoreBase( typeof(Entity) )
.UseOverridesFromAssemblyOf<MyFileMapOverride>();
var configuration = ConfigurationUtility
.CreateConfiguration<WebSessionContext, DefaultProxyFactoryFactory>(
"MyDbConnection",
ConfigurationUtility.ForMsSql,
mappings,
NHibernateConfiguration.GetConfigurationPath() );
return configuration.BuildSessionFactory();
}
AFAIK, there is no such thing in Fluent NHibernate as "max", however, if you set the allowed length of a column to a real big value, it should work fine. You can check MSDN for what number max means for each datatype in SQL Server, although it may mean some very different number in others.
I used reflector and found this:
public MsSql2005Dialect()
{
base.RegisterColumnType(DbType.String, 0x3fffffff, "NVARCHAR(MAX)");
base.RegisterColumnType(DbType.AnsiString, 0x7fffffff, "VARCHAR(MAX)");
base.RegisterColumnType(DbType.Binary, 0x7fffffff, "VARBINARY(MAX)");
}
So, it seems that NHibernate creates max by default? Still, Fluent doesn't. (Although I don't know why.)
With the Auto mapping feature, you can use conventions to achieve it.
Example:
var cfg = new Configuration();
var persistenceModel = new AutoPersistenceModel();
persistenceModel.Conventions.Add(
new PropertyConvention(),
new ReferenceConvention(),
new HasManyConvention(),
ConventionBuilder.Property.Always(delegate(IPropertyInstance instance)
{
if (instance.Property.PropertyType == typeof(string))
instance.Length(16000);
else if (instance.Property.PropertyType == typeof(byte[]))
instance.Length(30000000);
}));
persistenceModel.AddTypeSource(new AssemblyTypeSource(Assembly.GetExecutingAssembly()));
persistenceModel.Where(t => t.Namespace.EndsWith("Entities"));
cfg.AddAutoMappings(persistenceModel);
return cfg.BuildSessionFactory();
For me, this suffices, but you can always use larger numbers.
If you don't use automapping, Dan Fitch's solution is the way to go, I guess.
First off, I (annoyingly) put the map file into the core project rather than the data project.
I still couldn't get it to work with a map file, but i wrote an xml file instead, writing the file length in - thanks for that Dan
<property name="FileName"/>
<property name="FileType"/>
<property name="VersionNo"/>
<property name="FileLength"/>
<property name="FileData" length="2147483647"/>