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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4235107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:51:51  来源:igfitidea点击:

How to solve hibernate error: Repeated column in mapping for entity?

javahibernate-mappinghibernate3

提问by Neuquino

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'm getting this error:

我收到此错误:

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

采纳答案by Affe

It's @JoinColumn you need, not @Column.

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

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

etc

等等

(and as Frotthowe mentioned, it does seem a little bit odd for Flights to be OneToOne with airports. I must confess to usually ignoring the domain and assuming the names are some pseudo nonsense to facilitate the question :) )

(正如 Frotthowe 所提到的,航班与机场是一对一的似乎确实有点奇怪。我必须承认,通常会忽略域并假设名称是一些伪废话来帮助解决问题:))

回答by Don Roby

Your Flightclass has no id defined. It's normal for an entity to have an id, and I suspect this may be related to your problem.

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

回答by FRotthowe

@OneToOneis wrong. It would mean that each Airport only has one Flight. Use @ManyToOne. And you need to specify the column that references the from and to Airport id by @JoinColumn

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

回答by Alexey

Use @JoinColumntogether with @OneToOne

使用@JoinColumn连同@OneToOne

Also note that lazy will not work in this case.

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

回答by radeklos

I am using this for my table instead of Airports I have Cities:

我在我的桌子上使用它而不是我有城市的机场:

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