gpt4 book ai didi

java - hibernate 和复合键

转载 作者:行者123 更新时间:2023-11-29 03:56:13 26 4
gpt4 key购买 nike

我正在使用 Hibernate 和 Oracle 数据库,但遇到了问题。

我正在尝试映射此表:

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)
);

所以我的映射类看起来像:

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

和主键类:

@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

我努力坚持了一些事,结果得到了

ORA-00957: duplicate column name

那是因为 Hibernate 生成这样的查询:

insert into PASSENGERS_ON_THE_FLIGHT (seat, FLIGHT_ID, PASSENGER_ID, SEAT) values (?, ?, ?, ?)

我不明白为什么。我是否在映射类中遗漏了什么?

最佳答案

我遇到了与 Coposite Key 相同的问题,并解决了它在外部 PK 重复实体的 get 方法中的 @JoinColumn hibernate 注释中添加此参数:

@JoinColumn(..., updatable=false, insertable=false)

关于java - hibernate 和复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6282943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com