Context:
I’m working on a Spring Data JPA application with table-per-class inheritance. I’m encountering an error when trying to write a JPQL query to retrieve booking orders for a specific room and filter them based on their status.
Code Snippets:
Order.java (Super Class):
Java
package com.TravelConcierge.Hotel.Entity.order;
import com.TravelConcierge.Hotel.Enomiration.OrderStatus;
import com.TravelConcierge.Hotel.Entity.Room;
import jakarta.persistence.*;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "orders")
public abstract class Order {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@Column(nullable = false)
private String orderNumber;
@Column(nullable = false)
private LocalDateTime createdAt;
@ManyToOne(fetch = FetchType.EAGER) // This might need adjustment
@JoinColumn(name = "room_id")
private Room orderinghoom; // Typo: should be orderingRoom
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private OrderStatus status;
@Column(nullable = false)
private String confirmationCode;
}
Use code with caution.
content_copy
BookingOrder.java (Sub Class):
Java
package com.TravelConcierge.Hotel.Entity.order;
import com.TravelConcierge.Hotel.Entity.Room;
import jakarta.persistence.*;
import lombok.*;
import java.time.LocalDate;
@EqualsAndHashCode(callSuper = true)
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class BookingOrder extends Order {
@Column(nullable = false)
private LocalDate checkInDate;
@Column(nullable = false)
private LocalDate checkOutDate;
@OneToOne
@JoinColumn(name="resarvedroom_id") // Typo: should be reservedRoomId
private Room room;
}
Use code with caution.
content_copy
BookingRepository.java:
Java
package com.TravelConcierge.Hotel.Repository;
import com.TravelConcierge.Hotel.Entity.order.BookingOrder;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface BookingRepository extends JpaRepository<BookingOrder,Long> {
@Query("SELECT c FROM BookingOrder c " +
"WHERE c.orderingRoom.id IN (SELECT r.id FROM Room r WHERE r.id = :roomId) " +
"AND (c.status = 'ON-WAY' OR c.status = 'SETTED')")
List<BookingOrder> findByRoomIdAndStatus(@Param("roomId") Long roomId);
}
Use code with caution.
content_copy
Error Message:
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.TravelConcierge.Hotel.Repository.BookingRepository.findByOrderinghoomIdAndStatus(java.util.List);
…
Caused by: org.hibernate.query.sqm.UnknownPathException: Could not resolve attribute ‘orderingRoom’ of ‘com.TravelConcierge.Hotel.Entity.order.BookingOrder’
Question:
I’ve fixed some typos in the code (orderinghoom to orderingRoom, resarvedroom_id to reservedRoomId), but I’m still getting the error. Why can’t Hibernate resolve the orderingRoom attribute in the JPQL query? How can I fix this error to retrieve booking orders for a specific room and filter them by status?
Additional Information:
I’m using Spring Data JPA with table-per-class inheritance.
The Order class is annotated with @Entity.
The @ManyToOne annotation is used on the orderingRoom field in the Order class (might need adjustment based on inheritance strategy).
Desired Outcome:
I want to be able to use the findByRoomIdAndStatus method in my repository to retrieve booking orders for a specific room and filter them based
Desta Equar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.