{"id":353573,"date":"2024-05-20T22:30:38","date_gmt":"2024-05-20T22:30:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=353573"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=353573","title":{"rendered":"<span>\u0410\u0443\u0434\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 Spring Data JPA<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u0417\u0430\u0434\u0430\u0447\u0430: \u0432\u00a0\u0411\u0414 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u0442\u043e \u0435\u0451 \u043e\u0431\u043d\u043e\u0432\u0438\u043b, \u0438 \u043a\u0442\u043e \u0435\u0451 \u0443\u0434\u0430\u043b\u0438\u043b.<\/em><\/strong><\/p>\n<p>\u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u043a\u0430\u043a\u00a0\u0432\u0437\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0441\u0443\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0432\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432\u00a0\u043c\u0435\u0442\u043e\u0434\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u00a0\u043d\u0451\u043c \u0438 \u00ab\u0437\u0430\u0441\u0435\u0442\u0430\u0442\u044c\u00bb \u0435\u0433\u043e \u0432\u00a0\u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f (\u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0432\u0435\u0437\u0434\u0435 \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430 \u043f\u043e\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u0443), \u0430\u00a0\u0441\u00a0\u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \u043a\u0430\u043a\u2011\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u00a0\u044f\u0432\u043d\u043e \u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0440\u044f\u0434 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0437\u0432\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0447\u0442\u043e\u2011\u0442\u043e \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e (\u043d\u043e\u0432\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u043d\u0435\u00a0\u0437\u043d\u0430\u0442\u044c \u043e\u00a0\u0442\u0430\u043a\u043e\u0439 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u0430\u00a0\u0441\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u043e\u00a0\u043d\u0435\u0439)). <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a52\/d3f\/c39\/a52d3fc39642da903f1c86215cef5808.png\" width=\"780\" height=\"440\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a52\/d3f\/c39\/a52d3fc39642da903f1c86215cef5808.png\"\/><\/figure>\n<p>\u041c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u043d\u043e\u00a0\u043f\u043e\u0433\u0443\u0433\u043b\u0438\u0432, \u044f \u043d\u0435\u00a0\u043d\u0430\u0448\u0451\u043b \u043a\u0430\u043a\u043e\u0433\u043e\u2011\u0442\u043e \u044f\u0432\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a\u00a0\u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443\u00a0\u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u00a0\u0436\u0438\u0432\u043e\u0442\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 name, \u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0451\u0442\u0441\u044f \u0438\u0437\u00a0\u043f\u0430\u0441\u043f\u043e\u0440\u0442\u0430 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u044f\u00a0\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443:<\/p>\n<pre><code class=\"xml\">&lt;changeSet id=\"create-table-animal.xml\" author=\"alexander\">         &lt;createTable tableName=\"animal\">             &lt;column name=\"id\" type=\"UUID\">                 &lt;constraints nullable=\"false\" primaryKey=\"true\"\/>             &lt;\/column>             &lt;column name=\"name\" type=\"VARCHAR(255)\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"code\" type=\"VARCHAR(255)\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"created_at\" type=\"TIMESTAMP\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"updated_at\" type=\"TIMESTAMP\">             &lt;\/column>  &lt;!\u2014 \u0423 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u0442\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f-->             &lt;column name=\"deleted_at\" type=\"TIMESTAMP\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"created_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"created_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"updated_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"updated_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"deleted_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"deleted_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>         &lt;\/createTable>  &lt;!-- \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043d\u0435 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0445 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445-->         &lt;sql>             create unique index unique_code on animal(code) where deleted_at is null;         &lt;\/sql>     &lt;\/changeSet> <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9fc\/ee1\/04b\/9fcee104b8864a79596d2cf5ba51f88a.png\" width=\"793\" height=\"381\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9fc\/ee1\/04b\/9fcee104b8864a79596d2cf5ba51f88a.png\"\/><\/figure>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0442\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0434\u0430\u0442\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043e\u0442\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">\/**  * \u041e\u0431\u0449\u0438\u0439 \u043a\u0430\u0440\u043a\u0430\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439.  *\/ @Getter @ToString(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true) @NoArgsConstructor @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class AbstractAuditableEntity {      @CreatedDate     @Column(updatable = false)     @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant createdAt;      @LastModifiedDate     @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant updatedAt;  } <\/code><\/pre>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0430\u0443\u0434\u0438\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439:<\/strong><\/p>\n<pre><code class=\"java\">\/**  * \u0421\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0430\u0443\u0434\u0438\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.  *\/ @Setter @Getter @ToString(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true) @NoArgsConstructor @MappedSuperclass @EntityListeners(AuditUserListener.class) public class AuditUser extends AbstractAuditableEntity {      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b.      *\/     @ToString.Include     @Column(updatable = false)     private String createdBy;      \/**      * email \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b.      *\/     @ToString.Include     @Column(updatable = false)     private String createdByMail;      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b.      *\/     @ToString.Include     private String updatedBy;      \/**      * email \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b.      *\/     @ToString.Include     private String updatedByMail;      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b.      *\/     @ToString.Include     private String deletedBy;      \/**      * email \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b.      *\/     @ToString.Include     private String deletedByMail;  }<\/code><\/pre>\n<p><strong>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 AuditUserListener.class \u2013 \u0435\u0433\u043e \u0443\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043c\u044b \u0441\u0430\u043c\u0438.<\/strong><\/p>\n<pre><code class=\"java\">@Configurable @RequiredArgsConstructor(onConstructor_ = {@Lazy}) public class AuditUserListener {      \/**      * \u0421\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430      *\/     private final UserBuilderFromContext userBuilderFromContext;      private final EntityManager entityManager;      @PrePersist     private void beforeAnyCreate(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setCreatedBy(user.getFullName());         audit.setCreatedByMail(user.getEmail());     }      @PreUpdate     private void beforeAnyUpdate(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setUpdatedBy(user.getFullName());         audit.setUpdatedByMail(user.getEmail());     }      @PreRemove     private void beforeAnyRemove(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setDeletedBy(user.getFullName());         audit.setDeletedByMail(user.getEmail());         \/\/ \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0434\u0451\u0440\u0433\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0431\u0434 \u043f\u043e id \u043d\u043e save  \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f         \/\/ \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f sql \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0434\u0430\u0442\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f         \/\/ \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d entityManager \u0447\u0442\u043e\u0431\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0435\u0448 \u0441 \u0431\u0434         entityManager.flush();     }  }<\/code><\/pre>\n<p><strong>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435!: \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u00a0\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u041d\u043e\u00a0\u0442.\u043a. \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u0441\u0435 CRUD \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e.<\/strong><\/p>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u043b\u0435\u043d\u0438\u0432\u0430\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u043d\u043e\u0432: <\/p>\n<pre><code class=\"java\">@RequiredArgsConstructor(onConstructor_ = {@Lazy}<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/355\/b2d\/044\/355b2d044644e265546f20f1f0079a25.png\" width=\"1491\" height=\"266\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/355\/b2d\/044\/355b2d044644e265546f20f1f0079a25.png\"\/><\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442\u00a0\u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441\u0430 AuditUser, \u0441\u043e\u0437\u0434\u0430\u0432 \u043d\u0430\u0448\u0443 entity Animal. <\/p>\n<p><strong>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u0430\u043b\u0438\u043b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0438\u0437\u00a0\u0431\u0434 \u0435\u0451 \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u043c, \u0430\u00a0\u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u044f\u0433\u043a\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 (Soft delete).<\/strong><\/p>\n<pre><code class=\"java\">\/**  * Entity Animal  *\/ @Getter @Setter @ToString(onlyExplicitlyIncluded = true) @NoArgsConstructor @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @Entity @Table(name = \"animal\") \/\/ \u041c\u044f\u0433\u043a\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 @SQLDelete(sql = \"update animal set deleted_at=now() AT TIME ZONE 'UTC' where id = ?\") \/\/ \u0424\u0438\u043b\u044c\u0442\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 \u0431\u0434 @Where(clause = \" deleted_at is null \") public class Animal extends AuditUser {      \/**      * \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440.      *\/     @Id     @GeneratedValue     @ToString.Include     @EqualsAndHashCode.Include     protected UUID id;      \/**      * \u041a\u043e\u0434 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e \u043f\u043e \u043f\u0430\u0441\u043f\u043e\u0440\u0442\u0443      *\/     @Column     @ToString.Include     private String code;      \/**      * \u0418\u043c\u044f \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e      *\/     @Column     @ToString.Include     private String name;      @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant deletedAt;  }<\/code><\/pre>\n<h4>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/h4>\n<pre><code class=\"bash\">curl -X 'POST' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -H 'Content-Type: application\/json' \\   -d '{   \"name\": \"\u041c\u0443\u0440\u0437\u0438\u043a\",   \"code\": \"50 05 984 929\" }'<\/code><\/pre>\n<pre><code class=\"bash\">curl -X 'PUT' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -d '{   \"name\": \"\u0411\u0430\u0440\u0441\u0438\u043a\",   \"code\": \"50 05 984 929\",   \"id\": \"d085d72a-b15f-491b-8dbc-f6dbe3dba2e1\" }'<\/code><\/pre>\n<pre><code class=\"bash\">curl -X 'DELETE' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -H 'Content-Type: application\/json' \\   -d '[\"d085d72a-b15f-491b-8dbc-f6dbe3dba2e1\"]'  <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8ca\/326\/23a\/8ca32623a9fabeea5f4c1dc78d7438c6.png\" width=\"1575\" height=\"189\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8ca\/326\/23a\/8ca32623a9fabeea5f4c1dc78d7438c6.png\"\/><\/figure>\n<h3>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c <strong>\u043b\u044e\u0431\u0443\u044e Entity<\/strong> (\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u044b, \u0442\u0438\u043a\u0435\u0442\u044b), <strong>\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044f\u0441\u044c \u043e\u0442\u00a0AuditUser, \u0438 \u0430\u0443\u0434\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/strong>. \u0411\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043a\u043e \u0432\u0441\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0430\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043d\u043e\u0441\u044f\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u043c\u0443 \u0441\u043b\u043e\u044e \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u00ab\u0441\u0435\u0442\u0430\u0442\u044c\u00bb \u0435\u0433\u043e \u0432\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u043e\u043b\u044f. \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/757866\/\"> https:\/\/habr.com\/ru\/articles\/757866\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong><em>\u0417\u0430\u0434\u0430\u0447\u0430: \u0432\u00a0\u0411\u0414 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u0442\u043e \u0435\u0451 \u043e\u0431\u043d\u043e\u0432\u0438\u043b, \u0438 \u043a\u0442\u043e \u0435\u0451 \u0443\u0434\u0430\u043b\u0438\u043b.<\/em><\/strong><\/p>\n<p>\u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442, \u043a\u0430\u043a\u00a0\u0432\u0437\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0441\u0443\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0432\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432\u00a0\u043c\u0435\u0442\u043e\u0434\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u00a0\u043d\u0451\u043c \u0438 \u00ab\u0437\u0430\u0441\u0435\u0442\u0430\u0442\u044c\u00bb \u0435\u0433\u043e \u0432\u00a0\u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f (\u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0432\u0435\u0437\u0434\u0435 \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430 \u043f\u043e\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u0443), \u0430\u00a0\u0441\u00a0\u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \u043a\u0430\u043a\u2011\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u00a0\u044f\u0432\u043d\u043e \u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0440\u044f\u0434 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0437\u0432\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0447\u0442\u043e\u2011\u0442\u043e \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e (\u043d\u043e\u0432\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u043d\u0435\u00a0\u0437\u043d\u0430\u0442\u044c \u043e\u00a0\u0442\u0430\u043a\u043e\u0439 \u043d\u0435\u044f\u0432\u043d\u043e\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u0430\u00a0\u0441\u0442\u0430\u0440\u044b\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u043e\u00a0\u043d\u0435\u0439)). <\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u043d\u043e\u00a0\u043f\u043e\u0433\u0443\u0433\u043b\u0438\u0432, \u044f \u043d\u0435\u00a0\u043d\u0430\u0448\u0451\u043b \u043a\u0430\u043a\u043e\u0433\u043e\u2011\u0442\u043e \u044f\u0432\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a\u00a0\u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0443\u00a0\u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u00a0\u0436\u0438\u0432\u043e\u0442\u043d\u044b\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 name, \u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0451\u0442\u0441\u044f \u0438\u0437\u00a0\u043f\u0430\u0441\u043f\u043e\u0440\u0442\u0430 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u044f\u00a0\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443:<\/p>\n<pre><code class=\"xml\">&lt;changeSet id=\"create-table-animal.xml\" author=\"alexander\">         &lt;createTable tableName=\"animal\">             &lt;column name=\"id\" type=\"UUID\">                 &lt;constraints nullable=\"false\" primaryKey=\"true\"\/>             &lt;\/column>             &lt;column name=\"name\" type=\"VARCHAR(255)\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"code\" type=\"VARCHAR(255)\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"created_at\" type=\"TIMESTAMP\">                 &lt;constraints nullable=\"false\"\/>             &lt;\/column>             &lt;column name=\"updated_at\" type=\"TIMESTAMP\">             &lt;\/column>  &lt;!\u2014 \u0423 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0430\u0442\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f-->             &lt;column name=\"deleted_at\" type=\"TIMESTAMP\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"created_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"created_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"updated_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"updated_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>  &lt;!\u2014 \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b \u0437\u0430\u043f\u0438\u0441\u044c-->             &lt;column name=\"deleted_by\" type=\"VARCHAR(255)\">             &lt;\/column>             &lt;column name=\"deleted_by_mail\" type=\"VARCHAR(255)\">             &lt;\/column>         &lt;\/createTable>  &lt;!-- \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043d\u0435 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0445 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445-->         &lt;sql>             create unique index unique_code on animal(code) where deleted_at is null;         &lt;\/sql>     &lt;\/changeSet> <\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u0442\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0434\u0430\u0442\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043e\u0442\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">\/**  * \u041e\u0431\u0449\u0438\u0439 \u043a\u0430\u0440\u043a\u0430\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439.  *\/ @Getter @ToString(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true) @NoArgsConstructor @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class AbstractAuditableEntity {      @CreatedDate     @Column(updatable = false)     @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant createdAt;      @LastModifiedDate     @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant updatedAt;  } <\/code><\/pre>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0430\u0443\u0434\u0438\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439:<\/strong><\/p>\n<pre><code class=\"java\">\/**  * \u0421\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0430\u0443\u0434\u0438\u0442\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.  *\/ @Setter @Getter @ToString(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true) @NoArgsConstructor @MappedSuperclass @EntityListeners(AuditUserListener.class) public class AuditUser extends AbstractAuditableEntity {      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b.      *\/     @ToString.Include     @Column(updatable = false)     private String createdBy;      \/**      * email \u043a\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043b.      *\/     @ToString.Include     @Column(updatable = false)     private String createdByMail;      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b.      *\/     @ToString.Include     private String updatedBy;      \/**      * email \u043a\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u043b.      *\/     @ToString.Include     private String updatedByMail;      \/**      * \u0424\u0418\u041e \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b.      *\/     @ToString.Include     private String deletedBy;      \/**      * email \u043a\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u043b.      *\/     @ToString.Include     private String deletedByMail;  }<\/code><\/pre>\n<p><strong>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 AuditUserListener.class \u2013 \u0435\u0433\u043e \u0443\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043c\u044b \u0441\u0430\u043c\u0438.<\/strong><\/p>\n<pre><code class=\"java\">@Configurable @RequiredArgsConstructor(onConstructor_ = {@Lazy}) public class AuditUserListener {      \/**      * \u0421\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430      *\/     private final UserBuilderFromContext userBuilderFromContext;      private final EntityManager entityManager;      @PrePersist     private void beforeAnyCreate(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setCreatedBy(user.getFullName());         audit.setCreatedByMail(user.getEmail());     }      @PreUpdate     private void beforeAnyUpdate(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setUpdatedBy(user.getFullName());         audit.setUpdatedByMail(user.getEmail());     }      @PreRemove     private void beforeAnyRemove(AuditUser audit) {         var user = userBuilderFromContext.getUserFromContext();         audit.setDeletedBy(user.getFullName());         audit.setDeletedByMail(user.getEmail());         \/\/ \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0434\u0451\u0440\u0433\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0431\u0434 \u043f\u043e id \u043d\u043e save  \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f         \/\/ \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f sql \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0434\u0430\u0442\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f         \/\/ \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d entityManager \u0447\u0442\u043e\u0431\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0435\u0448 \u0441 \u0431\u0434         entityManager.flush();     }  }<\/code><\/pre>\n<p><strong>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435!: \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u00a0\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u041d\u043e\u00a0\u0442.\u043a. \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432\u00a0\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u0441\u0435 CRUD \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e.<\/strong><\/p>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u043b\u0435\u043d\u0438\u0432\u0430\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u043d\u043e\u0432: <\/p>\n<pre><code class=\"java\">@RequiredArgsConstructor(onConstructor_ = {@Lazy}<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442\u00a0\u0441\u0443\u043f\u0435\u0440\u043a\u043b\u0430\u0441\u0441\u0430 AuditUser, \u0441\u043e\u0437\u0434\u0430\u0432 \u043d\u0430\u0448\u0443 entity Animal. <\/p>\n<p><strong>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u0430\u043b\u0438\u043b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0438\u0437\u00a0\u0431\u0434 \u0435\u0451 \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u043c, \u0430\u00a0\u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043c\u044f\u0433\u043a\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 (Soft delete).<\/strong><\/p>\n<pre><code class=\"java\">\/**  * Entity Animal  *\/ @Getter @Setter @ToString(onlyExplicitlyIncluded = true) @NoArgsConstructor @EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) @Entity @Table(name = \"animal\") \/\/ \u041c\u044f\u0433\u043a\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 @SQLDelete(sql = \"update animal set deleted_at=now() AT TIME ZONE 'UTC' where id = ?\") \/\/ \u0424\u0438\u043b\u044c\u0442\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 \u0431\u0434 @Where(clause = \" deleted_at is null \") public class Animal extends AuditUser {      \/**      * \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440.      *\/     @Id     @GeneratedValue     @ToString.Include     @EqualsAndHashCode.Include     protected UUID id;      \/**      * \u041a\u043e\u0434 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e \u043f\u043e \u043f\u0430\u0441\u043f\u043e\u0440\u0442\u0443      *\/     @Column     @ToString.Include     private String code;      \/**      * \u0418\u043c\u044f \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e      *\/     @Column     @ToString.Include     private String name;      @Type(type = \"java.time.Instant\")     @EqualsAndHashCode.Include     @ToString.Include     private Instant deletedAt;  }<\/code><\/pre>\n<h4>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/h4>\n<pre><code class=\"bash\">curl -X 'POST' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -H 'Content-Type: application\/json' \\   -d '{   \"name\": \"\u041c\u0443\u0440\u0437\u0438\u043a\",   \"code\": \"50 05 984 929\" }'<\/code><\/pre>\n<pre><code class=\"bash\">curl -X 'PUT' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -d '{   \"name\": \"\u0411\u0430\u0440\u0441\u0438\u043a\",   \"code\": \"50 05 984 929\",   \"id\": \"d085d72a-b15f-491b-8dbc-f6dbe3dba2e1\" }'<\/code><\/pre>\n<pre><code class=\"bash\">curl -X 'DELETE' \\   'http:\/\/localhost:8080\/api\/v1\/animal' \\   -H 'accept: *\/*' \\   -H 'Authorization: Bearer \u0422\u0423\u0422 \u041c\u041e\u0419 JWT TOKEN' \\   -H 'Content-Type: application\/json' \\   -d '[\"d085d72a-b15f-491b-8dbc-f6dbe3dba2e1\"]'  <\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 <\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c <strong>\u043b\u044e\u0431\u0443\u044e Entity<\/strong> (\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u044b, \u0442\u0438\u043a\u0435\u0442\u044b), <strong>\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044f\u0441\u044c \u043e\u0442\u00a0AuditUser, \u0438 \u0430\u0443\u0434\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/strong>. \u0411\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u00a0\u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043a\u043e \u0432\u0441\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0430\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043d\u043e\u0441\u044f\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u00a0\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u00a0\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u043c\u0443 \u0441\u043b\u043e\u044e \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437\u00a0\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u00ab\u0441\u0435\u0442\u0430\u0442\u044c\u00bb \u0435\u0433\u043e \u0432\u00a0\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u043e\u043b\u044f. \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/757866\/\"> https:\/\/habr.com\/ru\/articles\/757866\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-353573","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/353573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353573"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/353573\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}