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

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

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

import static org.junit.Assert.assertEquals;

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

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

    @Override
    protected void afterInit() {
        doInJPA(entityManager -> {
            Post post = new Post();
            post.setId(1L);
            post.setTitle("High-Performance Java Persistence");

            entityManager.persist(post);
        });
    }

    @Test
    public void testFind() {
        doInJPA(entityManager -> {
            PostComment comment = new PostComment();
            comment.setReview("Just awesome!");

            Post post = entityManager.find(Post.class, 1L);
            comment.setPost(post);

            entityManager.persist(comment);
        });
    }

    @Test
    public void testGetReference() {
        doInJPA(entityManager -> {
            PostComment comment = new PostComment();
            comment.setReview("Just awesome!");

            Post post = entityManager.getReference(Post.class, 1L);
            comment.setPost(post);

            entityManager.persist(comment);
        });
    }
    
    @Entity(name = "Post")
    @Table(name = "post")
    public static class Post {

        @Id
        private Long id;

        private String title;

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

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

        @Id
        @GeneratedValue
        private Long id;

        private String review;

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

        public PostComment() {}

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

        public Long getId() {
            return id;
        }

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

        public String getReview() {
            return review;
        }

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

        public Post getPost() {
            return post;
        }

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

@@ -105,16 +105,66 @@
 ctTest;%0A
+import org.hibernate.LazyInitializationException;%0A
 import o
@@ -174,24 +174,24 @@
 junit.Test;%0A
-
 %0Aimport java
@@ -275,16 +275,53 @@
 tEquals;
+%0Aimport static org.junit.Assert.fail;
 %0A%0A/**%0A *
@@ -1154,32 +1154,224 @@
 nt);%0A        %7D);
+%0A%0A        Post post = doInJPA(entityManager -%3E %7B%0A            return entityManager.find(Post.class, 1L);%0A        %7D);%0A%0A        assertEquals(%22High-Performance Java Persistence%22, post.getTitle());
 %0A    %7D%0A%0A    @Tes
@@ -1686,32 +1686,32 @@
 rsist(comment);%0A
-
         %7D);%0A    
@@ -1701,24 +1701,362 @@
 %0A        %7D);
+%0A%0A        Post post = doInJPA(entityManager -%3E %7B%0A            return entityManager.getReference(Post.class, 1L);%0A        %7D);%0A%0A        try %7B%0A            post.getTitle();%0A%0A            fail(%22Should throw LazyInitializationException%22);%0A        %7D catch (LazyInitializationException e) %7B%0A            LOGGER.info(%22Failure expected%22, e);%0A        %7D
 %0A    %7D%0A    %0A

Add more examples to he Find vs. GetReference test class