gpt4 book ai didi

java - 字段列表中的 JPA 未知列

转载 作者:行者123 更新时间:2023-11-29 01:36:12 25 4
gpt4 key购买 nike

我在 AccommodationBooking 类之间建立了关系,并且我在 booking 表中设置了一个外键。

ALTER TABLE `project`.`booking` 
ADD INDEX `fk_accId_fk_idx` (`accommodation` ASC);
ALTER TABLE `project`.`booking`
ADD CONSTRAINT `fk_accId_fk`
FOREIGN KEY (`accommodation`)
REFERENCES `project`.`accommodation` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

住宿等级:

@Entity
....
public class Accommodation implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private BigInteger id;

@OneToMany(mappedBy = "accommodation", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference
private List < Booking > bookings;
......getters setters
}

预订舱位:

@Entity
public class Booking implements Serializable {
......
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "bookings", nullable = true)
@JsonBackReference
private Accommodation accommodation;
....getters setters
}

当我执行住宿列表查询时,我在字段列表中收到未知列错误。

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bookings7_.bookings' in 'field list'

即使我在表中设置关系并定义外键,我得到这个错误的原因是什么?

最佳答案

尝试在 JPA 中手动定义连接表映射。删除您的架构并让 JPA 创建您的表:

住宿等级

@OneToMany(mappedBy = "accommodation", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference
private List < Booking > bookings;

预订舱位

@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "accommodation_booking_join_table",
joinColumns = {@JoinColumn(name="booking_id")},
inverseJoinColumns = @JoinColumn(name = "accommodation_id"))
@JsonBackReference
private Accommodation accommodation;

关于java - 字段列表中的 JPA 未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43282658/

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