JPA 2.0 OneToOne Mapping Reference a Attribute

Is it possible to reference one attribute in a referenced entity in relation to OneToOne

Example:

   @Entity
   public class Country {
    @Id
    private Long countryId;
   @Column(name="code")
   private String countryCode;
  ...
  } 

 @Entity
 public class City {

  @Id
  private Long cityId;

   @OneToOne
   @JoinColumn(name="countryId",referencedColumnName="cityId")
    @Column(name="code")
    private String countryCode;

      

}

With this setting, I am getting @Column (s) error not allowed in @OneToOne property. Is it possible to do it another way in JPA 2.0

thanks sanjay

+3


source to share


1 answer


It's impossible. You can simply add an accessory for use cases like this that delegate to the referenced object:



public class City {
  public String getCountryCode() { 
    return null == country ? null : country.getCountryCode(); 
  }
}

      

0


source







All Articles