How to customize fetching? Spring Data JPA, ManyToMany relation with a new entity

I am learning Spring Data JPA and I am trying to build ManyToMany relation between class Account and class Authority (role). I also want to track the history of when the authority (role) has been revoked. Database will contain attributes like: assigned_at, revoked_at.

What are the possible ways to customize fetching of auhorities when the account is fetched?

Currently the Hibernate generates this two SQL queries:

Hibernate: 
    select
        a1_0.id,
        a1_0.created_at,
        a1_0.email,
        a1_0.first_name,
        a1_0.hash_identifier,
        a1_0.last_name,
        a1_0.locked,
        a1_0.password,
        a1_0.username,
        a1_0.verified 
    from
        accounts a1_0
Hibernate: 
    select
        aa1_0.account_id,
        aa1_0.id,
        aa1_0.assigned_at,
        a2_0.id,
        a2_0.authority_name,
        aa1_0.revoked_at 
    from
        accounts_authorities aa1_0 
    left join
        authorities a2_0 
            on a2_0.id=aa1_0.authority_id 
    where
        aa1_0.account_id=?

How to customize the fetching of authorities, so that only active authorities are fetched. Something like adding revoked_at is null in WHERE clause:

Hibernate: 
    select
        a1_0.id,
        a1_0.created_at,
        a1_0.email,
        a1_0.first_name,
        a1_0.hash_identifier,
        a1_0.last_name,
        a1_0.locked,
        a1_0.password,
        a1_0.username,
        a1_0.verified 
    from
        accounts a1_0
Hibernate: 
    select
        aa1_0.account_id,
        aa1_0.id,
        aa1_0.assigned_at,
        a2_0.id,
        a2_0.authority_name,
        aa1_0.revoked_at 
    from
        accounts_authorities aa1_0 
    left join
        authorities a2_0 
            on a2_0.id=aa1_0.authority_id 
    where
        aa1_0.account_id=?
    and
        aa1_0.revoked_at is null

I would like to use JPA Specifications and Criteria Builder if that is what I should do. Or is there a way to customize fetching of authorities with just @OneToMany / @ManyToOne if that makes sense. I can’t find similar solution in official documentation

Account class:

@Entity
@Table(name = "accounts")
public class Account implements UserDetails, CredentialsContainer {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    private String firstName;

    private String lastName;

    private String username;

    private String email;

    private String password;

    private LocalDateTime createdAt;

    private Boolean verified;

    private Boolean locked;

    private String hashIdentifier;

    @OneToMany(mappedBy = "account")
    private Set<AccountAuthority> accountsAuthorities;
}

Authority class:

@Entity
@Table(name = "authorities")
public class Authority {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    private String authorityName;

    @OneToMany(mappedBy = "authority")
    private Set<AccountAuthority> accountsAuthorities;
}

AccountAuthority class:

@Entity
@Table(name = "accounts_authorities")
public class AccountAuthority implements GrantedAuthority {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @ManyToOne
    @JoinColumn(name = "account_id")
    private Account account;

    @ManyToOne
    @JoinColumn(name = "authority_id")
    private Authority authority;

    private LocalDateTime assignedAt;

    private LocalDateTime revokedAt;
}

JpaRepository:

@Repository
public interface AccountRepository extends JpaRepository<Account, UUID>, JpaSpecificationExecutor<Account> {
    Optional<Account> findByUsername(String username);
}
@Override
    public List<Account> loadAllAccounts() {
        return accountRepository.findAll();
    }

New contributor

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

4

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