javahibernate-mappinghibernate3

How to solve hibernate error: Repeated column in mapping for entity?


HI, I have the following model:

@Entity
class Flight{
  private Airport airportFrom;
  private Airport airportTo;

  @OneToOne(fetch=FetchType.LAZY,optional=false)
  public Airport getAirportFrom(){
    return this.airportFrom;
  }

  @OneToOne(fetch=FetchType.LAZY,optional=false)
  public Airport getAirportTo(){
    return this.airportTo;
  }
}

@Entity
class Airport{
  private Integer airportId;

  @Id
  public Integer getAirportId(){
    this.airportId;
  }
}

And I'm getting this error:

org.hibernate.MappingException: Repeated column in mapping for entity: model.entities.Flight column: airportId (should be mapped with insert="false" update="false")

Solution

  • It's @JoinColumn you need, not @Column.

      @OneToOne(fetch=FetchType.LAZY,optional=false)
      @JoinColumn(name="airportFrom", referencedColumnName="airportId")
      public Airport getAirportFrom(){
        return this.airportFrom;
      }
    

    etc

    (and as Frotthowe mentioned, it does seem a little bit odd for Flights to be OneToOne with airports. I must confess to usually ignoring the domain and assuming the names are some pseudo nonsense to facilitate the question :) )