10

嗨,我有以下型号:

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

我收到了这个错误:

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

5 回答 5

11

您需要的是@JoinColumn,而不是@Column。

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

ETC

(正如 Frotthowe 所提到的,Flights 与机场的 OneToOne 似乎有点奇怪。我必须承认通常忽略域并假设名称是一些伪废话以促进问题:))

于 2010-11-20T21:50:49.030 回答
1

@OneToOne是错的。这意味着每个机场只有一个航班。使用@ManyToOne. 您需要指定引用 from 和 to Airport id 的列@JoinColumn

于 2010-11-20T21:56:28.793 回答
0

您的Flight班级没有定义 id。实体有 id 是正常的,我怀疑这可能与您的问题有关。

于 2010-11-20T21:49:17.600 回答
0

@JoinColumn一起使用@OneToOne

另请注意,惰性在这种情况下不起作用。

于 2010-11-20T22:00:41.427 回答
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;
  }
  //...
}
于 2013-02-17T18:33:28.563 回答