I'm using Hibernate and Oracle database and just got stuck.
I'm trying to map this table:
CREATE TABLE passengers_on_the_flight
(
flight_id NUMERIC(10) REFERENCES flight(flight_id),
passenger_id NUMERIC(20) REFERENCES passenger(passenger_id),
seat NUMERIC(5) NOT NULL,
CONSTRAINT "not free" PRIMARY KEY (flight_id,passenger_id,seat)
);
So my mapping class looks like:
@Entity
@Table(name = "PASSENGERS_ON_THE_FLIGHT")
@NamedQueries({
@NamedQuery(name = "PassengersOnTheFlight.findAll", query = "SELECT p FROM PassengersOnTheFlight p")})
public class PassengersOnTheFlight implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected PassengersOnTheFlightPK passengersOnTheFlightPK;
public PassengersOnTheFlightPK getPassengersOnTheFlightPK() {
return passengersOnTheFlightPK;
}
public void setPassengersOnTheFlightPK(PassengersOnTheFlightPK passengersOnTheFlightPK) {
this.passengersOnTheFlightPK = passengersOnTheFlightPK;
}
@JoinColumn(name = "SEAT", referencedColumnName = "SEAT", insertable = false, updatable = false)
private int seat;
@JoinColumn(name = "FLIGHT_ID", referencedColumnName = "FLIGHT_ID", insertable = false, updatable = false)
@ManyToOn开发者_StackOverflowe
private Flight flight;
@JoinColumn(name = "PASSENGER_ID",referencedColumnName = "PASSENGER_ID",insertable = false, updatable = false)
@ManyToOne
private Passenger passenger;
//Getters, setters for seat, flight and passanger
And primary key class:
@Embeddable
public class PassengersOnTheFlightPK implements Serializable {
@Column(name = "FLIGHT_ID",nullable=false)
private long flightId;
@Column(name = "SEAT",nullable=false)
private int seat;
@Column(name = "PASSENGER_ID", nullable=false)
private Long passengerId;
//Getters and setters for seat, flightId and passangerId
I tried to persist something and got
ORA-00957: duplicate column name
That because Hibernate generates such query:
insert into PASSENGERS_ON_THE_FLIGHT (seat, FLIGHT_ID, PASSENGER_ID, SEAT) values (?, ?, ?, ?)
I don't get why. Did I mis something in the mapping classes?
I get the same problem with a Coposite Key and solved it adding this params at the @JoinColumn hibernate annotation in the get methods of the external PK duplicate entities:
@JoinColumn(..., updatable=false, insertable=false)
Hibernate really REALLY wants you to have a unique identity key for each table; it has "issues" without it. Try putting an identity key on your table.
You get your error because you specified seat twice. Once you have it as property of your entity PassengersOnTheFlight
and second time in the key. If it is part of your key, remove it from the main object. If it is an integer, you probably don't want @JoinColumn
for it anyway.
精彩评论