NHibernate - is property lazy loading possible? - nhibernate

I've got some binary data that I store and was going to separate this out into a separate table so it could be lazy loaded.
However, i then came across this post by Ayende (http://ayende.com/Blog/archive/2010/01/27/nhibernate-new-feature-lazy-properties.aspx) which suggests that property lazy loading is now possible.
I have added the lazy="true" attribute to my property mapping but the field is still loaded from the database (I am using a simple text field to test).
My query:
return _session.CreateQuery("from Product")
.SetMaxResults(1)
.UniqueResult<Product>();
Mapping:
<property name="Description" type="string" column="FullDescription" lazy="true"/>
Has anyone been able to get this working? Personally I prefer this approach than having to add another table to my database.

As the article states: "This feature is now available on the NHibernate trunk". So either NH 3.0 or the latest trunk.

It will be possible on nhibernate 3.0, on the current version is not possible. You can download the trunk code or wait for the 3.0 version :-)

Related

Upgrade from NH 1.2 to 2.1 and type="serializable" throwing serialization Exceptions

I have an old NH 1.2 project that I am updating to 2.1, and so far it's been a breeze, until I hit a wall with properties that were mapped to type="serializable" and sql server 2000.
I keep getting serializationExceptions and the inner exception is complaining that it cannot find the assembly for 1.2.
Is it even possible to update to 2.1 in this regard? I have seen some posts discussing setting the column length, but no avail.
<property name="Lines" type="Serializable" >
<column name="Lines" not-null="false" sql-type="varbinary(max)"/>
</property>
Is it merely a configuration issue, or am I likely to be hosed for existing data?
Edit:
I have also tried entering an assembly redirect in the off chance it might work. No love there either.
Answering my own question and not removing it for posterity.
If you run into this SerializationException while attempting to deserialize, where the inner exception is clearly looking for an older version of the NHibernate assembly, it is because the property you serialized either is an NHibernate proxy itself, or has a NHibernate proxy in it's graph.
Moral of the story: Be darned careful what you set to type="serializable" in your maps.

What is the NHibernate Mapping.ByCode equivalent of the "hbm2ddl.keywords" setting?

I just ran into a situation related to this SO question: How to tell NHibernate always to force quoted identifiers?
My entity has an "Order" property, and NH chokes trying to create the database. I am using the new Mapping.ByCode feature of NHibernate 3.2, and its not entirely clear where I need to apply the hbm2dll.keywords setting.
Thanks in advance!
Ah, found it: http://nhforge.org/blogs/nhibernate/archive/2009/06/24/auto-quote-table-column-names.aspx
When you have an instance of a configured configuration (just before call BuildSessionFactory) you can execute:
SchemaMetadataUpdater.QuoteTableAndColumns(configuration);

Entity Validation in Sharp Architecture Repository

I have created a new 1.6 Sharp Architecture project.
I have marked my only Entity with HasUniqueDomainSignatureAttribute and one string property marked DomainSignatureAttribute.
I create 2 entities with the same DomainSignature and I'm able to save them both thous having duplicates.
Am I missing some configuration? As I was under the impression that this would work out of the box.
Before saving you should manually check entity for validness. I don't know how it is done now, but in previos versions each entitity had a property IsValid.
Well yes, the Validation method IsValid() is on the Entities.
But from the documentation I get the impression that if using NHibernate and NHibernate.Validators the repositories should validate the entities before Insert and Update.
As in the documentation on nhforge.org
In SharpArch.Data.NHibernate.NHibernateSession following snippet is called each time Init() is used.
Those things make me wonder why it doesn't work.
I could roll my own, but that seems like waste if it's already there.
Can anyone point me in the right direction? Also the SchemaExport util should use the validators when generating the scripts.

Castle-ActiveRecord Tutorial with .NET 3.5 broken?

Has anyone tried the ActiveRecord Intro Sample with C# 3.5?
I somehow have the feeling that the sample is completely wrong or just out of date. The XML configuration is just plain wrong:
<add key="connection.connection_string" value="xxx" />
should be :
<add key="hibernate.connection.connection_string" value="xxx" />
(if I understand the nhibernate config syntax right..)
I am wondering what I'm doing wrong. I get a "Could not perform ExecuteQuery for User" Exception when calling Count() on the User Model.
No idea what this can be. The tutorial source differs strongly from the source on the page (most notably in the XML configuration), and it's a VS2003 sample with different syntax on most things (no generics etc).
Any suggestions? ActiveRecord looks awesome..
(This was too long for a comment post)
[#Tigraine] From your comments on my previous answer it looks like the error lies not with the configuration, but with one of your entities. Removing the "hibernate" corrected the configuration so that it geve you the real error, which appears to be that the entity "Post" is not properly attributed for ActiveRecord to create its mapping.
If you further down in the error that it gives, it likely has some details as to what about "Post" failed.
Some common things include:
THe class does not have the [ActiveRecord] attribute.
There is no property with the [PrimaryKey] attribute.
There is no matching table called "Post" (or "Posts" if PluralizeTableNames is "true").
There is no matching column(s) for attributed properties.
Your attributed properties and public methods are not virtual (this one kills me all the time).
The 'hibernate' portion of the key was removed in NHibernate version 2.0.
This version is correct for NHibernate 2.0 onwards:
<add key="connection.connection_string" value="xxx" />
Edit:
I see that the quickstart doesn't come with the binaries for Castle and NHibernate. You must have downloaded the binaries from somewhere; it would be helpful if you could provide the version number of your NHibernate.dll file.
Confusingly, at least SOME of the quickstart has been updated to be current with NHibernate (NH) 2.0, but the latest 'proper' Castle release is still the 1.0 RC3 (almost a year old now), which does not include NH 2.0.
You can go two ways. You can continue using Castle RC3 and in this case you will indeed need to add the 'hibernate' prefix to your configuration entries. Or you can download a build of Castle from the trunk, which should be running against NH 2.0. The problem with the latter approach is that some of the other breaking changes introduced in NH 2.0 might not be fixed in the quick start.
Delete the "hibernate." part for all configuration entries. Your first example is the correct one.

Spring.net + Nhibernate Integration Tests Pass When They Should Not

I'm using Spring.net with NHiberante (HibernateTemplate) to implement my DAO's.
I also have some integration tests, that extend from 'AbstractTransactionalDbProviderSpringContextTests '.
DI is working fine, and all test pass BUT sometimes they pass even when they shouldn't.
For example if my hbm.xml files have an error like this:
<class name="Confluence.Domain.User" table="THIS TABLE DOES NOT EXIST">
The tests fails, but if the error is like this one:
<many-to-many
class="Confluence.Domain.User"
column="THIS COLUMN DOES NOT EXIST"/>
the tests pass silently hiding the bug.
I'm testing it using SetComplete() and checking the DB for the changes, but I think the whole idea of this kind of tests is not to do so.
Can anyone tell me how to fix this issue?
Thank you very much!
#Ben: If I have to actually execute the SQL scripts to see if they work, what is the benefit of using this kind of Spring tests?
When testing your NH based DAO's you should flush the session so that the database is updated with the new information but still rollback as before. How to do this is explained here - http://forum.springframework.net/showthread.php?t=5246 I've added this to the reference docs. Hope this helps.
Cheers,
Mark
If you have a syntax error in your mapping, then NHibernate will fail on config.BuildSessionFactory()
But for mispelled/non-existence database objects, the only way for NHibernate to know is to actually run a query... So you might employ some integration tests to test insert/select on a single entity, to make sure it works.
Not sure what this has to do with Spring.NET though.