{"id":380101,"date":"2024-06-27T15:00:09","date_gmt":"2024-06-27T15:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=380101"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=380101","title":{"rendered":"<span>Middle-starter-pack \u043f\u043e spring data<\/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<details class=\"spoiler\">\n<summary>\u043a\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u0442\u0430\u043a\u043e\u0439 \u0430\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0421\u0442\u0430\u0440\u0430\u043a\u043e\u0436\u0435\u0432 \u0414\u0435\u043d\u0438\u0441, <br \/>\u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u044f\u0432\u043b\u044f\u044e\u0441\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0418\u0422 \u043b\u0438\u0434\u043e\u043c \u043a\u0440\u043e\u0441\u0441\u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0444\u0438\u043d\u0442\u0435\u0445\u0435 (\u0432 \u0447\u0438\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 7 back-end \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432),<br \/>\u0438\u043c\u0435\u044e 5 \u043b\u0435\u0442 \u0441\u0442\u0430\u0436\u0430 \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 (java back-end),<br \/>\u0417\u0430 \u043f\u043b\u0435\u0447\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u043e\u043f\u044b\u0442 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0440\u0435\u0432\u043d\u0438\u043c \u043b\u0435\u0433\u0430\u0441\u0438, \u0442\u0430\u043a \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u044b &#171;\u0441 \u043d\u0443\u043b\u044f&#187;.<br \/>(\u0432\u044b\u0441\u0442\u0440\u0430\u0434\u0430\u0442\u044c \u0443\u0441\u043f\u0435\u043b \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e: \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430, \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438, \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u0434)<\/p>\n<\/div>\n<\/details>\n<h2>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f?<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u0442\u0441\u044f \u043b\u044e\u0431\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c spring-data-jpa.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0447\u0435\u0440\u043f\u044b\u0432\u0430\u044e\u0449\u0438\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u043c \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0442\u0435\u0445, \u043a\u0442\u043e \u0445\u043e\u0442\u044c \u0440\u0430\u0437 \u0441\u0442\u0430\u0432\u0438\u043b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e Transactional \u0432 spring \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u0438\u0441\u043a\u0440\u0435\u043d\u043d\u0435 \u0443\u0434\u0438\u0432\u043b\u044f\u043b\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u043d\u0435 \u0441\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u044e \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0443\u0440\u043e\u0432\u043d\u044f middle \u0438 \u0432\u044b\u0448\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u044d\u0442\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u0432\u043b\u0430\u0434\u0435\u0442\u044c.<\/p>\n<h2>\u041c\u0430\u0433\u0438\u0438 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442<\/h2>\n<p>\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c \u0432 \u043a\u043e\u0434\u0435 \u043a\u0430\u043a \u043a \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u0430\u043a\u043b\u0438\u043d\u0430\u043d\u0438\u044f\u043c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u0430\u0436\u0435 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u044f\u0441\u044c \u043e \u0442\u043e\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u0438 &#171;\u0437\u0430\u043a\u043b\u0438\u043d\u0430\u043d\u0438\u044f&#187; \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442.<\/p>\n<p>\u041d\u0435 \u043c\u043e\u0433\u0443 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0442\u044c \u043d\u0430 100% \u0447\u0442\u043e \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u041d\u0430\u0440\u043d\u0438\u0438, \u043d\u043e \u0441\u043e \u0432\u0441\u0435\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u044f\u0432\u043b\u044f\u044e, \u0447\u0442\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u0430\u0433\u0438\u0438 \u0442\u043e\u0447\u043d\u043e \u043d\u0435\u0442, \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u0442\u043e-\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a, \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u043f\u0438\u0441\u0430\u043d.<\/p>\n<p>\u041a\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 (\u0438\u043b\u0438 \u043d\u0435 \u043c\u044b), \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0447\u0442\u043e \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a, \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u044b \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u043c\/\u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c.<\/p>\n<details class=\"spoiler\">\n<summary>\u043e\u0433\u043e\u0432\u043e\u0440\u043e\u0447\u043a\u0430<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0415\u0441\u0442\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0444\u0430\u043a\u0442\u043e\u0440\u044b \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0435 \u0442\u0430\u043a \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/habr.com\/ru\/articles\/401681\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0441\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043b\u0443\u0447\u0438<\/a>, \u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0447\u044c.<br \/>\u0422\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442\u044b \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0440\u0443\u0433\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f (\u043c\u043e\u0436\u0435\u0442 \u0438 \u043f\u0440\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0448\u0443 \u043f\u043e\u0437\u0436\u0435).<\/p>\n<\/div>\n<\/details>\n<h2>\u041a \u0434\u0435\u043b\u0443<\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>pom<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">    &lt;parent>         &lt;groupId>org.springframework.boot&lt;\/groupId>         &lt;artifactId>spring-boot-starter-parent&lt;\/artifactId>         &lt;version>2.3.5.RELEASE&lt;\/version>         &lt;relativePath\/>     &lt;\/parent>      &lt;groupId>org.example&lt;\/groupId>     &lt;artifactId>transactional-demo&lt;\/artifactId>     &lt;version>1.0-SNAPSHOT&lt;\/version>      &lt;properties>         &lt;maven.compiler.source>11&lt;\/maven.compiler.source>         &lt;maven.compiler.target>11&lt;\/maven.compiler.target>         &lt;project.build.sourceEncoding>UTF-8&lt;\/project.build.sourceEncoding>     &lt;\/properties>      &lt;dependencies>         &lt;dependency>             &lt;groupId>org.springframework.boot&lt;\/groupId>             &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId>         &lt;\/dependency>         &lt;dependency>             &lt;groupId>org.postgresql&lt;\/groupId>             &lt;artifactId>postgresql&lt;\/artifactId>             &lt;optional>true&lt;\/optional>         &lt;\/dependency>         &lt;dependency>             &lt;groupId>org.projectlombok&lt;\/groupId>             &lt;artifactId>lombok&lt;\/artifactId>             &lt;optional>true&lt;\/optional>         &lt;\/dependency>          &lt;dependency>             &lt;groupId>org.springframework.boot&lt;\/groupId>             &lt;artifactId>spring-boot-starter-test&lt;\/artifactId>         &lt;\/dependency>     &lt;\/dependencies><\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>main<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">package org.example;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class Application {     public static void main(String[] args) {         SpringApplication.run(Application.class);     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">package org.example.data;  import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;  import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import java.util.UUID;  @Data @Entity @Builder @AllArgsConstructor @NoArgsConstructor @Table(name = \"example\") public class ExampleEntity {     @Id     @GeneratedValue     private UUID id;      private String value; } <\/code><\/pre>\n<pre><code class=\"java\">package org.example.data;  import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository;  import java.util.Optional; import java.util.UUID;  @Repository public interface ExampleEntityRepository extends CrudRepository&lt;ExampleEntity, UUID> {          Optional&lt;ExampleEntity> findByValue(String value); } <\/code><\/pre>\n<pre><code class=\"java\">package org.example.data;  import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service;  import java.util.UUID;  @Service @RequiredArgsConstructor public class ExampleService {      private final ExampleEntityRepository exampleEntityRepository;      public void save(String value) {         exampleEntityRepository.save(ExampleEntity.builder()                 .value(value)                 .build());     }          public ExampleEntity get(UUID id) {         return exampleEntityRepository.findById(id)                 .orElseThrow();     }  } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p><strong>\u041a\u0435\u0439\u0441 1: \u043a\u043b\u0430\u0441\u0441\u0438\u043a\u0430<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u043b \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438.<br \/>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 saveAll() ?<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service;  import javax.transaction.Transactional;  @Service @RequiredArgsConstructor public class FirstCase {          private final ExampleService exampleService;          public void saveAll() {         saveFirst();         saveSecond();     }          @Transactional     public void saveFirst() {         exampleService.save(\"first\");     }          @Transactional     public void saveSecond() {         exampleService.save(\"second\");     } } <\/code><\/pre>\n<p>\u041b\u044e\u0431\u043e\u0439 \u0437\u0434\u0440\u0430\u0432\u043e\u043c\u044b\u0441\u043b\u044f\u0449\u0438\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0430\u0441\u043f\u0435\u043a\u0442\u044b \u0432 \u0441\u043f\u0440\u0438\u043d\u0433\u0435, \u0434\u043e\u0433\u0430\u0434\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442<\/p>\n<details class=\"spoiler\">\n<summary>\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u0438 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>\u0412 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0435 (\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u043b\u0438 \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c).<br \/>\u041a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">package org.example.decorator;  import org.springframework.stereotype.Service;  public class DecoratorExample {      \/**      * \u041d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438      *\/     @Service     public static class SomeService {          public Object doSome() {             return new Object();         }     }      \/**      * \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \"\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\" \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435      *\/     public static class SomeServiceDecorator extends SomeService {          private final SomeService originService;          public SomeServiceDecorator(SomeService originService) {             this.originService = originService;         }          @Override         public Object doSome() {             before();             try {                 Object returnValue = originService.doSome(); \/\/ \u0432\u044b\u0437\u043e\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430                 after(returnValue);                 return returnValue;             } catch (Exception e) {                 return handle(e);             }         }          private void before() {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438         }          private void after(Object returnValue) {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043a\u043e\u043c\u043c\u0438\u0442\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438         }          private Object handle(Exception e) {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0442\u043a\u0430\u0442\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438             throw new RuntimeException(e);         }     } } <\/code><\/pre>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b &#171;\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438&#187; \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438 \u0442\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430, \u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b, \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/p>\n<pre><code class=\"java\">package org.example.decorator;  import org.springframework.stereotype.Service;  public class DelegateDecoratorExample {      \/**      * \u041d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438      *\/     @Service     public static class SomeService {          public Object doSome() {             return new Object();         }     }      \/**      * \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \"\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\" \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435      *\/     public static class SomeDelegateServiceDecorator extends SomeService {          private final SomeService originService;          public SomeDelegateServiceDecorator(SomeService originService) {             this.originService = originService;         }          @Override         public Object doSome() {             return originService.doSome(); \/\/ \u0432\u044b\u0437\u043e\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430         }     } } <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, ExampleService \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0435\u0440\u043d\u0443\u0442 \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0445\u043e\u0434\u0443 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 (\u043b\u0438\u0431\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0442\u0435 \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0447\u0442\u043e \u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043d\u0438\u043a\u0442\u043e \u0438\u0437 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u044f\u0432\u043d\u043e \u043d\u0435 \u043f\u043e\u043b\u0435\u0437\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c (<a href=\"https:\/\/www.youtube.com\/watch?v=BmBr5diz8WA\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u0430\u0437\u0431\u043e\u0440 \u043e\u0442 \u0433\u0443\u0440\u0443<\/a>).<br \/>\u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e6d\/a7f\/25f\/e6da7f25f52a7d9fb95ccab765f9e8ca.png\" alt=\"\" title=\"\" width=\"1386\" height=\"885\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e6d\/a7f\/25f\/e6da7f25f52a7d9fb95ccab765f9e8ca.png\"\/><\/figure>\n<\/p>\n<\/div>\n<\/details>\n<p><strong>\u041a\u0435\u0439\u0441 2: JPA \u0438 persistenceContext<\/strong><\/p>\n<p>\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u0440\u0430\u0431\u043e\u0442\u0430\u044f \u0441 JPA \u0432 \u043b\u0438\u0446\u0435 hibernate, \u043f\u0440\u0438\u043a\u0440\u044b\u0442\u043e\u0433\u043e spring-data, \u0434\u0430\u0436\u0435 \u043d\u0435 \u0434\u043e\u0433\u0430\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 persistenceContext (\u043b\u0438\u0431\u043e \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0435\u0441\u0442\u044c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u044f).<\/p>\n<p>\u0414\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u0431\u043e\u0440 \u0435\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 (\u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043d\u0438\u0433\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 &#171;Java Persistence API \u0438 Hibernate&#187;).<br \/>\u041f\u0440\u043e\u0431\u0435\u0433\u0443\u0441\u044c \u043f\u043e \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u0438:<\/p>\n<p><strong>persistenceContext<\/strong> &#8212; \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043a\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u044d\u0448, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u0441\u0435 \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0438\u0437 \u0411\u0414 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0439 \u0441\u0430\u043c\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0438\u043c\u0435\u0435\u0442 2 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<br \/>1. \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0441\u043e\u0431\u043e\u0439 \u0442\u043e, \u0447\u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u0438\u0434\u043d\u043e \u0432 \u0411\u0414<br \/>2. entity &#8212; \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0437 \u043f1 (\u043b\u0438\u0431\u043e \u043d\u043e\u0432\u044b\u0435 &#8212; \u0440\u0443\u043a\u0430\u043c\u0438 \u0432 \u043a\u043e\u0434\u0435), \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u043d\u0438\u043c\u0438 \u043c\u044b \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0435.<\/p>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u043e <strong>flush \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/strong>, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u0444\u0444 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c \u0411\u0414 (\u043f1) \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u043d\u0430\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b (\u043f2), \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 \u0434\u0438\u0444\u0444 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0411\u0414 \u0432 \u0432\u0438\u0434\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441 \u043a\u043e\u0433\u0434\u0430?<br \/>1. \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043c\u0438\u0442\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<br \/>2. \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043a\u043e\u0433\u0434\u0430 \u0445\u0438\u0431\u0435\u0440 \u0441\u043e\u0447\u0442\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0434 \u043a\u0430\u043a\u0438\u043c \u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u043c \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u043c \u0432 \u0411\u0414).<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 save \u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u0441\u0440\u0430\u0437\u0443, \u0438 \u0434\u0430\u0436\u0435, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u0430\u043a Transactional, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u043e \u0441\u0442\u0430\u043a\u0442\u0440\u0435\u0439\u0441\u0443, \u0438 \u043a\u043e\u043c\u043c\u0438\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u0430\u043c \u0436\u0435. (\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0438\u0436\u0435)<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;  public class FlushCase {      @Service     @RequiredArgsConstructor     public static class FirstService {         private final SecondService secondService;         private final ExampleService exampleService;          @Transactional         public void firstDoSame() {             secondService.secondDoSome(); \/\/ \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0435\u0442 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438 \u0447\u0442\u043e \u043c\u044b \u0443\u0432\u0438\u0434\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0411\u0414             exampleService.save(\"firstValue\");         }     }      @Service     @RequiredArgsConstructor     public static class SecondService {         private final ExampleService exampleService;          @Transactional         public void secondDoSome() {             exampleService.save(\"secondValue\");         }      } } <\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0442 \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 <strong>save<\/strong> \u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f <strong>\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442<\/strong> \u043d\u0430\u043c, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043e\u043d \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0435\u0442 <strong>\u044d\u043d\u0442\u0438\u0442\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c.<br \/><\/strong>\u0427\u0430\u0441\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleEntity; import org.example.data.ExampleEntityRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;  public class PersistEntityCase {      @Service     @RequiredArgsConstructor     public static class FirstService {         private final SecondService secondService;          @Transactional         public void doSome() {             ExampleEntity entity = secondService.getOrCreateEntity(\"value\");             entity.setValue(\"newValue\");         }     }      @Service     @RequiredArgsConstructor     public static class SecondService {         private final ExampleEntityRepository exampleEntityRepository;          @Transactional         public ExampleEntity getOrCreateEntity(String value) { \/\/ \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 upsert             return exampleEntityRepository.findByValue(value)                     .orElseGet(() -> ExampleEntity.builder()                             .value(value)                             .build());         }      } } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043f\u0438\u0441\u0430\u0432\u0448\u0438\u0439 FirstService, \u043e\u0441\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d \u043e \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 save(), \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c entity \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u044f\u0442\u0441\u044f \u0432 \u0411\u0414 \u0437\u0430 \u0441\u0447\u0435\u0442 autoFlush \u043f\u0440\u0438 \u043a\u043e\u043c\u043c\u0438\u0442\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. <\/p>\n<p>\u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 entity , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e persistenceContext \u043f\u0440\u043e \u043d\u0435\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 (\u0430 \u0443\u0437\u043d\u0430\u0435\u0442 \u043e\u043d \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 save).<\/p>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0435\u0441\u0442\u044c \u043a\u043e\u0441\u044f\u043a \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 SecondService, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u0430\u044f,<br \/>\u043d\u043e \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 save \u0432 FirstService \u0442\u043e\u0436\u0435 \u043d\u0435 \u0443\u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441 \u043f\u0440\u043e \u0442\u043e \u043a\u0430\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0441 \u0447\u0435\u043c \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c (\u0441 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0439 entity \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441 POJO) \u0432\u043e\u043f\u0440\u043e\u0441 \u043d\u0435 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u044b\u0439, \u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u0440\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044f\u043c\u0438, \u0447\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p><strong>\u041a\u0435\u0439\u0441 3: \u0430\u0442\u0440\u0438\u0431\u0443\u0442 readOnly<\/strong><\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;  public class ReadOnlyCase {          @Service     @RequiredArgsConstructor     public static class FirstService {                  private final SecondService secondService;                  @Transactional(readOnly = true)         public void doSome() {             secondService.secondDoSome();         }     }          @Service     @RequiredArgsConstructor     public static class SecondService {                  private final ExampleService exampleService;                  @Transactional         public void secondDoSome() {             exampleService.save(\"someValue\");         }     } } <\/code><\/pre>\n<p>\u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0430 \u0441\u0442\u044d\u043a\u0435 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c, \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u043a \u043d\u0435\u043c\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438, \u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u043d\u0430 &#171;\u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445&#187; \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<br \/>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u0435 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 FirstService.doSome() \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0432 \u0411\u0414 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u0442\u0440\u043e\u043a\u0438 30 \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 (\u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 SecondService.secondDoSome()).<\/p>\n<p>\u0412\u0441\u0435 \u044d\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e \u043f\u043e\u043f\u044b\u0442\u043a\u0443 \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043e\u0434 \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;  public class ReadOnlyCase2 {      @Service     @RequiredArgsConstructor     public static class FirstService {          private final SecondService secondService;          @Transactional         public void doSome() {             secondService.secondDoSome();         }     }      @Service     @RequiredArgsConstructor     public static class SecondService {          private final ExampleService exampleService;         private final OftenChangeSomeService oftenChangeSomeService;          \/**          * \u043c\u0435\u0442\u043e\u0434, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0430\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u0411\u0414 \u043d\u0435 \u0431\u044b\u043b\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c           *\/         @Transactional(readOnly = true)         public void secondDoSome() {             oftenChangeSomeService.logic();             exampleService.save(\"someValue\");         }     }      \/**      * \u0421\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u043a\u043e\u043c\u043c\u0430\u043d\u0434\u043e\u0439 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442      *\/     @Service     public static class OftenChangeSomeService {                  public Object logic() {             \/\/ \u043d\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438 \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0435 \u043f\u043e\u044f\u0432\u0438\u0441\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c             return new Object();         }     }      } <\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e readOnly \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 32 (\u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u043e\u043c SecondService.secondDoSome())  \u043d\u0430\u043c \u043d\u0435 \u0434\u0430\u0435\u0442 100% \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044e, \u0435\u0441\u043b\u0438 SecondService \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0439-\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u044e.<br \/>\u0412 \u0446\u0435\u043b\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u043a\u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043b\u0435\u0437\u0442\u0438 \u0431\u043e\u043a\u043e\u043c, \u0442\u0430\u043a \u0447\u0442\u043e \u0434\u043b\u044f \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0432\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b.<br \/>+ \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u044d\u0442\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0438\u0431\u043e \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 TransactionManager \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f (\u0438\u043b\u0438 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f)<\/p>\n<p><strong>\u041a\u0435\u0439\u0441 4: \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439<\/strong><\/p>\n<p>\u0427\u0430\u0441\u0442\u043e \u0432\u0438\u0436\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043e\u043b\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;  public class ExceptionCase {      @Service     @RequiredArgsConstructor     public static class FirstService {         private final SecondService secondService;         private final ExampleService exampleService;          @Transactional         public void firstDoSame() {             exampleService.save(\"firstValue\");             try {                 secondService.secondDoSame(); \/\/ \u0435\u0441\u043b\u0438 \u043d\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f, \u0442\u043e \u0441 \u043d\u0430\u0441 \u043d\u0435 \u0443\u0431\u0443\u0434\u0435\u0442             } catch (Exception e) {                 \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u0441 \u043f\u043e\u043b\u0433\u043e\u0449\u0435\u043d\u0438\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f             }         }     }      @Service     @RequiredArgsConstructor     public static class SecondService {         private final ExampleService exampleService;          @Transactional         public void secondDoSame() {             exampleService.save(\"secondValue\");         }      } } <\/code><\/pre>\n<p>\u041d\u043e \u043f\u043e\u0442\u043e\u043c \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u0430 \u0432 \u043b\u043e\u0433\u0430\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 &#171;\u043d\u0435\u0432\u0435\u0434\u043e\u043c\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438&#187;<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/950\/ac8\/339\/950ac83392b733fd47acbfc2326773ad.png\" width=\"1710\" height=\"338\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/950\/ac8\/339\/950ac83392b733fd47acbfc2326773ad.png\"\/><\/figure>\n<p>\u0410 \u0441\u043e\u043b\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 &#171;\u043f\u0440\u043e\u043b\u0435\u0442\u0430\u0435\u0442&#187; \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u0430\u043a Transactional, \u0432 \u0430\u0441\u043f\u0435\u043a\u0442\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0443\u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0438 \u0435\u0435 \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u044c.<br \/>\u0415\u0441\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 noRollbackFor, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u043f\u0430\u043d\u0430\u0446\u0435\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u0437 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043b\u0435\u0442\u0430\u0442\u044c \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<br \/>\u0421\u043f\u043e\u0441\u043e\u0431, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0431\u043e\u0447\u0438\u0439, \u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043e \u0437\u043d\u0430\u043d\u0438\u0435\u043c \u0434\u0435\u043b\u0430, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0447\u0442\u043e \u044d\u0442\u0438\u043c \u043a\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0435\u0442\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b.<br \/>\u0422\u0430\u043a \u0436\u0435, \u043d\u0430\u043b\u0438\u0447\u0438\u0435 noRollbackFor \u0441\u0432\u0438\u0434\u0435\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0443\u0435\u0442 \u043e \u0440\u0430\u0437\u043c\u044b\u0442\u0438\u0438 \u0437\u043e\u043d \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u043b\u0438\u0431\u043e \u043e \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c try\/catch \u0442\u0430\u043c \u0433\u0434\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u043c\u0435\u0441\u0442\u0435\u043d if\/else.<\/p>\n<p><strong>\u041a\u0435\u0439\u0441 5: Transactional \u0438 Controller<\/strong><br \/>\u0422\u0430\u043a \u0436\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0438\u0436\u0443 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434<\/p>\n<pre><code class=\"java\">package org.example.first;   import lombok.RequiredArgsConstructor; import org.example.data.ExampleEntity; import org.example.data.ExampleEntityRepository; import org.springframework.stereotype.Controller; import org.springframework.ui.Model;  public class ExampleController {      @RestController     @RequiredArgsConstructor     public static class ExampleRestController {         private final ExampleEntityRepository exampleEntityRepository;          @GetMapping         public Iterable&lt;ExampleEntity> getAll() {             return exampleEntityRepository.findAll();         }     }      @Controller     @RequiredArgsConstructor     public static class ExampleMvcController {         private final ExampleEntityRepository exampleEntityRepository;          @GetMapping         public String getAll(Model model) {             model.addAttribute(exampleEntityRepository.findAll());             return \"page.html\";         }     } } <\/code><\/pre>\n<p>\u041d\u0435 \u0431\u0443\u0434\u0443 \u0440\u0430\u0441\u043f\u0438\u043d\u0430\u0442\u044c\u0441\u044f \u043e \u0442\u043e\u043c, \u043d\u0430 \u0441\u043a\u043e\u043b\u044c \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0442\u044c entity \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432, \u0443\u043f\u043e\u043c\u044f\u043d\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u043e\u0432\u0438\u0442\u044c <em>LazyInitializationException<\/em>.<br \/>\u0416\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b persistenceContext \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439.<\/p>\n<p><strong>\u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u043b\u0430\u0441\u044c<\/strong> &#8212; <strong>\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0441\u043e\u0437\u0434\u0430\u043b\u0441\u044f<\/strong>, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0441\u0432\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 (\u043f\u0440\u0438\u0432\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c).<\/p>\n<p><strong>\u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0437\u0430\u043a\u0440\u044b\u043b\u0430\u0441\u044c<\/strong> &#8212; <strong>\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u043e\u043c\u0435\u0440 <\/strong>\u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u0435\u0441\u0441\u0438\u0435\u0439 \u0445\u0438\u0431\u0435\u0440\u0430.<strong><br \/><\/strong>\u0418 \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0438) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 (\u0432 \u043d\u0435\u0434\u0440\u0430\u0445 DispatcherServlet), \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u0443 \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 entity \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432 <s>\u0442\u044b\u043a\u0432\u0443<\/s> \u043e\u0431\u044b\u0447\u043d\u044b\u0435 POJO, \u0430 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u043f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0438 \u043b\u0435\u043d\u0438\u0432\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c.<\/p>\n<p><strong>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0411\u041b\u0418\u0426<\/strong><\/p>\n<p>1. JPA \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 propagation NESTED (\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u043a\u0430\u043a\u043e\u0439 \u0433\u0435\u043c\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c safePoint \u0434\u043b\u044f persistenceContext \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0447\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 entity \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0433\u0434\u0435 \u0443\u0433\u043e\u0434\u043d\u043e)<br \/>2. \u043d\u0443\u0436\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f propagation REQUIRES_NEW, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043c\u0430\u0442\u0447\u0430\u0441\u0442\u0438 \u0438 \u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0433\u0435\u043d\u0438\u0442\u0430\u043b\u0438\u0438 (\u043f\u0440\u0438\u0432\u0435\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0438\u0437\u043e\u043b\u044f\u0446\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u0438 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445)<br \/>3. \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c lombok \u0438 hibernate \u0442\u0430\u043a \u0436\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f (\u043f\u0440\u0438\u0432\u0435\u0442 StackOverflow \u0438 LazyInit)<br \/>4. \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 save() \u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0435\u0441\u0442\u044c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 entity, \u043d\u043e \u043d\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u043e\u0442 \u0436 \u0441\u0430\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041c\u0430\u0433\u0438\u0438 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0441\u0435 \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 (\u0430 \u0447\u0430\u0441\u0442\u043e \u0434\u0430\u0436\u0435 \u0447\u0430\u0441\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438).<br \/>\u041d\u0443\u0436\u043d\u043e \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0438 \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430 \u0441\u043b\u043e\u0432\u043e, \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\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\/824936\/\"> https:\/\/habr.com\/ru\/articles\/824936\/<\/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<details class=\"spoiler\">\n<summary>\u043a\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u0442\u0430\u043a\u043e\u0439 \u0430\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0421\u0442\u0430\u0440\u0430\u043a\u043e\u0436\u0435\u0432 \u0414\u0435\u043d\u0438\u0441, <br \/>\u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u044f\u0432\u043b\u044f\u044e\u0441\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0418\u0422 \u043b\u0438\u0434\u043e\u043c \u043a\u0440\u043e\u0441\u0441\u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0444\u0438\u043d\u0442\u0435\u0445\u0435 (\u0432 \u0447\u0438\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 7 back-end \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432),<br \/>\u0438\u043c\u0435\u044e 5 \u043b\u0435\u0442 \u0441\u0442\u0430\u0436\u0430 \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 (java back-end),<br \/>\u0417\u0430 \u043f\u043b\u0435\u0447\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u043e\u043f\u044b\u0442 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0440\u0435\u0432\u043d\u0438\u043c \u043b\u0435\u0433\u0430\u0441\u0438, \u0442\u0430\u043a \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u044b &#171;\u0441 \u043d\u0443\u043b\u044f&#187;.<br \/>(\u0432\u044b\u0441\u0442\u0440\u0430\u0434\u0430\u0442\u044c \u0443\u0441\u043f\u0435\u043b \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e: \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430, \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438, \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u0434)<\/p>\n<\/div>\n<\/details>\n<h2>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f?<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0441\u0442\u043e\u043b\u043a\u043d\u0435\u0442\u0441\u044f \u043b\u044e\u0431\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c spring-data-jpa.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0447\u0435\u0440\u043f\u044b\u0432\u0430\u044e\u0449\u0438\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u043c \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0442\u0435\u0445, \u043a\u0442\u043e \u0445\u043e\u0442\u044c \u0440\u0430\u0437 \u0441\u0442\u0430\u0432\u0438\u043b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e Transactional \u0432 spring \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u0438\u0441\u043a\u0440\u0435\u043d\u043d\u0435 \u0443\u0434\u0438\u0432\u043b\u044f\u043b\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u043d\u0435 \u0441\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u044e \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0443\u0440\u043e\u0432\u043d\u044f middle \u0438 \u0432\u044b\u0448\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u044d\u0442\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c \u0432\u043b\u0430\u0434\u0435\u0442\u044c.<\/p>\n<h2>\u041c\u0430\u0433\u0438\u0438 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442<\/h2>\n<p>\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c \u0432 \u043a\u043e\u0434\u0435 \u043a\u0430\u043a \u043a \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u0430\u043a\u043b\u0438\u043d\u0430\u043d\u0438\u044f\u043c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u0430\u0436\u0435 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u044f\u0441\u044c \u043e \u0442\u043e\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u0438 &#171;\u0437\u0430\u043a\u043b\u0438\u043d\u0430\u043d\u0438\u044f&#187; \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442.<\/p>\n<p>\u041d\u0435 \u043c\u043e\u0433\u0443 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0442\u044c \u043d\u0430 100% \u0447\u0442\u043e \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u041d\u0430\u0440\u043d\u0438\u0438, \u043d\u043e \u0441\u043e \u0432\u0441\u0435\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u044f\u0432\u043b\u044f\u044e, \u0447\u0442\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u0430\u0433\u0438\u0438 \u0442\u043e\u0447\u043d\u043e \u043d\u0435\u0442, \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u0442\u043e-\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a, \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u043f\u0438\u0441\u0430\u043d.<\/p>\n<p>\u041a\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 (\u0438\u043b\u0438 \u043d\u0435 \u043c\u044b), \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0447\u0442\u043e \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435 \u0442\u0430\u043a, \u0437\u043d\u0430\u0447\u0438\u0442 \u043c\u044b \u0447\u0442\u043e-\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u043c\/\u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c.<\/p>\n<details class=\"spoiler\">\n<summary>\u043e\u0433\u043e\u0432\u043e\u0440\u043e\u0447\u043a\u0430<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0415\u0441\u0442\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0444\u0430\u043a\u0442\u043e\u0440\u044b \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0435 \u0442\u0430\u043a \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/habr.com\/ru\/articles\/401681\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0441\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043b\u0443\u0447\u0438<\/a>, \u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0447\u044c.<br \/>\u0422\u0430\u043a \u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442\u044b \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0440\u0443\u0433\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f (\u043c\u043e\u0436\u0435\u0442 \u0438 \u043f\u0440\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0448\u0443 \u043f\u043e\u0437\u0436\u0435).<\/p>\n<\/div>\n<\/details>\n<h2>\u041a \u0434\u0435\u043b\u0443<\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432.<\/p>\n<details class=\"spoiler\">\n<summary>pom<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"xml\">    &lt;parent>         &lt;groupId>org.springframework.boot&lt;\/groupId>         &lt;artifactId>spring-boot-starter-parent&lt;\/artifactId>         &lt;version>2.3.5.RELEASE&lt;\/version>         &lt;relativePath\/>     &lt;\/parent>      &lt;groupId>org.example&lt;\/groupId>     &lt;artifactId>transactional-demo&lt;\/artifactId>     &lt;version>1.0-SNAPSHOT&lt;\/version>      &lt;properties>         &lt;maven.compiler.source>11&lt;\/maven.compiler.source>         &lt;maven.compiler.target>11&lt;\/maven.compiler.target>         &lt;project.build.sourceEncoding>UTF-8&lt;\/project.build.sourceEncoding>     &lt;\/properties>      &lt;dependencies>         &lt;dependency>             &lt;groupId>org.springframework.boot&lt;\/groupId>             &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId>         &lt;\/dependency>         &lt;dependency>             &lt;groupId>org.postgresql&lt;\/groupId>             &lt;artifactId>postgresql&lt;\/artifactId>             &lt;optional>true&lt;\/optional>         &lt;\/dependency>         &lt;dependency>             &lt;groupId>org.projectlombok&lt;\/groupId>             &lt;artifactId>lombok&lt;\/artifactId>             &lt;optional>true&lt;\/optional>         &lt;\/dependency>          &lt;dependency>             &lt;groupId>org.springframework.boot&lt;\/groupId>             &lt;artifactId>spring-boot-starter-test&lt;\/artifactId>         &lt;\/dependency>     &lt;\/dependencies><\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>main<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">package org.example;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class Application {     public static void main(String[] args) {         SpringApplication.run(Application.class);     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">package org.example.data;  import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;  import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import java.util.UUID;  @Data @Entity @Builder @AllArgsConstructor @NoArgsConstructor @Table(name = \"example\") public class ExampleEntity {     @Id     @GeneratedValue     private UUID id;      private String value; } <\/code><\/pre>\n<pre><code class=\"java\">package org.example.data;  import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository;  import java.util.Optional; import java.util.UUID;  @Repository public interface ExampleEntityRepository extends CrudRepository&lt;ExampleEntity, UUID> {          Optional&lt;ExampleEntity> findByValue(String value); } <\/code><\/pre>\n<pre><code class=\"java\">package org.example.data;  import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service;  import java.util.UUID;  @Service @RequiredArgsConstructor public class ExampleService {      private final ExampleEntityRepository exampleEntityRepository;      public void save(String value) {         exampleEntityRepository.save(ExampleEntity.builder()                 .value(value)                 .build());     }          public ExampleEntity get(UUID id) {         return exampleEntityRepository.findById(id)                 .orElseThrow();     }  } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p><strong>\u041a\u0435\u0439\u0441 1: \u043a\u043b\u0430\u0441\u0441\u0438\u043a\u0430<\/strong><\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u043b \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438.<br \/>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 saveAll() ?<\/p>\n<pre><code class=\"java\">package org.example.first;  import lombok.RequiredArgsConstructor; import org.example.data.ExampleService; import org.springframework.stereotype.Service;  import javax.transaction.Transactional;  @Service @RequiredArgsConstructor public class FirstCase {          private final ExampleService exampleService;          public void saveAll() {         saveFirst();         saveSecond();     }          @Transactional     public void saveFirst() {         exampleService.save(\"first\");     }          @Transactional     public void saveSecond() {         exampleService.save(\"second\");     } } <\/code><\/pre>\n<p>\u041b\u044e\u0431\u043e\u0439 \u0437\u0434\u0440\u0430\u0432\u043e\u043c\u044b\u0441\u043b\u044f\u0449\u0438\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0430\u0441\u043f\u0435\u043a\u0442\u044b \u0432 \u0441\u043f\u0440\u0438\u043d\u0433\u0435, \u0434\u043e\u0433\u0430\u0434\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442<\/p>\n<details class=\"spoiler\">\n<summary>\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0422\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u0438 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>\u0412 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0435 (\u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043f\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u043a\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u043b\u0438 \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c).<br \/>\u041a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">package org.example.decorator;  import org.springframework.stereotype.Service;  public class DecoratorExample {      \/**      * \u041d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438      *\/     @Service     public static class SomeService {          public Object doSome() {             return new Object();         }     }      \/**      * \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \"\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\" \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435      *\/     public static class SomeServiceDecorator extends SomeService {          private final SomeService originService;          public SomeServiceDecorator(SomeService originService) {             this.originService = originService;         }          @Override         public Object doSome() {             before();             try {                 Object returnValue = originService.doSome(); \/\/ \u0432\u044b\u0437\u043e\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430                 after(returnValue);                 return returnValue;             } catch (Exception e) {                 return handle(e);             }         }          private void before() {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438         }          private void after(Object returnValue) {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043a\u043e\u043c\u043c\u0438\u0442\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438         }          private Object handle(Exception e) {             \/\/ \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0442\u043a\u0430\u0442\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438             throw new RuntimeException(e);         }     } } <\/code><\/pre>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b &#171;\u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438&#187; \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438 \u0442\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430, \u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b, \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0434\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/p>\n<pre><code class=\"java\">package org.example.decorator;  import org.springframework.stereotype.Service;  public class DelegateDecoratorExample {      \/**      * \u041d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438      *\/     @Service     public static class SomeService {          public Object doSome() {             return new Object();         }     }      \/**      * \u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u043c\u0438 \"\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\" \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435      *\/     public static class SomeDelegateServiceDecorator extends SomeService {          private final SomeService originService;          public SomeDelegateServiceDecorator(SomeService originService) {             this.originService = originService;         }          @Override         public Object doSome() {             return originService.doSome(); \/\/ \u0432\u044b\u0437\u043e\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430         }     } } <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, ExampleService \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0435\u0440\u043d\u0443\u0442 \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0445\u043e\u0434\u0443 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 (\u043b\u0438\u0431\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0442\u0435 \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0447\u0442\u043e \u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043d\u0438\u043a\u0442\u043e \u0438\u0437 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u044f\u0432\u043d\u043e \u043d\u0435 \u043f\u043e\u043b\u0435\u0437\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c (<a href=\"https:\/\/www.youtube.com\/watch?v=BmBr5diz8WA\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u0430\u0437\u0431\u043e\u0440 \u043e\u0442 \u0433\u0443\u0440\u0443<\/a>).<br \/>\u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432<\/p>\n<figure class=\"full-width\"><\/figure>\n<\/p>\n<\/div>\n<\/details>\n<p><strong>\u041a\u0435\u0439\u0441 2: JPA \u0438 persistenceContext<\/strong><\/p>\n<p>\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u0440\u0430\u0431\u043e\u0442\u0430\u044f \u0441 JPA \u0432 \u043b\u0438\u0446\u0435 hibernate, \u043f\u0440\u0438\u043a\u0440\u044b\u0442\u043e\u0433\u043e spring-data, \u0434\u0430\u0436\u0435 \u043d\u0435 \u0434\u043e\u0433\u0430\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0438 persistenceContext (\u043b\u0438\u0431\u043e \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043b\u044b\u0448\u0430\u043b\u0438, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0435\u0441\u0442\u044c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u044f).<\/p>\n<p>\u0414\u0435\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u0431\u043e\u0440 \u0435\u0433\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 (\u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043d\u0438\u0433\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 &#171;Java Persistence API \u0438 Hibernate&#187;).<br \/>\u041f\u0440\u043e\u0431\u0435\u0433\u0443\u0441\u044c \u043f\u043e \u043f\u043e\u0432\u0435\u0440\u0445\u043d\u043e\u0441\u0442\u0438:<\/p>\n<p><strong>persistenceContext<\/strong> &#8212; \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u043a\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u044d\u0448, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u0441\u0435 \u0442\u043e, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0438\u0437 \u0411\u0414 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0439 \u0441\u0430\u043c\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0438\u043c\u0435\u0435\u0442 2 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<br \/>1. \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0441\u043e\u0431\u043e\u0439 \u0442\u043e, \u0447\u0442\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u0438\u0434\u043d\u043e \u0432 \u0411\u0414<br \/>2. entity &#8212; \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0437 \u043f1 (\u043b\u0438\u0431\u043e \u043d\u043e\u0432\u044b\u0435 &#8212; \u0440\u0443\u043a\u0430\u043c\u0438 \u0432 \u043a\u043e\u0434\u0435), \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u043d\u0438\u043c\u0438 \u043c\u044b \u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0435.<\/p>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u043e <strong>flush \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/strong>, \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u0444\u0444 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c \u0411\u0414 (\u043f1) \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438 \u043d\u0430\u0448\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b (\u043f2), \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 \u0434\u0438\u0444\u0444 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0411\u0414 \u0432 \u0432\u0438\u0434\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441 \u043a\u043e\u0433\u0434\u0430?<br \/>1. \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043c\u0438\u0442\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<br \/>2. \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043a\u043e\u0433\u0434\u0430 \u0445\u0438\u0431\u0435\u0440 \u0441\u043e\u0447\u0442\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0434 \u043a\u0430\u043a\u0438\u043c \u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u043c \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u043c \u0432 \u0411\u0414).<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 save \u0443<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-380101","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/380101","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=380101"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/380101\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=380101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=380101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=380101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}