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:
countiesstate_cd (integer)
district_cd (integer)
county_cd (integer)
description (varchar)
adjacent_countiesstate_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!