简体   繁体   English

字段列表中的NativeQuery未知列

[英]NativeQuery unknown column in field list

@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);

Without nativeQuery as parameter I have error ',' or nulls expected, got LIMIT stackoverflow but when using it and set true i have error 如果没有nativeQuery作为参数,我会出现错误','或预期为null,但是得到LIMIT stackoverflow,但是在使用它并将其设置为true时我遇到了错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'r' in 'field list'

EDIT: Entity used in query 编辑:查询中使用的实体

@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 会议实体

@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<>();
}

other day i hvae @JoinColumn buy in some days i delete this anotation 前几天我在@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);

I think this should be something like above. 我认为这应该像上面。 The problem is happening, because you're trying use alias name r then field name reservationSeance , and then also field name id . 问题正在发生,因为您尝试使用别名r,然后使用字段名ReservationSeance ,然后再使用字段名id It's not correct. 不对

Please, provide your table to get more information about your issue. 请提供您的表格以获取有关您的问题的更多信息。 I can suppose, that r.reservationSeance.id it's fk, and in this case, you should use full fk name, which was pointed in your @JoinColumn name annotation. 我可以假设r.reservationSeance.id是fk,在这种情况下,您应该使用完整的fk名称,该名称在@JoinColumn name注释中指向。

Updated. 更新。

Please, have a look at Reservation or Seance class, there will be relationship annotation. 请看一下Reservation或Seance类,将有关系注释。 Where you used @OneToMany for example, then should be @JoinColumn name = 'your_fk_name' And then, please, paste this fk_name to query instead of r.reservationSeance.id. 例如,在使用@OneToMany的位置,则应为@JoinColumn name = 'your_fk_name' 。然后,请粘贴此fk_name进行查询,而不是r.reservationSeance.id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM