编程语言
首页 > 编程语言> > java – 如何解决hibernate错误:实体映射中的重复列?

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

作者:互联网

嗨,我有以下型号:

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

解决方法:

这是你需要的@JoinColumn,而不是@Column.

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

等等

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

标签:java,hibernate-mapping,hibernate3
来源: https://codeday.me/bug/20190730/1580750.html