Automapping collection convention - fluent-nhibernate

ok, I have difficult time with automapping collections using fluent nhibernate. This time I tried to apply a collection convention which simply says to use camelCaseField with underscore. Well I got the convention loaded and I hit the breakpoint in the method below FNH still produces strange mapping. What I am doing wrong?
public class Parent
{
public virtual int Id { get; set; }
private IList<Child> _testCollection;
public virtual IList<Child> TestCollection
{
get
{
return _testCollection;
}
}
}
public class Child
{
public virtual int Id { get; set; }
}
public class CollectionAccessConvention : ICollectionConvention
{
public void Apply( ICollectionInstance instance )
{
instance.Access.CamelCaseField( CamelCasePrefix.Underscore );
}
}
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Test.Parent, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Parent`">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<bag access="nosetter.camelcase" name="TestCollection" mutable="true">
<key>
<column name="Parent_id" />
</key>
<one-to-many class="Test.Child, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
EDIT: #Bary: the strange thing is access="nosetter.camelcase". I think it should be access="field.camelcase-underscore". Any suggestions?

I figure it out.
Conventions work little bit strange but over time I think I will fully realize it. When you want to apply a convention about certain property or anything it will be applied only if it is not configured/set/defined already. So, when fluent compiles the mappings it automatically sets the readonly properties to access="nosetter.camelcase". Fortunately there is a way to fix this.
The solution:
You have to define your own automapping configuration by extend DefaultAutomappingConfiguration class and then override the method public virtual Access GetAccessStrategyForReadOnlyProperty(Member member) OR just implement the IAutomappingConfiguration interface. After you are done you can add this configuration when you initialize the fluent configuration.
Fluently.Configure( Configuration )
.Mappings( cfg =>
{
cfg.AutoMappings.Add( *yourIAutomappingConfiguration* )
}

Related

NH4 LINQ Query using wrong column for one-to-one

I have one-to-one relationship between two tables, and I want to be able to create a LINQ query that will return the "parent" tables where there is something in the child table. The problem is that the query NH is generating is checking to see if the parent table's ID is not null (and it never is) rather than joining to the child table. This is regardless of whether I use lazy or non-lazy loading. I'm using custom automap conventions with an override, but here's the HBM XML that gets generated:
Mapping for Abstract Class, Concrete Class that is Parent
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="AbstractClass, DomainObjects, Version=2.2.1.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" table="ABSTRACT_CLASS">
<id name="AbstractClassId" type="System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ABSTRACT_CLASS_ID" />
<generator class="identity" />
</id>
<joined-subclass name="ConcreteClass, DomainObjects, Version=2.2.1.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" table="CONCRETE_CLASS">
<key>
<column name="ABSTRACT_CLASS_ID" />
</key>
<one-to-one cascade="none" class="AuxiliaryClass, DomainObjects, Version=2.2.1.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" name="AuxiliaryClass" property-ref="Foo" />
</joined-subclass>
</class>
</hibernate-mapping>
Mapping for Child Table
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="AuxiliaryClass, DomainObjects, Version=2.2.1.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" table="AUXILIARY_CLASS">
<id name="AuxiliaryClassiD" type="System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="AUXILIARY_CLASS_ID" />
<generator class="identity" />
</id>
<many-to-one class="ConcreteClass, DomainObjects, Version=2.2.1.0, Culture=neutral, PublicKeyToken=aaaaaaaaaaaaaaaa" name="Foo">
<column name="FOO_ABSTRACT_CLASS_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Class Definitions
public abstract class AbstractClass
{
public virtual Int32? AbstractClassId { get; set; }
}
public class ConcreteClass : AbstractClass
{
public virtual AuxiliaryClass AuxiliaryClass { get; set; }
}
public class AuxiliaryClass
{
public virtual Int32? AuxiliaryClassId { get; set; }
public virtual ConcreteClass Foo { get; set; }
}
The LINQ query that isn't work is:
nh.Query<ConcreteClass>().Where(cc => cc.AuxiliaryClass != null);
The query that's being generated is:
select
concretecl0_.CONCRETE_CLASS_ID as CONCRETE1_0_
from
CONCRETE_CLASS concretecl0_
inner join
ABSTRACT_CLASS concretecl0_1_
on
concretecl0_.ABSTRACT_CLASS_ID=concretecl0_1_.ABSTRACT_CLASS_ID
where
concretecl0_.ABSTRACT_CLASS_ID is not null
If I turn off lazy loading, the joins out to the auxiliary table but still compares the concrete class's table's ID to null.
edit
Per #Suhas's suggestion:
Try changing your Linq query to nh.Query(cc => cc.AuxiliaryClass.AuxiliaryClassId > 0); assuming AuxiliaryClassId is of type int
I actually did cc => cc.AuxiliaryClass.AuxiliaryClassId != null, which worked, getting me this query:
select
concretecl0_.ABSTRACT_CLASS_ID as concretecl0_1_0_
from
CONCRETE_CLASS concretecl0_
inner join
ABSTRACT_CLASS concretecl0_1_
on
concretecl0_.concretecl0_=concretecl0_1_.concretecl0_
, AUXILIARY_CLASS auxiliaryc1_
where
concretecl0_.ABSTRACT_CLASS_ID=auxiliaryc1_.FOO_ABSTRACT_CLASS_ID
and (auxiliaryc1_.AUXILIARY_CLASS_ID is not null)
However, when I tried the inverse case, cc => cc.AuxiliaryClass.AuxiliaryClassId == null, I got a non-working query:
select
concretecl0_.ABSTRACT_CLASS_ID as concretecl0_1_0_
from
CONCRETE_CLASS concretecl0_
inner join
ABSTRACT_CLASS concretecl0_1_
on
concretecl0_.concretecl0_=concretecl0_1_.concretecl0_
, AUXILIARY_CLASS auxiliaryc1_
where
concretecl0_.ABSTRACT_CLASS_ID=auxiliaryc1_.FOO_ABSTRACT_CLASS_ID
and (auxiliaryc1_.AUXILIARY_CLASS_ID is null)
Just listing down my comments (slightly tailored to an answer) from the original questions as those seems to have helped the author of the question
Try changing your Linq query to nh.Query<ConcreteClass>(cc => cc.AuxiliaryClass.AuxiliaryClassId > 0); assuming AuxiliaryClassId is of type int
The above would not work when you want to fetch records present in ConcreteClass but not present in AuxiliaryClass. For that you might want to use left outer join. If you do left outer join, then you can do it via QueryOver (or Linq in a not so direct way) and then do the check for nullity in your code. Depending on the size of the dataset you load, this may not be a good thing to do in production. I am not sure how would do subquery to achieve what you are trying to achieve
if LINQ is the only option then you can use DefaultIfEmpty method to get left outer joins in LINQ. This SO question should be a good starting point.
Lastly a plain SQL could be most effective in this situation. If you want to go down that route then you can use ISession.CreateSQLQuery and use your SQL query as is

NHibernate could not resolve property of an inherited property

Why can't NHibernate access a property inherited from an abstract base class. When I try to use the property in a QueryOver in the Where clause I'm getting
could not resolve property: ID of: TheWorkshop.Web.Models.Customer
var customer = Session.QueryOver<Customer>()
.Where(c=>c.ID ==id)
.SingleOrDefault<Customer>();
Intelisense helped me build the query and the solution compiles, so there is an ID property on the Customer class. The ID property on Customer is inherited from an abstract Contact class that in turn inherits from a DomainEntity<T> which exposes a protected field.
public abstract class DomainEntity<T>
{
protected Guid _persistenceId;
//...
}
public abstract class Contact : DomainEntity<Contact>
{
public virtual Guid ID
{
get { return _persistenceId; }
}
public virtual Address Address
{
get { return _address; }
set { _address = value; }
}
//...
}
and in the mapping file
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TheWorkshop.Web"
namespace="TheWorkshop.Web.Models"
default-access="field.camelcase-underscore"
default-lazy="true">
<class name="Contact" table="Contacts" abstract="true">
<id name="_persistenceId" column="ID" type="Guid" access="field"
unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid.comb" />
</id>
<!-- ... -->
<union-subclass name="Customer" table="Customers">
Following the answer to a similar question I updated to NHibernate 3.3.3-CR1 from NHibernate 3.3.2.4000 but I still have the same issue.
The problem was that NHibernate couldn't infer from my mapping how to resolve the ID property. So although the classes compiled fine and the _persistenceId property on the abstract base class could be accessed through a getter on the implementing classes, because of the mismatch in names between _persistenceId and ID NHibernate wasn't able to follow that.
The (easier) solution was to change my names to match up. There is a harder solution which involves implementing the IProperyAccessor, IGetter and ISetter interfaces and in order to provide a path to pass the string ID in order to use the ClassName access strategy.
The simpler of the two solutions was just to rename _persistenceId to _id (and update all the references to it) so
<id name="_persistenceId" column="ID" type="Guid" access="field"
unsaved-value="00000000-0000-0000-0000-000000000000">
becomes
<id name="Id" column="Id" type="Guid"
unsaved-value="00000000-0000-0000-0000-000000000000">
Note I was also able to drop the access="field" in the updated id mappings

NHibernate one-to-many foreign key is NULL

I have a problem with NHibernate. What i am trying to to is very simple:
I have two Classes. UserTicket and UserData. A UsertTicket has some UserData and a UserData belongs to one UserTicket:
public class UserData{
public virtual int Id { get; set; }
public virtual String PDF_Path { get; set; }
}
public class UserTicket
{
public virtual int Ticketnr { get; set; }
public virtual IList<UserData> UserData { get; set; }
}
And here the mappig xml:
<class name="UserTicket" table="UserTicket">
<id name="Ticketnr">
<generator class="identity"/>
</id>
<bag name="UserData" inverse="true" cascade="all-delete-orphan" lazy="false">
<key column="FK_Ticketnr" not-null="false"/>
<one-to-many class="UserData" />
</bag>
</class>
<class name="UserData" table="UserData">
<id name="Id">
<generator class="identity"/>
</id>
<property name="PDF_Path" />
</class>
When i run it, it works, but the column FK_Ticketnr in the DB-Table of UserData is always = NULL.
Someone told me i have to make a back link from my Child (UserData) to the Parent-Class. But i can not figure out how to do so. How do i get Hibernate to write the value of the Primary-Key (Ticketnr) of UserTicket into FK_Ticketnr?
Thanks in advance
Remove inverse="true" on your bag. That is telling NHibernate that the relationship is managed from the UserData mapping. Since you don't have a back reference, it is never persisted. The other option is to put a parent reference on UserData, but I wouldn't recommend it if you don't need it.
The alternative is the following...
public class UserData
{
// Keep your other properties, add this one too...
public virtual UserTicket Ticket { get; set; }
}
Modify your mapping file like this...
<class name="UserData" table="UserData">
<id name="Id">
<generator class="identity" />
</id>
<property name="PDF_Path" />
<many-to-one name="Ticket" column="FK_Ticketnr" />
</class>
Keep the inverse="true" on the bag.
If you go with this approach, you will need to set Ticket on your UserData objects when you add them to the UserData bag. In other words, you need to maintain both sides of the relationship. You could do this manually or you could try to automate it a little with with methods or constructors.

Automatically removing associations when deleting entities in NHibernate

I have the following entities:
namespace NhLists {
public class Lesson {
public virtual int Id { get; set; }
public virtual string Title { get; set; }
}
public class Module {
public virtual int Id { get; set; }
public virtual IList<Lesson> Lessons { get; set; }
public Module() {
Lessons = new List<Lesson>();
}
}
}
And the following mappings:
<class name="Module" table="Modules">
<id name="Id">
<generator class="identity"/>
</id>
<list name="Lessons" table="ModuleToLesson"
cascade="save-update">
<key column="moduleId"/>
<index column="position"/>
<many-to-many
column="lessonId"
class="NhLists.Lesson, NhLists"/>
</list>
</class>
<class name="Lesson" table="Lessons">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Title">
<column name="Title" length="16" not-null="true" />
</property>
</class>
When I delete a lesson by session.Delete(lesson), is there anyway I can have NHibernate automatically update the association in Module.Lessons to remove the entry from the set? Or am I forced to go through all Modules and look for the lesson and remove that by hand?
Edit: Fixed ICollection and <set> in mappings to IList<> and <list> like I want and tested it.
You have false idea. If you want to delete the Lesson object from Module you do that manually. NHibernate just tracks such your action and when session.Commit() is called then the reference between Module and Lesson is deleted in the database.
Calling session.Delete(lesson) deletes the lesson object from database (if foreign keys are set properly then reference between Module and Lesson is deleted of course but it is not responsibility for NHibernate).
In conclusion, it is not possible to delete the lesson object from the Module.Lessons list automatically by calling session.Delete(lesson). NHibernate does not track such entity references.
Turns out that if we do not need IList semantics and can make do with ICollection the update problem can be solved by adding a reference back from Lesson to Module, such as:
public class Lesson {
...
protected virtual ICollection<Module> InModules { get; set; }
...
}
And to the mapping files add:
<class name="Lesson" table="Lessons">
...
<set name="InModules" table="ModuleToLesson">
<key column="lessonId"/>
<many-to-many column="moduleId" class="NhLists.Module, NhLists"/>
</set>
</class>
Then a Lesson deleted is also removed from the collection in Module automatically. This also works for lists but the list index is not properly updated and causes "holes" in the list.

nhibernate to save only required properties

I'm using NHibernate 2.2 for my database work and I've faced an issue recently. I have a class called PrescDrugItem which is shown below
public class PrescDrugItem
{
public virtual int ItemNumber { get; set; }
[DataMember]
public virtual int AmountIssued { get; set; }
[DataMember]
public virtual string TimePeriod { get; set; }
}
following is the mapping file
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly ="DataContractsLib"
namespace="DataContractsLib.Prescription" >
<class name="PrescDrugItem">
<id name="ItemNumber" type="Int32">
<generator class="native" />
</id>
<property name="AmountIssued" type="Int32" />
<property name="TimePeriod" type="String" length="30" />
</class>
my problem is, now I need to add another property to the class Item (say ItemTradeName etc), but I dont want it to be saved to the database( because I want to use this new property to store some data temporary). I tried update=false and insert=false in the mapping file but no success yet. Could you guys please tell me is this possible thing to do . Thank you.
If it's not to be fetched from the database either, just add it as a normal property of your class and don't map it.