Reputation: 12214
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")
Upvotes: 10
Views: 59419
Reputation: 517
Use @JoinColumn
together with @OneToOne
Also note that lazy will not work in this case.
Upvotes: 0
Reputation: 2248
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;
}
//...
}
Upvotes: 0
Reputation: 3662
@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
Upvotes: 1
Reputation: 47954
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 :) )
Upvotes: 11
Reputation: 41127
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.
Upvotes: 0