yep.. I finally sorted this one out.

I'll post the code for my files for a reference as this is a slightly different 
example to the one in the wiki.

I've included the location class which shows how the composite foreign key is 
referenced.

@Entity
  | @Table(name="country")
  | public class Country  implements Serializable {
  | 
  |     java.lang.String countryCode1;
  |     
  |     java.lang.String countryCode2;
  |     
  |     java.lang.String shortName;
  |     
  |     java.lang.String longName;
  |     
  |     java.lang.String countryCode3;
  |     
  |     java.util.List<CountryRegion> countryRegions;
  | 
  |     @Id
  |     @Column(name="country_code_1")
  |     public java.lang.String getCountryCode1() {
  |             return countryCode1;
  |     }
  | 
  |     public void setCountryCode1(java.lang.String countryCode1) {
  |             this.countryCode1 = countryCode1;
  |     }
  | 
  |     @Column(name="country_code_2")
  |     public java.lang.String getCountryCode2() {
  |             return countryCode2;
  |     }
  | 
  |     
  |     public void setCountryCode2(java.lang.String countryCode2) {
  |             this.countryCode2 = countryCode2;
  |     }
  | 
  |     @Column(name="country_code_3")
  |     public java.lang.String getCountryCode3() {
  |             return countryCode3;
  |     }
  | 
  |     public void setCountryCode3(java.lang.String countryCode3) {
  |             this.countryCode3 = countryCode3;
  |     }
  | 
  |     @Column(name="long_name")
  |     public java.lang.String getLongName() {
  |             return longName;
  |     }
  | 
  |     public void setLongName(java.lang.String longName) {
  |             this.longName = longName;
  |     }
  | 
  |     @Column(name="short_name")
  |     public java.lang.String getShortName() {
  |             return shortName;
  |     }
  | 
  |     public void setShortName(java.lang.String shortName) {
  |             this.shortName = shortName;
  |     }
  | 
  | 
  | 
  |     @OneToMany(mappedBy="country")
  |     public java.util.List<CountryRegion> getCountryRegions() {
  |             return countryRegions;
  |     }
  | 
  |     public void setCountryRegions(java.util.List<CountryRegion> 
countryRegions) {
  |             this.countryRegions = countryRegions;
  |     }
  | 
  | 
  | 
  | }
  | 

@Entity
  | @Table(name = "country_region")
  | public class CountryRegion implements Serializable {
  | 
  |     java.lang.String name;
  | 
  |     java.lang.String description;
  | 
  |     CountryRegionPK pk;
  |     
  |     Country country;
  | 
  |     java.util.List<Location> locations;
  |     
  |     java.util.List<Company> companies;
  |     
  |     public CountryRegion() {
  |     }
  | 
  |     // mapping of table columns to PK fields
  |     // note that the length had to be set so that PK size didn't exceed 
MySQLs 1024 byte max
  |     @EmbeddedId
  |     @AttributeOverrides({
  |          @AttributeOverride(name = "regionCode", column = @Column(name = 
"region_code", length=3)),
  |          @AttributeOverride(name = "countryCode1", column = 
@Column(name="country_code_1", length=2))
  |        })
  |     public CountryRegionPK getPk() {
  |             return pk;
  |     }
  | 
  |     public void setPk(CountryRegionPK pk) {
  |             this.pk = pk;
  |     }
  | 
  |     @Column(name = "description")
  |     public java.lang.String getDescription() {
  |             return description;
  |     }
  | 
  |     public void setDescription(java.lang.String description) {
  |             this.description = description;
  |     }
  | 
  |     @Column(name = "name")
  |     public java.lang.String getName() {
  |             return name;
  |     }
  | 
  |     public void setName(java.lang.String name) {
  |             this.name = name;
  |     }
  | 
  |     // must use insertable=false and updateable=false when referencing 
country_code_1 for the second time.
  |     @ManyToOne(fetch=FetchType.EAGER)
  |     @JoinColumn(name="country_code_1", insertable=false, updatable=false)
  |     public Country getCountry() {
  |             return country;
  |     }
  | 
  |     public void setCountry(Country country) {
  |             this.country = country;
  |     }
  |     
  |     @OneToMany(fetch=FetchType.LAZY,mappedBy="countryRegion")
  |     public java.util.List<Company> getCompanies() {
  |             return companies;
  |     }
  | 
  |     public void setCompanies(java.util.List<Company> companies) {
  |             this.companies = companies;
  |     }
  | 
  |     @OneToMany(fetch=FetchType.LAZY,mappedBy="countryRegion")
  |     public java.util.List<Location> getLocations() {
  |             return locations;
  |     }
  | 
  |     public void setLocations(java.util.List<Location> locations) {
  |             this.locations = locations;
  |     }
  | 
  | }


  | @Embeddable
  | public class CountryRegionPK implements java.io.Serializable{
  | 
  | 
  | 
  |     private String countryCode1;
  |    private String regionCode;
  |     
  |    public CountryRegionPK() {
  |             super();
  |             // TODO Auto-generated constructor stub
  |     }
  |     
  |    public CountryRegionPK(String regionCode, String countryCode1)
  |    {
  |       this.regionCode = regionCode;
  |       this.countryCode1 = countryCode1;
  |    }
  | 
  |    public String getCountryCode1()
  |    {
  |       return countryCode1;
  |    }
  | 
  |    public void setCountryCode1(String countryCode1)
  |    {
  |       this.countryCode1 = countryCode1;
  |    }
  | 
  |    public String getRegionCode()
  |    {
  |       return regionCode;
  |    }
  | 
  |    public void setRegionCode(String regionCode)
  |    {
  |       this.regionCode = regionCode;
  |    }
  | 
  |    public int hashCode()
  |    {
  |       return (int) countryCode1.hashCode() + regionCode.hashCode();
  |    }
  | 
  |    public boolean equals(Object obj)
  |    {
  |       if (obj == this) return true;
  |       if (!(obj instanceof CountryRegionPK)) return false;
  |       if (obj == null) return false;
  |       CountryRegionPK pk = (CountryRegionPK) obj;
  |       return pk.countryCode1.equals(countryCode1) && 
pk.regionCode.equals(regionCode);
  |    }
  | }

@Entity
  | @Table(name="location")
  | public class Location implements Serializable{
  | 
  |     
  |     java.lang.Integer locationId;
  | 
  |     java.lang.String city;
  | 
  |     java.lang.String postalCode;
  | 
  |     CountryRegion countryRegion;
  |     
  |     java.util.Date creationDate;
  |     
  |     @Column(name="creation_date",nullable=false,updatable=false)
  |     public java.util.Date getCreationDate() {
  |             return creationDate;
  |     }
  | 
  |     public void setCreationDate(java.util.Date creationDate) {
  |             this.creationDate = creationDate;
  |     }
  |     
  |     @Id(generate = GeneratorType.AUTO)
  |     @Column(name="location_id")    
  |     public java.lang.Integer getLocationId() {
  |             return locationId;
  |     }
  | 
  |     public void setLocationId(java.lang.Integer locationId) {
  |             this.locationId = locationId;
  |     }
  |     
  |     @Column(name="city")  
  |     public java.lang.String getCity() {
  |             return city;
  |     }
  | 
  |     public void setCity(java.lang.String city) {
  |             this.city = city;
  |     }
  | 
  | 
  |     // country_region table is mapped by multiple columns.
  |     
  | @ManyToOne
  |     @JoinColumns ( [EMAIL PROTECTED](nullable=false, name="region_code"), 
  | @JoinColumn(nullable=false, name="country_code_1")})
  |     public CountryRegion getCountryRegion() {
  |             return countryRegion;
  |     }
  | 
  |     public void setCountryRegion(CountryRegion countryRegion) {
  |             this.countryRegion = countryRegion;
  |     }
  | 
  | 
  |     @Column(name="postal_code")  
  |     public java.lang.String getPostalCode() {
  |             return postalCode;
  |     }
  | 
  |     public void setPostalCode(java.lang.String postalCode) {
  |             this.postalCode = postalCode;
  |     }
  | 
  | }

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3915116#3915116

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3915116


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to