如何解决hibernate错误:实体映射中的重复列?

时间:2010-11-20 21:32:08

标签: java hibernate-mapping hibernate3

HI,我有以下型号:

@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")

5 个答案:

答案 0 :(得分:6)

你需要@JoinColumn,而不是@Column。

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

(正如Frotthowe提到的那样,飞往机场的OneToOne似乎有点奇怪。我必须承认通常会忽略该域名,并假设这些名称是一些虚假的废话,以方便提问:))

答案 1 :(得分:1)

@OneToOne错了。这意味着每个机场只有一个航班。使用@ManyToOne。您需要通过@JoinColumn

指定引用from和to机场ID的列

答案 2 :(得分:0)

您的Flight课程没有定义ID。实体拥有ID是正常的,我怀疑这可能与您的问题有关。

答案 3 :(得分:0)

@JoinColumn@OneToOne

一起使用

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

答案 4 :(得分: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;
  }
  //...
}