java - 字段列表中的 NativeQuery 未知列
问题描述
@Query(value = "SELECT r FROM Reservation r WHERE r.reservationSeance.id=:seanceId " +
"AND r.seanceDate=:seanceDate " +
"order by r.id DESC LIMIT 1", nativeQuery = true)
public Reservation findReservationBySeanceDateAndSeanceId(@Param("seanceId") int seanceId, @Param("seanceDate") java.time.LocalDate seanceDate);
如果没有 nativeQuery 作为参数,我有错误“,”或预期为空,得到了 LIMIT stackoverflow 但是当使用它并设置为 true 时我有错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'r' in 'field list'
编辑:查询中使用的实体
@Entity
@Table(name="reservation")
@Data
@NoArgsConstructor
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
private Integer id;
private java.time.LocalDate reservationDate;
private java.time.LocalDate seanceDate;
private Integer reservationNr;
@ManyToOne(fetch = FetchType.LAZY)
private Seance reservationSeance;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "user_id")
private User userReservation;
@OneToMany(orphanRemoval = true, cascade = CascadeType.MERGE)
private List<Seat> seats = new ArrayList<>();
}
降神会实体
@Entity
@Table(name="seance")
@Data
public class Seance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
private Integer id;
private java.time.LocalTime displayTime;
@ManyToOne
private Film film;
@Column(length=127)
private String kind;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private Hall hall;
@OneToMany(mappedBy = "reservationSeance")
@JsonIgnore
private List<Reservation> reservations = new ArrayList<>();
}
前几天我在@JoinColumn 买了一些日子我删除了这个注释
解决方案
@Query(value = "SELECT * FROM Reservation r WHERE **r.reservationSeanceId**=:seanceId " +
"AND r.seanceDate=:seanceDate " +
"order by r.id DESC LIMIT 1", nativeQuery = true)
public Reservation findReservationBySeanceDateAndSeanceId(@Param("seanceId") int seanceId, @Param("seanceDate") java.time.LocalDate seanceDate);
我认为这应该类似于上面的内容。问题正在发生,因为您尝试使用别名r然后字段名reservationSeance,然后还有字段名id。这是不正确的。
请提供您的表格以获取有关您的问题的更多信息。我可以假设,r.reservationSeance.id是 fk,在这种情况下,您应该使用完整的 fk 名称,该名称在您的@JoinColumn name
注释中指出。
更新。
请看一下Reservation 或Seance 课程,会有关系注释。@OneToMany
例如,您使用的位置应该是@JoinColumn name = 'your_fk_name'
然后,请粘贴此 fk_name 以进行查询,而不是 r.reservationSeance.id。
推荐阅读
- angular - Angular e2e 测试 Chromedriver:失败:元素点击被拦截:元素在点(512、828)不可点击
- c# - 将实体框架与 Sql Server Compact Edition 一起使用,无需添加 App.Config
- android - 如何在android中将一个活动调用的凌空POST请求的响应发送到另一个活动
- javascript - 语法错误 javascript php
- c++ - 实现自己的排序算法而不是 QSortFilterProxyModel
- powershell - 在 Powershell 中将多行字符串作为脚本参数移交
- xml - 具有特定属性的节点的求和函数
- jquery - Angular DataTable 响应式扩展不适用于服务器端数据表
- uml - 允许加入节点的多个事件操作
- python - 如果列表的元素指向相同的内存地址,那么两个列表如何不指向同一个对象?