Я изучаю Spring Data JPA и пытаюсь построить связь ManyToMany между учетной записью класса и авторитетом класса. Я также хочу отслеживать историю отзыва полномочий. Таблица базы данных «accounts_authorities» будет содержать такие атрибуты, как назначенные_at, отозванные_at.
Каковы возможные способы настройки получения полномочий при получении учетной записи?
В настоящее время Hibernate генерирует два SQL-запроса:
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=?
Как настроить получение полномочий, чтобы извлекались только активные полномочия. Что-то вроде добавления предложения WHERE revoked_at is null:
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
Я хотел бы использовать Спецификации JPA и Criteria Builder, если мне это нужно. Или есть способ настроить получение полномочий с помощью @OneToMany/@ManyToOne, если это имеет смысл. Я не могу найти подобное решение в официальной документации.
Класс учетной записи:
@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 accountsAuthorities;
}
Класс полномочий:
@Entity
@Table(name = "authorities")
public class Authority {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private String authorityName;
@OneToMany(mappedBy = "authority")
private Set accountsAuthorities;
}
Класс 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;
}
JpaRepository:
@Repository
public interface AccountRepository extends JpaRepository, JpaSpecificationExecutor {
}
@Override
public List loadAllAccounts() {
return accountRepository.findAll();
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... -with-a-ne
Как фильтровать выборку Hibernate? Spring Data JPA, отношение ManyToMany с новой сущностью, спецификации JPA ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как настроить получение? Spring Data JPA, отношение ManyToMany с новым объектом
Anonymous » » в форуме JAVA - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как настроить получение? Spring Data JPA, отношение ManyToMany с новым объектом
Anonymous » » в форуме JAVA - 0 Ответы
- 22 Просмотры
-
Последнее сообщение Anonymous
-