These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: newbie question about nhibernate collections
PostPosted: Thu Sep 24, 2009 7:48 pm 
Newbie

Joined: Fri Sep 28, 2007 3:45 pm
Posts: 9
Hi all,

I am not sure how this came up now after a few months of using nhibernate but i have the following problem. I have a Company object and an address object. Each company can have multiple addresses, each address is associated with a single company. My mappings are:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.Domain.Company, Core" table="companies">
     
      <id name="ID" column="id" type="Int32" unsaved-value="0">
          <generator class="native"/>
      </id>

      <property name="Name" column="name" type="String" not-null="true" length="255" />
     
      <many-to-one name="Parent" class="Core.Domain.Company, Core" column="parent_id" cascade="all" />

      <bag name="Children" lazy="true" cascade="all-delete-orphan" >
          <key column="parent_id" />
          <one-to-many class="Core.Domain.Company, Core" />
      </bag>

[b]      <bag name="Addresses" table="addresses" lazy="true" cascade="all">
          <key column="company_id"/>
          <many-to-many column="id" class="Core.Domain.Address, Core"/>
      </bag>[/b]

  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.Domain.Address, Core" table="addresses">
   
    <id name="ID" column="id" type="Int32" unsaved-value="0">
        <generator class="native"/>
    </id>

    <property name="Name" column="name" type="String" not-null="true" length="255" />
    <property name="Line1" column="line1" type="String" not-null="true" length="255" />
    <property name="Line2" column="line2" type="String" length="255" />
    <property name="Line3" column="line3" type="String" length="255" />

    <property name="City" column="city" type="String" not-null="true" length="255" />
    <property name="Area" column="area" type="String" length="255" />
    <property name="Postcode" column="postcode" type="String" length="255" />

    <many-to-one name="Company" fetch="select" cascade="all">
      <column name="company_id" not-null="true" />
    </many-to-one>
     
  </class>
</hibernate-mapping>


Note that this is a desktop app so there is a single session that remains open and every db write is wrapped in a transaction.
Each company is being saved by doing the following:

Code:
Session.SaveOrUpdate(company);


I get the following output. The last line is completely wrong and i dont understand why its doing that. Any ideas?

Code:
NHibernate: INSERT INTO companies (name, parent_id) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'tetst', @p1 = NULL
NHibernate.SQL: 2009-09-25 02:40:08,953 [10] DEBUG NHibernate.SQL [(null)] - INSERT INTO companies (name, parent_id) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'tetst', @p1 = NULL
NHibernate: INSERT INTO addresses (name, line1, line2, line3, city, area, postcode, company_id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7); select SCOPE_IDENTITY();@p0 = 'testt', @p1 = 'tetetwet', @p2 = '', @p3 = '', @p4 = 'werwerwer', @p5 = 'fsas', @p6 = '12112', @p7 = 4
[b]NHibernate: INSERT INTO addresses (company_id, id) VALUES (@p0, @p1);@p0 = 4, @p1 = 4[/b]
NHibernate.SQL: 2009-09-25 02:40:08,968 [10] DEBUG NHibernate.SQL [(null)] - INSERT INTO addresses (name, line1, line2, line3, city, area, postcode, company_id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7); select SCOPE_IDENTITY();@p0 = 'testt', @p1 = 'tetetwet', @p2 = '', @p3 = '', @p4 = 'werwerwer', @p5 = 'fsas', @p6 = '12112', @p7 = 4
NHibernate.SQL: 2009-09-25 02:40:09,000 [10] DEBUG NHibernate.SQL [(null)] - INSERT INTO addresses (company_id, id) VALUES (@p0, @p1);@p0 = 4, @p1 = 4


Top
 Profile  
 
 Post subject: Re: newbie question about nhibernate collections
PostPosted: Fri Sep 25, 2009 2:25 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Addresses is a many-to-many associations:

<bag name="Addresses" table="addresses" lazy="true" cascade="all">
<key column="company_id"/>
<many-to-many column="id" class="Core.Domain.Address, Core"/>
</bag>

If that's correct, you need a different table here that contains the link between company_id and address_id. Hava a look here:

http://nhforge.org/doc/nh/en/index.html#collections-ofvalues

Or you change the mapping to one-to-many and everything should be fine.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.