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

5

10

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")
Stanwin answered 20/11, 2010 at 21:32 Comment(1)
I already tried to add @Column(name="airportFrom) and @Column(name="airportTo") under each @OneToOne, but I got this error: "@column(s) not allowed on a @onetoone property"Stanwin
Q
11

It's @JoinColumn you need, not @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 :) )

Quadrivial answered 20/11, 2010 at 21:50 Comment(0)
P
1

@OneToOne is 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

Parity answered 20/11, 2010 at 21:56 Comment(1)
I think it depends on his business rule, but the @JoinColumn was the problem.Cathepsin
G
0

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

Goldsworthy answered 20/11, 2010 at 21:49 Comment(1)
The Id of the class it is not part of the problem, that's why it is not in the question.Stanwin
B
0

Use @JoinColumn together with @OneToOne

Also note that lazy will not work in this case.

Brutalize answered 20/11, 2010 at 22:0 Comment(0)
C
0

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;
  }
  //...
}
Cellarer answered 17/2, 2013 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.