How to solve the hibernation problem: repeating column in mapping for an object?

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 get this error:

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

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

etc.

(and, as Frothow noted, it would be a little strange for Flights if the flights were OneToOne with airports. I must admit that I usually ignored the domain and assumed that the names were pseudo-nonsense to ease the matter :))

+6
source

@OneToOnewrong. This would mean that each airport has only one flight. Use @ManyToOne. And you need to specify a column that refers to the identifier from and to the airport, to@JoinColumn

+1

Flight . , , .

0

@JoinColumn @OneToOne

, lazy .

0

:

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;
  }
  //...
}
0

Source: https://habr.com/ru/post/1776017/


All Articles