【问题标题】:How to solve hibernate error: Repeated column in mapping for entity?如何解决休眠错误:实体映射中的重复列?
【发布时间】:2011-05-13 05:10:54
【问题描述】:

您好,我有以下型号:

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

【问题讨论】:

  • 我已经尝试在每个@OneToOne 下添加@Column(name="airportFrom) 和@Column(name="airportTo"),但出现此错误:“@column(s) not allowed在@onetoone 属性上"

标签: java hibernate-mapping hibernate3


【解决方案1】:

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

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

等

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

【讨论】:

  • 如何在 xml 映射(不是注解)中实现这一点?
【解决方案2】:

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

【讨论】:

  • 我认为这取决于他的业务规则,但@JoinColumn 是问题所在。
【解决方案3】:

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

【讨论】:

  • 类的 ID 它不是问题的一部分,这就是为什么它不在问题中。
【解决方案4】:

我将它用于我的桌子,而不是我拥有城市的机场:

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

【讨论】:

    【解决方案5】:

    将@JoinColumn 与@OneToOne 一起使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多