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

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.junit.Before;
import org.junit.Test;

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

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

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

    @Override
    protected Properties properties() {
        Properties properties = super.properties();
        properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
        properties.put("hibernate.generate_statistics", Boolean.TRUE.toString());
        properties.put("hibernate.cache.use_query_cache", Boolean.TRUE.toString());
        return properties;
    }

    @Before
    public void init() {
        super.init();
        doInJPA(entityManager -> {
            Post post1 = new Post();
            post1.setId(1L);
            post1.setTitle("High-Performance Java Persistence");

            entityManager.persist(post1);

            Announcement post2 = new Announcement();
            post2.setId(2L);
            post2.setTitle("High-Performance Hibernate");

            entityManager.persist(post2);
        });
    }

    @Test
    public void testEntityLoad() {

        doInJPA(entityManager -> {
            LOGGER.info("First access");
            entityManager.find(Post.class, 1L);
            entityManager.find(Announcement.class, 2L);
        });

        doInJPA(entityManager -> {
            LOGGER.info("Second access");
            entityManager.find(Post.class, 1L);
            entityManager.find(Announcement.class, 2L);
        });
    }

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

        @Id
        private Long id;

        private String title;

        @Version
        private int version;

        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 = "Announcement")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public static class Announcement extends Post {

        @Temporal(TemporalType.DATE)
        private Date validUntil;

        public Date getValidUntil() {
            return validUntil;
        }

        public void setValidUntil(Date validUntil) {
            this.validUntil = validUntil;
        }
    }
}

@@ -136,72 +136,9 @@
 ons.
-Cache;%0Aimport org.hibernate.annotations.CacheConcurrencyStrategy
+*
 ;%0Aim
@@ -208,24 +208,57 @@
 sistence.*;%0A
+import javax.persistence.Entity;%0A
 import java.
@@ -1871,62 +1871,328 @@
     
-%7D);%0A    %7D%0A%0A    @Entity(name = %22Post%22)%0A    @Inheritance
+    entityManager.find(Post.class, 2L);%0A        %7D);%0A        doInJPA(entityManager -%3E %7B%0A            LOGGER.info(%22Third access%22);%0A            entityManager.find(Post.class, 2L);%0A        %7D);%0A    %7D%0A%0A    @Entity(name = %22Post%22)%0A    @Inheritance%0A    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
 %0A   
@@ -2679,64 +2679,8 @@
 t%22)%0A
-    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)%0A
     

Add inheritance cache test update