Search code examples
javahibernatejpaspring-data-jpa

TypedQuery issue while creating query using two tables


I am getting SemanticException while trying to create the TypedQuery like below

String findEmptyTableQuery = "select * from dining_table dt " +
             "where "+
             "dt.location = ?1 " +
             "and dt.id not in ( select dining_table_id " +
             "from reserved_slot )";
TypedQuery<DiningTable> findEmptyTable = entityManager.createQuery(
            findEmptyTableQuery, DiningTable.class);

While the resulting query runs fine in H2 console, I am unable to figure out what might be the issue here. I guess I am not specifying the table/column name correctly in the query string. Can anyone please shed some light ?

The exception

org.hibernate.query.SemanticException: A query exception occurred [select * from dining_table dt where dt.location = ?1 and dt.id not in ( select dining_table_id from reserved_slot )]

Any help is appreciated.

Edit: The mapping

@OneToMany(
            mappedBy = "diningTable",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    @JsonIgnoreProperties("reservedSlots")
    private List<ReservedSlot> reservedSlots = new ArrayList<>();

Edit 2: Adding Entities

@Entity
public class DiningTable {

        @Id
        @Column(name = "ID")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Convert(converter= TablePositionConverter.class)
        @Column(name="POSITION")
        private TablePosition tablePosition;

        @Max(6)
        private int maxCapacity;

        @Convert(converter= TableLocationConverter.class)
        @Column(name="LOCATION")
        private TableLocation tableLocation;

        @OneToMany(mappedBy = "diningTable", cascade = CascadeType.ALL,
                orphanRemoval = true
        )
        @JsonIgnoreProperties("reservations")
        private List<Reservation> reservations = new ArrayList<>();

        @OneToMany(
                mappedBy = "diningTable",
                cascade = CascadeType.ALL,
                orphanRemoval = true
        )
        @JsonIgnoreProperties("reservedSlots")
        private List<ReservedSlot> reservedSlots = new ArrayList<>();

        @ManyToOne(fetch = FetchType.EAGER)
        private Restaurant restaurant;

@Entity
public class ReservedSlot {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private int reservedChair;

    private int availableChair;

    private LocalDate reservationDate;

    private LocalDateTime arrivalTime;

    private LocalDateTime departureTime;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JsonIgnoreProperties("diningTable")
    private DiningTable diningTable;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reservation_id")
    private Reservation reservation;

Solution

  •     String findEmptyTableQuery = "SELECT dt FROM DiningTable dt " +
                "LEFT JOIN dt.reservedSlots rs " +
                "WHERE dt.tableLocation = :location " +
                "AND rs.id IS NULL";
    
        TypedQuery<DiningTable> findEmptyTable = entityManager.
                createQuery(findEmptyTableQuery, DiningTable.class);
        //don't forget to set parameter
        findEmptyTable.setParameter("location", location);