Код: Выделить всё
@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private List roles = new ArrayList();
}
Код: Выделить всё
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditableEntity {
@CreatedDate
@Column(name = "created_on")
private LocalDateTime createdOn;
@CreatedBy
@Column(name = "created_by")
private User createdBy;
@LastModifiedDate
@Column(name = "updated_on")
private LocalDateTime updatedOn;
@LastModifiedBy
@Column(name = "updated_by")
private User updatedBy;
}
- createdBy >
- createdDate
- updatedBy
- updatedDate
В getCurrentAuditor< /code> реализация AuditorAware Я бы получил текущего участника из аутентификации, нашел объект User из базы данных с тем же идентификатором и вернул его:
Код: Выделить всё
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails user = (CustomUserDetails) authentication.getPrincipal();
return userService.findUserById(user.getId());
Код: Выделить всё
@Data
public class CustomUserDetails implements UserDetails {
private Long id;
private String username;
private String password;
private Collection
Подробнее здесь: [url]https://stackoverflow.com/questions/78250782/spring-jpa-data-auditing-how-to-design-it[/url]