-->
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: OneToMany JPA self-reference through JoinTable
PostPosted: Fri Jul 08, 2011 9:22 am 
Newbie

Joined: Wed Feb 13, 2008 7:28 pm
Posts: 5
I'm fairly new to JPA and am trying to figure out something that seems pretty basic to me. I feel like I'm probably missing something simple that I should be seeing.

I have two tables: county and adjacent_counties. One table represents the counties and the other is a relationship table that links counties throughout the country that are adjacent (even if they are in a different state).

Here is the data structure:
counties
state_cd (integer)
district_cd (integer)
county_cd (integer)
description (varchar)

adjacent_counties
state_cd (integer)
county_cd (integer)
adjacent_state_cd (integer)
adjacent_county_cd (integer)

Here's what I have for beans:

Code:
public class CountyPK {

     @Column(name = "state_cd")
     private Integer stateCode;
     @Column(name = "district_cd")
     private Integer districtCode;
     @Column(name = "county_cd")
     private Integer countyCode;

     ...

}

public class County {

     @EmbeddedId
     protected CountyPK countyPK;
     private String description;

     @OneToMany()
     @JoinTable(name="adjacent_counties",
        joinColumns = {
          @JoinColumn(name="state_cd", referencedColumnName="state_cd"),
          @JoinColumn(name="county_cd", referencedColumnName="county_cd")
        },
         inverseJoinColumns = {
          @JoinColumn(name="adjacent_state_cd", referencedColumnName="state_cd"),
          @JoinColumn(name="adjacent_county_cd", referencedColumnName="county_cd")
        })
      private List<County> adjacentCounties;

     ...

}


With this setup, I get:

org.hibernate.AnnotationException: referencedColumnNames(state_cd, county_cd) of County.adjacentCounties referencing County not mapped to a single property.

I'm not sure what that is telling me. I wouldn't expect those two columns to be mapped to a single property, they should be mapped individually to two different properties. Is it a problem because it's a self-reference? Or because of the additional property of district_cd in the key?

...or do I just have a silly syntax error in my annotation?

Thanks very much for taking a look!


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.