Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 502
Neuquino Avatar asked Nov 20 '10 21:11

Neuquino


2 Answers

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 :) )

like image 159
Affe Avatar answered Oct 25 '22 07:10

Affe


@OneToOne is wrong. It would mean that each Airport only has one Flight. Use @ManyToOne. And you need to specify the column that references the from and to Airport id by @JoinColumn

like image 30
FRotthowe Avatar answered Oct 25 '22 07:10

FRotthowe