Spring Boot 3.x.x, Left Join with condition stopped working correctly

I have custom query in jpa repository:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    @Query("""
        select 
            user
        from User user
        left join user.role r
        left join r.attributes a on a.type = '2'
        where user.id = :userId
    """)
    @EntityGraph("User.getRoles")
    User getMyUser(@Param("userId") Long userId);
}

Entites:

@Entity(name = "users")
@NamedEntityGraph(name = "User.getRoles", attributeNodes = {
            @NamedAttributeNode(value = "role", subgraph = "User.getRoles.attributes")
        },
        subgraphs = {
                @NamedSubgraph(name = "User.getRoles.attributes",
                        attributeNodes = @NamedAttributeNode(value = "attributes")
                )
        }
)
public class User {
    @Id
    @GeneratedValue
    private Long id;
    @ManyToMany
    @JoinTable(
            name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set<Role> role;
}

@Entity
public class Role {
    @Id
    @GeneratedValue()
    private Long id;
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "attribute_id")
    public Set<Attribute> attributes;
}

@Entity
public class Attribute {
    @Id
    @GeneratedValue
    private Long id;
    @ManyToOne
    @JoinColumn(name = "role_id")
    private Role role;
    private String type;
}

When I use Spring Boot 2.6.4 my Hibernate query is:

select
        user0_.id as id1_3_0_,
        role2_.id as id1_1_1_,
        attributes3_.id as id1_0_2_,
        role1_.user_id as user_id1_2_0__,
        role1_.role_id as role_id2_2_0__,
        attributes3_.role_id as role_id3_0_2_,
        attributes3_.type as type2_0_2_,
        attributes3_.attribute_id as attribut4_0_1__,
        attributes3_.id as id1_0_1__ 
    from
        users user0_ 
    left outer join
        user_roles role1_ 
            on user0_.id=role1_.user_id 
    left outer join
        roles role2_ 
            on role1_.role_id=role2_.id 
    left outer join
        attribute attributes3_ 
            on role2_.id=attributes3_.attribute_id 
            and (
                attributes3_.type='2'
            ) 
    where
        user0_.id=?

When I use Spring Boot 3.0.0 Hibernate query is:

Hibernate: 
    select
        u1_0.id,
        r2_0.user_id,
        r2_1.id,
        a2_0.attribute_id,
        a2_0.id,
        a2_0.role_id,
        a2_0.type 
    from
        users u1_0 
    left join
        (user_roles r1_0 
    join
        roles r1_1 
            on r1_1.id=r1_0.role_id) 
                on u1_0.id=r1_0.user_id 
        left join
            attribute a1_0 
                on r1_1.id=a1_0.attribute_id 
                and a1_0.type='2' 
        left join
            (user_roles r2_0 
        join
            roles r2_1 
                on r2_1.id=r2_0.role_id) 
                    on u1_0.id=r2_0.user_id 
            left join
                attribute a2_0 
                    on r2_1.id=a2_0.attribute_id 
            where
                u1_0.id=?

But the real problem is the different results:

    public User method() {
        var myUser = userRepository.getMyUser(1L);
        System.out.println(myUser);
        return myUser;
    }

Spring Boot 2.6.4:

User(id=1, role=[Role(id=1, attributes=[Attribute(id=2, type=1)])])

Spring Boot 3.0.0:

User(id=1, role=[Role(id=1, attributes=[Attribute(id=2, type=1), Attribute(id=1, type=2)])])

I also cannot move the condition from ON to WHERE, because left join will work as Inner join.
That’s not working right:

        select 
            user
        from User user
        left join user.role r
        left join r.attributes a 
        where user.id = :userId and (a.type = '2' and a.type is null)

I want to use springboot 3.2.0 and have the query work correctly

New contributor

Denis Bezrukov 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