首页 > 解决方案 > 字段列表中的 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 买了一些日子我删除了这个注释

标签: javaspring-data

解决方案


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


推荐阅读