我有Accommodation
和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:无法从根提示中提取ResultSet com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列&#39; bookings7_.bookings&#39;在&#39;字段列表&#39;
即使我设置关系并在表中定义外键,我得到此错误的原因是什么?
答案 0 :(得分:4)
尝试在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;
答案 1 :(得分:0)
尝试在db和entity类中将列名更改为小写。
答案 2 :(得分:0)
我有这种情况,我通过更改字段在查询上的位置来解决它。看起来它是一个MySQL错误,就像这篇文章中提到的那个(或相同):
https://bugs.mysql.com/bug.php?id=1689
这个MySQL错误的描述提到了一个类似的解决方案:“我发现通过交换该字段在表中的位置与另一个字段工作正常。”