-->
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.  [ 1 post ] 
Author Message
 Post subject: Problems with ManyToOne and composite keys - Solved
PostPosted: Mon May 24, 2010 4:27 am 
Newbie

Joined: Sun May 23, 2010 7:45 am
Posts: 1
I managed to solve my problem and I am pasting in the correct annotations below:

Table Petcase
Code:
@ManyToOne
@JoinColumn(name = "country_id", nullable = false, insertable = false, updatable = false)
private Country country;

@ManyToOne
@JoinColumns( { @JoinColumn(name = "country_id", nullable = false, insertable = false, updatable = false),
         @JoinColumn(name = "county_id", nullable = false, insertable = false, updatable = false) })
private County county;


Table County
Code:
@ManyToOne
   @JoinColumn(name = "country_id", nullable = false, insertable = false, updatable = false)
   private Country country;



_______________________________________________________________________________

Hello!

I am relatively new to Hibernate and I have a problem with my setup.

I have three tables that look like this:

Table Country
Code:
...
country_id pk, int
...


Table County
Code:
...
county_id pk, int
country_id fk, int
...


Table Petcase
Code:
...
country_id fk, int
county_id fk, int
...


I have tried to get the Annotations right for this setup without success, here is what I currently have and that is working without runtime errors.

Class County
Code:
...
@EmbeddedId
private CountyCompositeKey countyCompositeKey = new CountyCompositeKey();
...


Class CountyCompositeKey
Code:
...

   @Column(name = "country_id")
   private int countryId;

   @Column(name = "county_id")
   private int countyId;

...


Class Country
Code:
...
@OneToMany(mappedBy = "countyCompositeKey.countryId", targetEntity = County.class, fetch = FetchType.EAGER)
private List<County> counties;
...


Class Petcase
Code:
@Column(name = "country_id")
private int countryId;

@Column(name = "county_id")
private int countyId;


This configuration has some fatal flaws:

In Petcase, I can not get the Country or County objects to work.
In County, I can not get a Country-object.

Could someone help me with setting up the annotations for getting this to work?
Is my DataModel ok, or should it be done in some other way?
Any help is greatly appreciated.


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

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.