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). Apart from that, i want to track the history when the authority (role) has been revoked, so by database will contain attributes like: assigned_at, revoked_at.

This is my Account class: it will implement UserDetails and CredentialsContainer

@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;
}

This is 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;
}

This is relation class AccountAuthority:

@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;
}

Now when i fetch the list of accounts using JpaRepository:

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

The Hibernate has generated 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=?

Correct me if I am wrong, the way I understand it that when account is fetched it will fetch the list of authorities.

My question is is it possible to prevent fetching the list of authorities when fetching account, for example so that only this query is executed:

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

I tried setting fetch = FetchType.LAZY on all the @OneToMany and @ManyToOne, but that did not work.

Also, the database will contain multiple records of same account_id and authority_id since I want to track the history of authority (role) being revoked:

IMAGE OF TABLE – open here

Is it possible to alter the authorities fetching so that only authorities where revoked_at is 9999-12-31 23:59:59 are fetched, something like this:

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 


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 revoked_at='9999-12-31 23:59:59'

I tried using JPA specifications, but didn’t work the way I wanted.

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.

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