package com.vladmihalcea.book.hpjp.hibernate.association;

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.junit.Test;

import javax.persistence.*;
import java.nio.ByteBuffer;
import java.util.*;

/**
 * @author Vlad Mihalcea
 */
public class UnidirectionalOneToManySetTest extends AbstractTest {

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[] {
            Post.class,
            PostComment.class,
        };
    }

    @Test
    public void testLifecycle() {
        doInJPA(entityManager -> {
            Post post = new Post("First post");

            post.getComments().add(new PostComment("My first review"));
            post.getComments().add(new PostComment("My second review"));
            post.getComments().add(new PostComment("My third review"));

            entityManager.persist(post);
            entityManager.flush();

            for(PostComment comment: new ArrayList<>(post.getComments())) {
                post.getComments().remove(comment);
            }
        });
    }

    @Entity(name = "Post")
    @Table(name = "post")
    public static class Post {

        @Id
        @GeneratedValue
        private Long id;

        private String title;

        public Post() {}

        public Post(String title) {
            this.title = title;
        }

        @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
        private Set<PostComment> comments = new HashSet<>();

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public Set<PostComment> getComments() {
            return comments;
        }
    }

    @Entity(name = "PostComment")
    @Table(name = "post_comment")
    public static class PostComment {

        @Id
        @GeneratedValue
        private Long id;

        private String slug;

        private String review;

        public PostComment() {
            byte[] bytes = new byte[8];
            ByteBuffer.wrap(bytes).putDouble(Math.random());
            slug = Base64.getEncoder().encodeToString(bytes);
        }

        public PostComment(String review) {
            this();
            this.review = review;
        }

        public String getSlug() {
            return slug;
        }

        public void setSlug(String slug) {
            this.slug = slug;
        }

        public String getReview() {
            return review;
        }

        public void setReview(String review) {
            this.review = review;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            PostComment comment = (PostComment) o;
            return Objects.equals(slug, comment.slug);
        }

        @Override
        public int hashCode() {
            return Objects.hash(slug);
        }
    }
}

@@ -863,177 +863,406 @@
     
-    entityManager.flush();%0A%0A            for(PostComment comment: new ArrayList%3C%3E(post.getComments())) %7B%0A                post.getComments().remove(comment);%0A            %7D
+%7D);%0A%0A        doInJPA(entityManager -%3E %7B%0A            Post post = entityManager.createQuery(%0A                %22select p %22 +%0A                %22from Post p %22 +%0A                %22join fetch p.comments %22 +%0A                %22where p.id = :postId%22, Post.class)%0A            .setParameter(%22postId%22, 1L)%0A            .getSingleResult();%0A%0A            post.getComments().remove(post.getComments().iterator().next());
 %0A   
@@ -2255,38 +2255,8 @@
 d;%0A%0A
-        private String slug;%0A%0A
     
@@ -2318,171 +2318,8 @@
 ) %7B%0A
-            byte%5B%5D bytes = new byte%5B8%5D;%0A            ByteBuffer.wrap(bytes).putDouble(Math.random());%0A            slug = Base64.getEncoder().encodeToString(bytes);%0A
     
@@ -2373,28 +2373,8 @@
 ) %7B%0A
-            this();%0A
     
@@ -2418,162 +2418,8 @@
  %7D%0A%0A
-        public String getSlug() %7B%0A            return slug;%0A        %7D%0A%0A        public void setSlug(String slug) %7B%0A            this.slug = slug;%0A        %7D%0A%0A
     
@@ -2700,166 +2700,114 @@
 if (
-o == null %7C%7C getClass() != o.getClass()) return false;%0A            PostComment comment = (PostComment) o;%0A            return Objects.equals(slug, comment.slug
+!(o instanceof PostComment)) return false;%0A            return id != null && id.equals(((PostComment) o).id
 );%0A 
@@ -2889,26 +2889,10 @@
 urn 
-Objects.hash(slug)
+31
 ;%0A  

Update test