QueryDSL join(a, b) vs join(b).on(): Why does @SQLRestriction behave differently?

Intro:

I’m using QueryDSL with Hibernate in a Spring Boot project. I’ve noticed a difference in the way @SQLRestriction is applied when using join(a, b) versus join(b).on(). Specifically, when using join(a, b), the @SQLRestriction on both entities a and b is applied, but when using join(b).on(), only the @SQLRestriction on entity a is applied.

Example Entities:

@Entity
@SQLRestriction("deleteTime is null")
data class Order(
    @Id
    val id: Long,

    @ManyToOne
    @JoinColumn(name = "customer_id")
    val customer: Customer,

    val deleteTime: LocalDateTime?
)

@Entity
@SQLRestriction("deleteTime is null")
data class Customer(
    @Id
    val id: Long,

    val name: String,
    val deleteTime: LocalDateTime?
)

QueryDSL Example:

Using join(a, b):

val query = JPAQuery<Order>(entityManager)
val order = QOrder.order
val customer = QCustomer.customer

query.from(order)
     .join(order.customer, customer)
     .where(customer.name.eq("John Doe"))
     .fetch()

Resulting Native Query:

SELECT o.*
FROM orders o
JOIN customers c ON o.customer_id = c.id AND c.delete_time IS NULL
WHERE o.delete_time IS NULL AND c.name = 'John Doe';

Using join(b).on():

val query = JPAQuery<Order>(entityManager)
val order = QOrder.order
val customer = QCustomer.customer

query.from(order)
     .join(customer).on(order.customer.id.eq(customer.id))
     .where(customer.name.eq("John Doe"))
     .fetch()

Resulting Native Query:

SELECT o.*
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.delete_time IS NULL AND c.name = 'John Doe';

Note: The resulting native queries are not from actual QueryDSL execution but are written to illustrate the behavior I observed.

Issue:

When using join(a, b), the @SQLRestriction on both Order and Customer entities is applied correctly. However, when using join(b).on(), the @SQLRestriction on the Customer entity is not applied automatically. Interestingly, the @SQLRestriction on the Order entity is still applied correctly in the from clause.

Question:

Why does @SQLRestriction behave differently when using join(b).on() compared to join(a, b)? Is there a way to ensure that @SQLRestriction is applied to both entities when using join(b).on()?

Any insights or documentation references that explain this behavior would be greatly appreciated.

•   In a real service operation, when joining two tables a and b, I noticed that the @SQLRestriction was not applied in the query log.
•   Initially, there was no relationship between the two tables.
•   I observed that the code working correctly in another project had the common point of having relationships between tables.
•   I established the relationship and used join(b).on(), but still only the @SQLRestriction on entity a was applied.
•   When using join(a, b), both restrictions were applied as expected.

New contributor

Goose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật