Java – how to resolve hibernate errors: duplicate columns in entity mappings?

Hi, I have the following models:

@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;
  }
}

I received 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

This is the @ joincolumn you need, not @ column

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

wait

(as frotthowe mentioned, onetoone flying to the airport seems a little strange. I must admit that this field is usually ignored and assume that these names are some false nonsense for convenience:)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>