Question

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")
Was it helpful?

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

OTHER TIPS

@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

Your Flight class has no id defined. It's normal for an entity to have an id, and I suspect this may be related to your problem.

I am using this for my table instead of Airports I have Cities:

class Tender implements java.io.Serializable {
  //...
  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinColumn(name = "city")
  public City getCity() {
    return this.city;
  }

  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @JoinColumn(name = "tour_city")
  public City getTourCity() {
    return this.tourCity;
  }
  //...
}

City implements java.io.Serializable {
  //...
  @Id
  @SequenceGenerator(name = "city_pkey", sequenceName = "city_uid_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "city_pkey")
  @Column(name = "uid", unique = true, nullable = false)
  public int getUid() {
    return this.uid;
  }
  //...
}

Use @JoinColumn together with @OneToOne

Also note that lazy will not work in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top