@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<>();
}
Seance实体
@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购买,有时会删除此注释
答案 0 :(得分:1)
@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。