java 字段列表中的 JPA 未知列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43282658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:17:03  来源:igfitidea点击:

JPA Unknown Column in Field List

javamysqlspringhibernatejpa

提问by Malena T

I have a relation between Accommodationand Bookingclasses, and also I set a foreign key in booking table.

我在AccommodationBooking类之间有关系,并且我在预订表中设置了一个外键。

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;

Accommodation class:

住宿等级:

@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
}

Booking class:

预订舱位:

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

When I execute a query for listing accommodations, I get unknown column in field list error.

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

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'

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet] 与根本原因 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 'field list' 中的未知列 'bookings7_.bookings'

Even I set the relation and define the foreign key in table, what is the reason that I get this error?

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

回答by XiCoN JFS

Try to define your join-table mapping manually in JPA. Drop your schema and let JPA create your tables:

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

Accommodation class

住宿等级

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

Booking class

预订舱位

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

回答by Hussain Ali

Try changing your column names to lower case in your db and entity class.

尝试在数据库和实体类中将列名更改为小写。

回答by Lekso Pozuaho

I had a situation like that, and I solved it by changing the field's position on the query. Looks like it's a MySQL bug, like (or the same as) the one mentioned on this post:

我遇到过这样的情况,我通过更改字段在查询中的位置来解决它。看起来这是一个 MySQL 错误,就像(或与)这篇文章中提到的错误一样:

https://bugs.mysql.com/bug.php?id=1689

https://bugs.mysql.com/bug.php?id=1689

The description of this MySQL bug mentioned a similar workaround solution: "I found that by swapping that field's position in the table with another field that it worked OK."

这个 MySQL 错误的描述提到了一个类似的解决方法:“我发现通过将该字段在表中的位置与另一个工作正常的字段交换。”