One to many with 1 class

19 views
Skip to first unread message

Plácido Monteiro Dinelli Bisneto

unread,
Aug 17, 2011, 4:14:00 PM8/17/11
to codec...@googlegroups.com
Hi,

How can I map an one to many relationship using only one class?

Eg.:

One product has many Similar Products (that are another projects)..

Or is there a better way to do it?

Aleksandar Hummel

unread,
Aug 17, 2011, 5:01:13 PM8/17/11
to codec...@googlegroups.com
By simply including it in the model?

class Product
{
public virtual IList<Product> RelatedProducts { get; set; }

}

This works for me, for you no?


2011/8/17 Plácido Monteiro Dinelli Bisneto <cid...@live.com>

Plácido Monteiro Dinelli Bisneto

unread,
Aug 18, 2011, 9:37:20 AM8/18/11
to codec...@googlegroups.com
Well, I tried this but it does not works...
The RelatedProducts table is not generated besides it appears inside the HbmMapping generated by conform.
Take a look:

<class name="Product" table="Products">
    <id name="Id" column="ProductId" type="Int64">
      <generator class="hilo">
        <param name="table">NextHighVaues</param>
        <param name="column">NextHigh</param>
        <param name="max_lo">100</param>
        <param name="where">Entity = 'Product'</param>
      </generator>
    </id>
    <bag name="SimilarProducts" table="SimilarProducts" lazy="true" inverse="true" cascade="save-update, persist" batch-size="10">
      <key column="ProductId" foreign-key="ProductId_SimilarProduct" not-null="true" />
      <one-to-many class="Product" />
    </bag>
</class>

But it does not generates the table.
Any thoughts?
Thanks in advance!

Aleksandar Hummel

unread,
Aug 18, 2011, 12:48:12 PM8/18/11
to codec...@googlegroups.com
You are supposed to instantiate a  NHibernate.Tool.hbm2ddl.ShemaExport object and call its Create method, with export parameter set to true to generate the db. Are you doing that?

2011/8/18 Plácido Monteiro Dinelli Bisneto <cid...@live.com>

Plácido Monteiro Dinelli Bisneto

unread,
Aug 18, 2011, 12:54:56 PM8/18/11
to codec...@googlegroups.com
Yes, indeed I am.

I think that this may be a problem when using conventions to create the naming of the foreign key. I think that confORM is getting lost when doing this because it may be generating two ProductId column in the SimilarProducts table. 
Every time that I use the Product class as many-to-one in other classes it uses the name "ProductId" as foreign key.
I really don't know how to handle this.
I'm trying several approaches but none that satisfies me. 
I changed the mapping to ICollection<Int64> and I'll add manually only the ProductId values in the SimilarProducts table and the NH generates the table correctly.

Do you have any other idea?

Aleksandar Hummel

unread,
Aug 18, 2011, 12:59:24 PM8/18/11
to codec...@googlegroups.com
Any of that doesn't matter, if your mapping is produced, as you showed it is, then it must be a problem with either the db (maybe you don't have rights or something), or maybe connection is misconfigured somehow? Can you show your db config? Are you sure you can talk to your db?

2011/8/18 Plácido Monteiro Dinelli Bisneto <cid...@live.com>
Yes, indeed I am.

Plácido Monteiro Dinelli Bisneto

unread,
Aug 18, 2011, 1:05:20 PM8/18/11
to codec...@googlegroups.com
I have full access to the database. The database is local in my machine and I have admin rights.
I use the SchemaExport and it generates all the others tables based in their mappings.

The database configuration is this:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="ECS.Frigelar">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=WKS16;initial catalog=ECommerce;Integrated Security=SSPI
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>


Aleksandar Hummel

unread,
Aug 18, 2011, 2:43:35 PM8/18/11
to codec...@googlegroups.com
Oh, it's because this relation is a many-to-many!

You should use
orm.ManyToMany<Product, Product>();

then I get something like this in the mapping, and it works for me..

  <class name="Product" table="PRODUCTS">
...
    <list name="RelatedProducts" table="PRODUCTS_PRODUCTS" cascade="all">
      <key column="PRODUCT_ID" />
      <list-index column="RelatedProducts_IDX" />
      <many-to-many class="Product" column="RelatedProducts_PRODUCT_ID" />
    </list>
...
  </class>

2011/8/18 Plácido Monteiro Dinelli Bisneto <cid...@live.com>
I have full access to the database. The database is local in my machine and I have admin rights.

Plácido Monteiro Dinelli Bisneto

unread,
Aug 18, 2011, 4:18:49 PM8/18/11
to codec...@googlegroups.com
I changed the mapping to many to many and it generated the tables correctly..


Thank you! 

Aleksandar Hummel

unread,
Aug 18, 2011, 4:40:51 PM8/18/11
to codec...@googlegroups.com
You're welcome.

2011/8/18 Plácido Monteiro Dinelli Bisneto <cid...@live.com>
I changed the mapping to many to many and it generated the tables correctly..


Thank you! 

Reply all
Reply to author
Forward
0 new messages