-->
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.  [ 5 posts ] 
Author Message
 Post subject: Issues with bidirectional one-to-many relation
PostPosted: Sat Mar 15, 2008 1:13 pm 
Newbie

Joined: Sat Mar 15, 2008 12:09 pm
Posts: 2
Location: Belgium
I'm having a very odd issue which I've been struggling with for many hours. I'm completely out of ideas, so any help is greatly appreciated.

I have two entities, Company and Campaign, where one company has many campaigns and one compaign has one company—a bidirectional one-to-many relationship.

The relevant code looks more or less like this (some parts have been left out for clarity):

Code:
public class Campaign
{
   @ManyToOne
   @JoinColumn(name="company_id")
   public Company getCompany() { return company; }

   public void setCompany(Company company) { this.company = company; }
}

public class Company
{
   @OneToMany(mappedBy="company")
   public Collection<Campaign> getCampaigns() { return campaigns; }

   public void setCampaigns(Collection<Campaign> campaigns) { this.campaigns = campaigns; }
}


When I try to create a company and a campaign, save both, then link them together (using setCompany, for example), I get weird errors caused by violated foreign key constraints:

Quote:
Batch entry 0 insert into campaigns (company_id, name, id) values (NULL, Foo, 2118) was aborted.
ERROR: insert or update on table "campaigns" violates foreign key constraint "fkfd772163fb464619"
Detail: Key (id)=(2118) is not present in table "companies".


If I'm interpreting this correctly, then either Hibernate or Postgres is looking for a Company with the ID of a Campaign, which is causing an error because there is no company with the campaign's ID. I have no idea why this is happening…

There is one foreign key constraint in the "campaigns" table, and this is what it looks like:

Quote:
"fkfd772163c8bfc11b" FOREIGN KEY (company_id) REFERENCES companies(id)


which looks fine.

I have no idea what's going on here. Are my annotations correct? Did I forget an annotation here or there? Any hints that point me in the right direction are welcome. Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Issues with bidirectional one-to-many relation
PostPosted: Sat Mar 15, 2008 2:03 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The weird thing to me in the first place is why this is null. How do you create objects and save them? I would expect something like this:

Code:
Company company = new Company();
company.set..(..);

Campaign campaign = new Campaign();

campaign.set..();

company.getCampaigns().add(campaign);
campaign.setCompany(company);

em.persist(company);
em.persist(campaign);




Farzad-


Top
 Profile  
 
 Post subject: Re: Issues with bidirectional one-to-many relation
PostPosted: Sat Mar 15, 2008 2:10 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
and there is something else. If you look at the foreign key names they are different so I am guessing you might have added a wrong foreign key too. Examine the table for all it's constraints and make sure what you see if what you thing it is.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 15, 2008 9:24 pm 
Newbie

Joined: Sat Mar 15, 2008 3:54 am
Posts: 6
Quoted from book hibernate in action, I would suggest you to create a helper method in Company. say
Code:
public void addCampaign(Campaign campaign){
   if(campaign.getCompany() != null){
      campaign.getCompany().getCampaigns().remove(campaign);
      campaign.setCompany(this);
   }
   this.campaigns.add(campaign);
}

And then you just need to persist company variable and it will propagate persistency to campaign as well.
And set show_sql=true in hibernate config. Grab the showing sql in log and copy paste to run on your database which is normally what I will do to debug.
Hope this help.
BTW, I am also a new learner of hibernate so don't trust me too much ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 10:10 am 
Newbie

Joined: Sat Mar 15, 2008 12:09 pm
Posts: 2
Location: Belgium
Alright, looks like I made a really stupid but really subtle mistake. Company used one database while Campaign used another. This obviously caused lots of very weird errors and exceptions…

So yeah, issue is solved. :)


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