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

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

import javax.persistence.*;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertNotNull;

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

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

    @Override
    protected void afterInit() {
        doInJPA(entityManager -> {
            for (int i = 0; i < 100; i++) {
                Post post = new Post(String.format("Post nr. %d", i));
                post.setDetails(new PostDetails("Excellent!"));

                entityManager.persist(post);
            }
        });
    }

    @Test
    public void testNPlusOne() {
        doInJPA(entityManager -> {
            List<Post> posts = entityManager.createQuery("""
                select p
                from Post p
                where p.title like 'Post nr.%'
                """, Post.class)
            .getResultList();
        });
    }

    @Test
    public void testWithoutNPlusOne() {
        doInJPA(entityManager -> {
            List<PostSummary> posts = entityManager.createQuery("""
                select p
                from PostSummary p
                where p.title like 'Post nr.%'
                """, PostSummary.class)
            .getResultList();
        });
    }

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

        @Id
        @GeneratedValue
        private Long id;

        private String title;

        @OneToOne(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private PostDetails details;

        public Post() {}

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

        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 PostDetails getDetails() {
            return details;
        }

        public void setDetails(PostDetails details) {
            if (details == null) {
                if (this.details != null) {
                    this.details.setPost(null);
                }
            }
            else {
                details.setPost(this);
            }
            this.details = details;
        }
    }

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

        @Id
        @GeneratedValue
        private Long id;

        private String title;

        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;
        }
    }

    @Entity(name = "PostDetails")
    @Table(name = "post_details")
    public static class PostDetails {

        @Id
        private Long id;

        @Column(name = "created_on")
        private Date createdOn;

        @Column(name = "created_by")
        private String createdBy;

        @OneToOne(fetch = FetchType.LAZY)
        @MapsId
        private Post post;

        public PostDetails() {}

        public PostDetails(String createdBy) {
            createdOn = new Date();
            this.createdBy = createdBy;
        }

        public Long getId() {
            return id;
        }

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

        public Date getCreatedOn() {
            return createdOn;
        }

        public String getCreatedBy() {
            return createdBy;
        }

        public Post getPost() {
            return post;
        }

        public void setPost(Post post) {
            this.post = post;
        }
    }
}

@@ -90,25 +90,144 @@
 pjp.
-util.AbstractTest
+hibernate.logging.validator.sql.SQLStatementCountValidator;%0Aimport com.vladmihalcea.book.hpjp.util.AbstractTest;%0Aimport org.junit.Ignore
 ;%0Aim
@@ -319,24 +319,69 @@
 util.List;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import stati
@@ -817,14 +817,15 @@
 i = 
-0
+1
 ; i %3C
+=
  100
@@ -1049,32 +1049,44 @@
    %7D%0A%0A    @Test%0A
+    @Ignore%0A
     public void 
@@ -1101,32 +1101,96 @@
 sOne() %7B%0A       
+ SQLStatementCountValidator.reset();%0A%0A        List%3CPost%3E posts =
  doInJPA(entityM
@@ -1213,34 +1213,22 @@
         
-List%3CPost%3E posts =
+return
  entityM
@@ -1429,72 +1429,242 @@
 %7D);%0A
-    %7D%0A%0A    @Test%0A    public void testWithoutNPlusOne() %7B%0A       
+%0A        assertEquals(100, posts.size());%0A        SQLStatementCountValidator.assertSelectCount(1);%0A    %7D%0A%0A    @Test%0A    public void testWithoutNPlusOne() %7B%0A        SQLStatementCountValidator.reset();%0A%0A        List%3CPostSummary%3E posts =
  doI
@@ -1703,33 +1703,14 @@
     
-List%3CPostSummary%3E posts =
+return
  ent
@@ -1916,32 +1916,131 @@
 t();%0A        %7D);
+%0A%0A        assertEquals(100, posts.size());%0A        SQLStatementCountValidator.assertSelectCount(1);
 %0A    %7D%0A%0A    @Ent

Add SqlCountValidator on N+1 one-to-one use case