-->
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.  [ 9 posts ] 
Author Message
 Post subject: beginner help with many to many (join table) relationship
PostPosted: Tue Oct 31, 2006 3:05 pm 
Newbie

Joined: Tue Oct 31, 2006 2:34 pm
Posts: 7
I've just started investigating NHibernate in the last few days and have worked out almost all of my issues until now.
I have created a many to many relationship between "accounts" and "contacts" using a joining table.
My code successfully saves both objects to my database without throwing any errors, but it does not make an entry in the join table. So, just to clarify, nhibernate stores the account in tblAccounts, and it stores the contact in tblContacts, but it does not make an entry in the tblContactAccountJoin table so I lose the relationship.
I'm assuming that I have missed something (probably very simple) in my mapping XML but can not figure out what. Any help would be much appreciated.

Hibernate version: 1.0.2

Mapping documents:

Code:
<class name="portal.Account, portal" table="tblAccounts">
        <id name="AccountID" column="accountID" >
            <generator class="identity" />
        </id>
        <property name="AccountName" column="accountName" />
       
        <bag name="Contacts" table="tblContactAccountJoin" cascade="all"  inverse="false" lazy="false">
         <key column="accountID" />
         <many-to-many column="contactID" class="portal.Contact, portal"  />
        </bag>
    </class>
   
    <class name="portal.Contact, portal" table="tblContacts" discriminator-value="?">
      <id name="ContactID" column="contactID" unsaved-value="0">
         <generator class="identity" />
      </id>      
      
      <bag name="Accounts" table="tblContactAccountJoin" cascade="all" inverse="true" lazy="false">
         <key column="contactID" />
         <many-to-many column="accountID" class="portal.Account, portal"  />
      </bag>
      
      <property name="NameFirst" column="nameLast" type="string" length="50" />
      <property name="NameLast" column="nameFirst" type="string" length="50" />
      
      <property name="Title" column="title" type="string" length="50" />




Code between sessionFactory.openSession() and session.close():
Code:
Account newAccount = new Account();
         newAccount.AccountName = accountName.Text;

         Contact myContact = new Contact();
         myContact.NameFirst = "ian";
         myContact.NameLast = "emerick";

         myContact.Accounts.Add(newAccount);

         newAccount.Contacts.Add(myContact);
         session.SaveOrUpdate(newAccount);



Class Definitions:
Quote:
public class Account
{
private int accountID;
private string accountName;
private IList contacts = new ArrayList(); //Many-to-Many Relationship
public Account()
{
}

public int AccountID
{
get { return accountID; }
set { accountID = value; }
}
public string AccountName
{
get { return accountName; }
set { accountName = value; }
}
public IList Contacts
{
get { return this.contacts;}
set { this.contacts = value;}
}

}

public class Contact
{
private int contactID;
private string nameFirst;
private string nameLast;
private string title;
private IList accounts = new ArrayList();

public Contact()
{
}

public int ContactID
{
get { return contactID; }
set { contactID = value; }
}

public string Title
{
get { return title; }
set { title = value; }
}

public string NameFirst
{
get { return nameFirst; }
set { nameFirst = value; }
}

public string NameLast
{
get { return nameLast; }
set { nameLast = value; }
}
public IList Accounts
{
get { return this.accounts; }
set { this.accounts = value; }
}


}




Name and version of the database you are using: MSSQL 2000


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 9:04 am 
Beginner
Beginner

Joined: Fri Oct 20, 2006 8:02 am
Posts: 36
Try setting the inverse to false into the contact bag to accounts.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 12:11 pm 
Newbie

Joined: Tue Oct 31, 2006 2:34 pm
Posts: 7
no dice


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 11:20 am 
Newbie

Joined: Tue Oct 31, 2006 2:34 pm
Posts: 7
i have yet to find a solution to this.
I've seen 2 or 3 other threads with this exact same problem and none of them have solutions. Is this really a problem that nobody can figure out?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 11:32 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Did you call Flush()?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 12:59 pm 
Newbie

Joined: Tue Oct 31, 2006 2:34 pm
Posts: 7
sergey you're my hero.
the flush did the trick.

i am a bit comfused as to why NHibernate would go thru the trouble of executing 2 queries to persist the objects to the database and queue the 3rd to link them. I understand that it would do this for performance reasons but I would think if it is already executing queries it would do the entire batch that it has queued. I imagine there's a rather complex algorithm that it uses to determine what queries are required and which ones it can queue.

Thanks again sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 1:46 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Save normally also queues the SQL, but since you are using identity generators, it has to execute the SQL right away to retrieve the identifier value (Save returns it).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 11:12 am 
Newbie

Joined: Thu Aug 07, 2008 10:38 am
Posts: 1
Hi all,

I have a problem very similar to that of the OP. I'm using jpa/hibernate3 and annotations and in two cases the join tables for OneToMany mappings end up empty. Funny thing is that if I run my test suite a 2nd time the join tables get populated. I'm using merge(), mind that calling flush before the merge makes no difference...

Both cases look more or less like this:
Owning object is an @Embeddable which has a List of objects that are @Entity, I'm trying to use

Code:
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="owner_owned",
       joinColumns=@JoinColumn(name="owner_id"),
       inverseJoinColumns=@JoinColumn(name="owned_id")
    )
private List<Owned> owneds;


The join table
Code:
owner_owned
is not populated, however I get all other data.


I'm stuck, even if it might be hard to point out the exact cause based on my short description maybe someone can explain the possible reasons for jpa/hibernate to behave like this?

thanks
/Oskar


Top
 Profile  
 
 Post subject: Re: beginner help with many to many (join table) relationship
PostPosted: Sun Jun 28, 2009 8:23 am 
Newbie

Joined: Wed May 27, 2009 7:43 pm
Posts: 4
HI there,

I have a very similar problem, only i am using Spring's getHibenateTemplate to call the hibernate methods. how can i call "flush()" when working in this manner?

thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.