title: Hibernate @OneToMany relation cascade option not working
body: <p>I design simple 1:N schema , Account(1):AccountProfileImage(N).</p>
  <p>Below codes are entity codes.</p>
  <pre class="lang-java prettyprint-override"><code>// Account.java
  
  @Entity
  @Table(name = &quot;account&quot;)
  @Getter
  @NoArgsConstructor(access = AccessLevel.PROTECTED)
  @EntityListeners(AuditingEntityListener.class)
  public class Account {
  
      @GeneratedValue
      @Id
      @Column(name = &quot;id&quot;)
      private Long id;
  
      @Column(name = &quot;email&quot;, nullable = false)
      private String email;
  
      @Column(name = &quot;password&quot;, nullable = false)
      private String password;
  
      @Column(name = &quot;first_name&quot;, nullable = false)
      private String firstName;
  
      @Column(name = &quot;last_name&quot;, nullable = false)
      private String lastName;
  
      @CreatedDate
      @Column(name = &quot;created_at&quot;)
      private LocalDateTime createdAt;
  
      @OneToMany(mappedBy =&quot;account&quot;,cascade = CascadeType.ALL)
      private final List&lt;AccountProfileImage&gt; profileImages= new ArrayList&lt;&gt;();
      
      @Builder
      public Account(String email,String firstName,String lastName,String password){
          this.email=email;
          this.firstName=firstName;
          this.lastName=lastName;
          this.password=password;
      }
  }
  
  // AccountProfileImage.java
  
  @Entity
  @Table(name = &quot;account_profile_image&quot;)
  @NoArgsConstructor(access = AccessLevel.PROTECTED)
  @Getter
  @EntityListeners(AuditingEntityListener.class)
  public class AccountProfileImage {
      @Id
      @GeneratedValue
      private Long id;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = &quot;account_id&quot;)
      private Account account;
  
      @Column(name = &quot;image_url&quot;)
      private String imageURL;
  
      @CreatedDate
      @Column(name = &quot;created_at&quot;)
      private LocalDateTime createdAt;
  
      @Builder
      public AccountProfileImage (Account account,String imageURL){
          this.account=account;
          // this.account.addProfileImage(this);
          this.imageURL=imageURL;
      }
  }
  
  </code></pre>
  <p>and this is test code for AccountProfileRepository code.</p>
  <pre class="lang-java prettyprint-override"><code>@Test
      @Rollback(value = false)
      public void saveAccountProfileImageTest() throws Exception {
          // given
          Account account = Account.builder()
                  .email(&quot;user@email.com&quot;)
                  .firstName(&quot;user&quot;)
                  .lastName(&quot;user&quot;)
                  .password(&quot;1234&quot;)
                  .build();
  
  
          AccountProfileImage profileImage = AccountProfileImage.builder()
                  .account(account)
                  .imageURL(&quot;pathToURI&quot;)
                  .build();
  
  
          AccountProfileImage profileImage2 = AccountProfileImage.builder()
                  .account(account)
                  .imageURL(&quot;pathToURI2&quot;)
                  .build();
  
          accountRepository.save(account);
  
          
  
          // when
          List&lt;AccountProfileImage&gt; images = profileImageRepository.findAllByAccount_IdOrderByCreatedAtDesc(1L);
          // then
          // this assertion fail
          assertThat(images.size()).isEqualTo(2);
      }
  </code></pre>
  <p>What i want to expect find by List of images whose size is 2 because I add <code>CscadeType.ALL</code> in <code>Account</code> entity class and when creating <code>AccountProfileImage</code> object, I set account member variable in <code>AccountProfileImage</code> object.</p>
  <blockquote>
  <p>this.account=account;</p>
  </blockquote>
  <p>Did I something wrong?</p>
  <hr />
  <p>I add below method in Account entity and 2 lines at test code, then it works fine. Do i have to do this everytime? Is there exist another better approach or best practice?</p>
  <pre class="lang-java prettyprint-override"><code>// Account Entity
  public void addProfileImages(AccountProfileImage image){
          this.profileImages.add(image);
      }
  
  // test code
  account.addProfileImages(profileImage);
  account.addProfileImages(profileImage2);
  
  accountRepository.save(account);
  
  // when
  List&lt;AccountProfileImage&gt; images = profileImageRepository.findAllByAccount_IdOrderByCreatedAtDesc(1L);
  
  // then
  // this assertion pass
  assertThat(images.size()).isEqualTo(2);
  </code></pre>