FluentNHibernate: Id Column Name Ignored - nhibernate

FluentNHibernate: 1.3.0.733
NHibernate: 3.3.1.4000
I'm trying to set the column name of the Id column, but it seems to be ignored.
Edit:
Found solution. Property redeclaration (new-modifier) was the problem (see answer).
I'm using AutoMapping with conventions and overrides.
Override:
public class OrderHeadMapping : IAutoMappingOverride<OrderHead>
{
public void Override(AutoMapping<OrderHead> mapping)
{
mapping.Schema("[database].[dbo]");
mapping.Table("OrderHeads");
mapping.Id(x => x.Id, "desiredColumnName")
.Column("desiredColumnName")
.GeneratedBy.UuidString();
...
}
}
This code gets executed, but the column name stays "Id".
I've already exported the mappings to an directory to see what's the outcome:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" schema="[database].[dbo]" name="OrderHead, Core, Version=1.0.4666.19686, Culture=neutral, PublicKeyToken=null" table="OrderHeads">
<cache usage="read-write" />
<id name="Id" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="assigned" />
</id>
...
</class>
</hibernate-mapping>
I've searched my whole solution for ".Id(" and ".Column(" to ensure it is not accidently reset but none of the results deal with setting/overwriting the id column name. So now I'm a little bit lost.

I finally found the problem:
One thing I left out in my description is that I have two base entities:
public abstract class Entity
{
protected object _id;
public virtual object Id
{
get { return _id; }
protected internal set { _id = value; }
}
}
public abstract class Entity<TKey> : Entity
{
public Entity()
{
_id = default(TKey);
}
new public virtual TKey Id
{
get { return (TKey)_id; }
protected internal set { _id = (TKey)value; }
}
...
}
The problem was that FluentNHibernate handles redefined properties twice: Entity.Id and Entity<>.Id ultimately overwriting the desired mapping with the mapping of the base class version.
So I have to walk the inheritance tree up to check if this member is the top most rewrite (if any).
Now I handle the problem in the ShouldMap method in my implementation of DefaultAutomappingConfiguration:
public override bool ShouldMap(Member member)
{
var res = base.ShouldMap(member);
if (res == true)
{
var originalDeclaringType = GetOriginalDeclaringType(member.MemberInfo);
...
if(member.Name == "Id")
{
if (GetTopMostRedefinedMember(member.MemberInfo) != member.MemberInfo.DeclaringType)
return false;
}
}
...
return res;
}
with GetTopMostRedefinedMember being:
private Type GetTopMostRedefinedMember(MemberInfo member)
{
List<Type> types = new List<Type>();
Type type = member.ReflectedType;
while (type != null)
{
types.Add(type);
type = type.BaseType;
}
foreach (var t in types)
{
var tmp = t.GetMember(member.Name, BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.DeclaredOnly);
if (tmp.Length != 0)
{
type = t;
break;
}
}
return type;
}
Disclaimer: This code is not thoroughly tested.
I hope someday someone will be safed from debugging hours!
Lg
warappa

Related

NHibernate - Sqlite in memory DB - Can't test the mapping files

I wanted to test whether my entities can be persisted to the database or not, so I came across this article:
http://www.codethinked.com/nhibernate-20-sqlite-and-in-memory-databases
My code to initialize the session factory is the same the one in the article:
public class NHibernateInMemoryTestFixtureBase
{
protected static ISessionFactory sessionFactory;
protected static Configuration configuration;
public static void InitalizeSessionFactory(params Assembly[] assemblies)
{
if (sessionFactory != null)
return;
var properties = new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
properties.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", "Data Source=:memory:;Version=3;New=True;");
properties.Add("connection.release_mode", "on_close");
configuration = new Configuration();
configuration.Properties = properties;
foreach (Assembly assembly in assemblies)
{
configuration = configuration.AddAssembly(assembly);
}
sessionFactory = configuration.BuildSessionFactory();
}
public ISession CreateSession()
{
ISession openSession = sessionFactory.OpenSession();
IDbConnection connection = openSession.Connection;
new SchemaExport(configuration).Execute(false, true, false, true, connection, null);
return openSession;
}
}
And here's my test:
[Test]
public void IWillChangeThisNameLater()
{
InitalizeSessionFactory(typeof(LogRepository).Assembly);
var session = this.CreateSession();
Log log = Log.New("a", "b", "I");
session.Save(log);
session.Flush();
Assert.Greater(log.IDColumn, 0);
}
And the problem is, I removed the "a" property of Log from the log.hbm.xml and session.Save(log) is not throwing an exception or anything, it just works...
This must be obvious and on porpose, but I fail to find out why that is, how can it save it if is not mapped, is that how the in memory database work? how can I test my mapping then?
I mainly did this in-memory test so that I can know right away if a valid entity is failing to persist, of course that would include missing properties on the mapping file.
Any thoughts will be appreciated.
EDIT:
As requested,
the Log entity definition:
public class Log : DomainBase<Log, ILogRepository<Log>>
{
private int logId;
private string tableName;
private string field;
private string userLogin;
protected Log()
{
}
protected Log(string tableName, string field, string userLogin)
{
TableName = tableName;
Field = field;
UserLogin = userLogin;
}
public virtual int LogId { get; set; }
public virtual string TableName { get; set; }
public virtual string Field { get; set; }
public virtual string UserLogin { get; set; }
}
the Log Mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DomainProject" table="Log" lazy="true">
<id name="logId" column="ID" type="int">
<generator class="native" />
</id>
<property name="TableName" column="TableName" type="string" />
<property name="Field" column="Field" type="string" />
<property name="UserLogin" column="UserLogin" type="string" />
</class>
</hibernate-mapping>
If a class contains a property that is not mentioned in the mappings, NHibernate will ignore the property.

NHibernate composite id mapping issue, bi/uni-directional OneToMany, ManyToOne and ManyToMany

In my project all of entities have composite primary keys [systemId as long, deviceId as long, id as long]. Values filled manualy before I save the entity.
I'm using "code first" approach and to provide simplicity references NHibernate.Mapping.Attributes extension to define schema with attributes just like in Java based Hibernate.
All entities have an abstract base type which provides shared properties and functionality:
[Serializable]
public abstract class EntityBase
{
[CompositeId(0, Name = "id", ClassType = typeof(EntityId))]
[KeyProperty(1, Name = "systemId", Column = "restId")]
[KeyProperty(2, Name = "deviceId", Column = "deviceId")]
[KeyProperty(3, Name = "id", Column = "id")]
private EntityId id = new EntityId(); // this is a component, see below
[Property(Column = "isDeleted", NotNull = true)]
private bool deleted = false;
public EntityId Id
{
get { return id; }
set { id = value; }
}
public bool Deleted
{
get { return deleted; }
set { deleted = value; }
}
}
Behind the composite id there is a component, which represent the complex primary key:
[Serializable]
[Component]
public class EntityId
{
[Property(Column = "restId", NotNull = true)]
private long systemId = 0;
[Property(NotNull = true)]
private long deviceId = 0;
[Property(NotNull = true)]
private long id = 0;
public long SystemId
{
get { return systemId; }
set { systemId = value; }
}
public long DeviceId
{
get { return deviceId; }
set { deviceId = value; }
}
public long Id
{
get { return id; }
set { id = value; }
}
}
I definied two entities called OTMList and OTMItem which have bi-directional OneToMany and ManyToOne associations to each other.
[Serializable]
[Class]
public class OTMList : EntityBase
{
[List(0, Cascade = "none", Generic = true, Lazy = CollectionLazy.True)]
[Key(1, Column = "id")]
[Index(2, Column = "id")]
[OneToMany(3, NotFound = NotFoundMode.Exception, ClassType = typeof(OTMItem))]
private IList<OTMItem> otmItems = new List<OTMItem>();
public IList<OTMItem> OTMItems
{
get { return otmItems; }
set { otmItems = value; }
}
}
[Serializable]
[Class]
public class OTMItem : EntityBase
{
[ManyToOne(0, Name = "otmList", ClassType = typeof(OTMList), Column = "OTMListId", Cascade = "none", Lazy = Laziness.Proxy)]
private OTMList otmList = null;
public OTMList OTMList
{
get { return otmList; }
set { otmList = value; }
}
}
The hibernate mapping xml file contains the following information:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-access="field" auto-import="true" assembly="NHibernateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.ListTest.OTM.OTMList, NHibernateTest">
<composite-id class="NHibernateTest.Domain.EntityId, NHibernateTest" name="id">
<key-property name="systemId" column="restId" />
<key-property name="deviceId" column="deviceId" />
<key-property name="id" column="id" />
</composite-id>
<property name="deleted" column="isDeleted" not-null="true" />
<list name="otmItems" lazy="true" cascade="none" generic="true">
<key column="id" />
<index column="id" />
<one-to-many class="NHibernateTest.ListTest.OTM.OTMItem, NHibernateTest" not-found="exception" />
</list>
</class>
<class name="NHibernateTest.ListTest.OTM.OTMItem, NHibernateTest">
<composite-id class="NHibernateTest.Domain.EntityId, NHibernateTest" name="id">
<key-property name="systemId" column="restId" />
<key-property name="deviceId" column="deviceId" />
<key-property name="id" column="id" />
</composite-id>
<property name="deleted" column="isDeleted" not-null="true" />
<many-to-one name="otmList" class="NHibernateTest.ListTest.OTM.OTMList, NHibernateTest" column="OTMListId" cascade="none" lazy="proxy" />
</class>
</hibernate-mapping>
When I validate the schema with SchemaValidator of the NHibernate I get the following exception:
Foreign key (FKF208BF0B9A2FCB3:OTMItem [OTMListId])) must have same number of columns as the referenced primary key (OTMList [restId, deviceId, id])
The problem are the same too when I try to create uni-directional ManyToOne or bi/uni-directional ManyToMany associations.
Somebody can help me please?
The full source code available here:
NHibernateTest source code
The problem is solved, check this out:
http://jzo001.wordpress.com/category/nhibernate/

Nhibernate: One-To-Many mapping problem - Cannot cascade delete without inverse. Set NULL error

I have the current scenario whereby an Article has only 1 Outcome each. Each Article may or may not have an Outcome.
In theory, this is a one-to-one mapping, but since NHibernate does not really support one-to-one, I used a One-To-Many to substitute. My Primary Key on the child table is actually the ArticleID (FK).
So I have the following setup:
Classes
public class Article
{
public virtual Int32 ID { get;set;}
private ICollection<ArticleOutcome> _Outcomes {get;set;}
public virtual ArticleOutcome Outcome
{
get {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
return this._Outcomes.First();
return null;
}
set {
if( value == null ) {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
this._Outcomes.Clear();
}
else {
if( this._Outcomes == null )
this._Outcomes = new HashSet<ArticleOutcome>();
else if ( this._Outcomes.Count >= 1 )
this._Outcomes.Clear();
this._Outcomes.Add( value );
}
}
}
}
public class ArticleOutcome
{
public virtual Int32 ID { get;set; }
public virtual Article ParentArticle { get;set;}
}
Mappings
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasMany<ArticleOutcome>( Reveal.Property<Article>("_Outcomes") )
.AsSet().KeyColumn("ArticleID")
.Cascade.AllDeleteOrphan() //Cascade.All() doesn't work too.
.LazyLoad()
.Fetch.Select();
}
}
public class ArticleOutcomeMap : ClassMap<ArticleOutcome>
{
public ArticleOutcomeMap(){
this.Id( x=> x.ID, "ArticleID").GeneratedBy.Foreign("ParentArticle");
this.HasOne( x=> x.ParentArticle ).Constrained ();
//This do not work also.
//this.References( x=> x.ParentArticle, "ArticleID" ).Not.Nullable();
}
}
Now my problem is this:
It works when I do an insert/update of the Outcome.
e.g.
var article = new Article();
article.Outcome = new ArticleOutcome { xxx = "something" };
session.Save( article );
However, I encounter SQL errors when attempting to delete via the Article itself.
var article = session.Get( 123 );
session.Delete( article ); //throws SQL error.
The error is something to the like of Cannot insert NULL into ArticleID in ArticleOutcome table.
The deletion works if I place Inverse() to the Article's HasMany() mapping, but insertion will fail.
Does anyone have a solution for this? Or do I really have to add a surrogate key to the ArticleOutcome table?
Solution
Here's the mapping for Fluent if anyone is interested.
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasOne( x=> x.Outcome ).Cascade.All();
}
}
public class Article
{
//... other properties
public virtual ArticleOutcome Outcome { get;set;}
}
NHibernate does support one-to-one.
I have a case very similar to yours. Here are the relevant parts:
class Articule
{
public virtual ArticleOutcome Outcome { get; set; }
}
class ArticuleOutcome
{
public virtual Article ParentArticle { get; set; }
}
Mapping (sorry, I don't use Fluent, but it shouldn't be hard to translate):
<class name="Article">
<id name="ID">
<generator class="identity"/>
</id>
<one-to-one name="Outcome" cascade="all"/>
</class/>
<class name="ArticleOutcome">
<id name="ID" column="ArticleId">
<generator class="foreign">
<param name="property">ParentArticle</param>
</generator>
</id>
<one-to-one name="ParentArticle" constrained="true"/>
</class/>
Usage:
var article = new Article();
article.Outcome = new ArticleOutcome
{
xxx = "something",
ParentArticle = article
};
session.Save(article);
Delete should work as you are using it now.

Automapping conventions for when Property and backing field have NOTHING in common?

Using Fluent NHibernate, I cannot seem to devise the necessary automapping conventions for the following (seemingly simple and common) use-case:
public class MyClass
{
private int _specialIdentityField
private string _firstname;
public Id { get { return _specialIdentityField; }; }
public virtual string Firstname
{
get
{
return _firstname;
}
set
{
_firstname = value;
}
}
}
public class OtherClass
{
private int _specialIdentityField
private string _lastname;
public Id { get { return _specialIdentityField; }; }
public virtual string Lastname
{
get
{
return _lastname;
}
set
{
_lastname = value;
}
}
}
The desired mappings are like so:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase-underscore" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyClass" table="`MyClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Firstname" type="System.String">
<column name="Firstname" />
</property>
</class>
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="OtherClass" table="`OtherClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Lastname" type="System.String">
<column name="Lastname" />
</property>
</class>
</hibernate-mapping>
Basically the rules are:
everything is field-camelcase-underscore as an access type EXCEPT identity
identity is a fixed-name field in every class (name=_someSpecialIdentityField)
identity access is always field-only and bears no relation to the name of the RO property that surrounds it
The part of this that is completely tripping me up is the convention-mapping of the identity-related elements (clearly the convention-mapping of the properties is completely standard fare). The issue I am having is how to tell FNH conventions that my identity field is a fixed-name. All of the convention overrides that I can find seem to assume there will always be some relationship between the property that represents identity and the name of its underlying backing field (e.g. I can set a 'custom prefix' for the backing field, but cannot seem to see how I can just say "this is always the name of the backing field").
Its obvious to me how to accomplish this with explicit mapping (and for that matter, with XML mapping files) but not obvious at all to me how to accomplish this with convention-based (Automapping) mapping in FNH.
This can't be an atypical use-case so I must just be overlooking something terribly obvious. Thoughts from any FNH gurus appreciated!
EDIT: Take a look at the IAutomappingConfiguration interface. Create you own implementation or override the DefaultAutomappingConfiguration class.
public virtual bool IsId(Member member)
{
return member.Name.Equals("id", StringComparison.InvariantCultureIgnoreCase);
}
then you add it to the initialization:
Fluently.Configure( Configuration )
.Mappings( cfg =>
{ cfg.AutoMappings.Add( IAutomappingConfigurationInstance )}
===============================================
Hi Steve, I think I know what you are trying to do. I use Proteus and FNH automapping. To do the trick with the id I created a wrapper around Proteus which do two things:
1) Maps the ID
2) Hides the Setter for the id
public abstract class EntityObject<TEntity> : IdentityPersistenceBase<TEntity, Guid>, IEntity
where TEntity : class, IEntity
{
public virtual Guid Id
{
get { return _persistenceId; }
}
Guid IIdentifiedEntity<Guid>.Id
{
get { return _persistenceId; }
set { _persistenceId = value; }
}
public virtual int Version
{
get { return _persistenceVersion; }
set { _persistenceVersion = value; }
}
}
And to avoid the property IsTransient to be persisted + other stuff you can create MappingAlternation:
public class EntityAlteration : IAutoMappingAlteration
{
public void Alter( AutoPersistenceModel model )
{
model.OverrideAll( map =>
{
Type recordType = map.GetType().GetGenericArguments().Single();
if( recordType.BaseType.Name == "EntityObject`1" )
{
Type changeType = typeof( Change<> ).MakeGenericType( recordType );
var change = ( IChange )Activator.CreateInstance( changeType );
change.Go( map );
}
} );
}
}
interface IChange
{
void Go( object mapObject );
}
class Change<TRecord> : IChange where TRecord : EntityObject<TRecord>
{
void IChange.Go( object mapObject )
{
var map = ( AutoMapping<TRecord> )mapObject;
map.Id( x => x.Id ).GeneratedBy.Guid().Access.Property();
map.IgnoreProperty( x => x.IsTransient );
}
}
PS: I am really missing the times when you were active in the online space. It was exciting Summer and Authumn 2 years ago...

NHibernate: Many to Many relationship not working

I have the following database schema:
http://lh4.ggpht.com/_SDci0Pf3tzU/SdM3XnAmmxI/AAAAAAAAEps/Ie3xW3ZVNfQ/s400/styleerror.png
And this is my mapping file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="CodeSmithSampel.Generated.BusinessObjects" assembly="CodeSmithSampel">
<class name="CodeSmithSampel.Generated.BusinessObjects.Store, CodeSmithSampel" table="store" lazy="true">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<bag name="Employees" lazy="true" cascade="all-delete-orphan" inverse="true" >
<key column="Store_id"></key>
<one-to-many class="Employee"></one-to-many>
</bag>
<bag name="Products" table="storeproduct" lazy="true" cascade="all" inverse="true" >
<key column="Store_id"></key>
<many-to-many column="Product_id" class="Product" />
</bag>
</class>
</hibernate-mapping>
And ths is my Store entity class:
public partial class Store : BusinessBase<int>
{
#region Declarations
private string _name = String.Empty;
private IList<Employee> _employees = new List<Employee>();
private IList<Product> _products = new List<Product>();
#endregion
#region Constructors
public Store() { }
#endregion
#region Methods
public override int GetHashCode()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(this.GetType().FullName);
sb.Append(_name);
return sb.ToString().GetHashCode();
}
#endregion
#region Properties
public virtual string Name
{
get { return _name; }
set
{
OnNameChanging();
_name = value;
OnNameChanged();
}
}
partial void OnNameChanging();
partial void OnNameChanged();
public virtual IList<Employee> Employees
{
get { return _employees; }
set
{
OnEmployeesChanging();
_employees = value;
OnEmployeesChanged();
}
}
partial void OnEmployeesChanging();
partial void OnEmployeesChanged();
public virtual IList<Product> Products
{
get { return _products; }
set
{
OnProductsChanging();
_products = value;
OnProductsChanged();
}
}
partial void OnProductsChanging();
partial void OnProductsChanged();
#endregion
}
The product class:
public partial class Product : BusinessBase<int>
{
#region Declarations
private float _price = default(Single);
private string _name = null;
private IList<Store> _stores = new List<Store>();
#endregion
#region Constructors
public Product() { }
#endregion
#region Methods
public override int GetHashCode()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(this.GetType().FullName);
sb.Append(_price);
sb.Append(_name);
return sb.ToString().GetHashCode();
}
#endregion
#region Properties
public virtual float Price
{
get { return _price; }
set
{
OnPriceChanging();
_price = value;
OnPriceChanged();
}
}
partial void OnPriceChanging();
partial void OnPriceChanged();
public virtual string Name
{
get { return _name; }
set
{
OnNameChanging();
_name = value;
OnNameChanged();
}
}
partial void OnNameChanging();
partial void OnNameChanged();
public virtual IList<Store> Stores
{
get { return _stores; }
set
{
OnStoresChanging();
_stores = value;
OnStoresChanged();
}
}
partial void OnStoresChanging();
partial void OnStoresChanged();
#endregion
}
The mapping for the Product class:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="CodeSmithSampel.Generated.BusinessObjects" assembly="CodeSmithSampel">
<class name="CodeSmithSampel.Generated.BusinessObjects.Product, CodeSmithSampel" table="product" lazy="true">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="Price" column="Price" />
<property name="Name" column="Name" />
<bag name="Stores" table="storeproduct" lazy="true" cascade="all" inverse="true" >
<key column="Product_id"></key>
<many-to-many column="Store_id" class="Store" />
</bag>
</class>
</hibernate-mapping>
What is particularly weird is that when I add a Store object to one of the product, the database record is not updated; the add doesn't seem to take place, although the new store object exists in the database:
IManagerFactory managerFactory = new ManagerFactory();
var productManager = managerFactory.GetProductManager();
var myProduct= productManager.GetById(2);
var myStore = new Store();
myStore.Name = "new Store"; //a "new store" entry is created in the Store table
myProduct.Stores.Add(myStore); // but this "new store" is not linked to the myproduct, as it should.
productManager.Session.CommitChanges();
Is there anything I miss?
Note: I generate the above code using CodeSmith.
Edit: The accepted answer works. The reason I got in this problem is because
Only one entity class should have inverse = true, not two. So either Product or Store should set the inverse to false. The code generation tool didn't handle this properly.
The correct way to Add Many to Many relationship is explained below. You must add two times.
Can this have anything to do with the fact that you have a surrogate key in the storeproducts table ?
What happens if you remove this surrogate key column Id, and put the primary key on the combination of the product_id and store_id columns ?
I believe that, if you want to have a surrogate key on the storeproducts table, you'll have to create yet another entity.
If you want to use the surrogate key, you'll have to use the idbag mapping.
How does your Product class and mapping look like ?
I see that you specify the 'inverse' attribute in your mapping of the Products collection in the Store entity.
If you do this (and thus you have a bi-directional association), then you should add the Store to the Stores collection of the product as well.
Since -from the NH documentation- :
Changes made only to the inverse end
of the association are not persisted.
This means that NHibernate has two
representations in memory for every
bidirectional association, one link
from A to B and another link from B to
A. This is easier to understand if you
think about the .NET object model and
how we create a many-to-many
relationship in C#:
category.Items.Add(item); // The category now "knows" about the relationship
item.Categories.Add(category); // The item now "knows" about the relationship
session.Update(item); // No effect, nothing will be saved!
session.Update(category); // The relationship will be saved
The non-inverse side is used to save
the in-memory representation to the
database. We would get an unneccessary
INSERT/UPDATE and probably even a
foreign key violation if both would
trigger changes! The same is of course
also true for bidirectional
one-to-many associations.
You may map a bidirectional
one-to-many association by mapping a
one-to-many association to the same
table column(s) as a many-to-one
association and declaring the
many-valued end inverse="true".
This means, that only one of the ends should be inverse.
Adding a Product to a store, should be done like this:
public class Store
{
public void AddProduct( Product p )
{
if( _products.Contains (p) == false )
{
_products.Add (p);
p.AddStore(this);
}
}
}
public class Product
{
public void AddStore( Store s )
{
if( _stores.Contains (s) == false )
{
_stores.Add (s);
s.AddProduct(this);
}
}
}
(Very important to check whether the collection already contains the item to be added; otherwise you'll end up in an infinite loop.