package com.vladmihalcea.hibernate.masterclass.laboratory.mapping;

import com.vladmihalcea.hibernate.masterclass.laboratory.util.AbstractTest;
import org.junit.Before;
import org.junit.Test;

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

/**
 * <code>IndexOverrideTest</code> -
 *
 * @author Vlad Mihalcea
 */
public class IndexOverrideTest extends AbstractTest {

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

    @Before
    public void init() {
        super.init();
        doInTransaction(session -> {
            ChildY childy = new ChildY();
            ChildZ childz = new ChildZ();
            session.persist(childy);
            session.persist(childz);
        });
    }

    @Test
    public void testIndex() {
        doInTransaction(session -> {
            List<Base> bases = session.createQuery("from Base").list();
            bases.stream().forEach(base -> base.setX(UUID.randomUUID().toString()));
        });
    }

    @Entity(name = "Base")
    @Table(name="Base")
    @Inheritance(strategy = InheritanceType.JOINED)
    public static abstract class Base {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;

        private String x;

        public Long getId() {
            return id;
        }

        @Access(AccessType.PROPERTY)
        public String getX() {
            return x;
        }

        public void setX(String x) {
            this.x = x;
        }
    }

    @Entity(name = "ChildY")
    @Table(name="ChildY")
    @DiscriminatorValue("Y")
    public static class ChildY extends Base {

        private String y;

        @Override
        @org.hibernate.annotations.Index(name = "xy")
        @Access(AccessType.PROPERTY)
        @Column(insertable = false, updatable = false)
        public String getX() {
            return super.getX();
        }
    }

    @Entity(name = "ChildZ")
    @Table(name="ChildZ")
    @Inheritance(strategy = InheritanceType.JOINED)
    @DiscriminatorValue("Z")
    public static class ChildZ extends Base {

        private String z;

        @Override
        @org.hibernate.annotations.Index(name = "xz")
        @Access(AccessType.PROPERTY)
        @Column(insertable = false, updatable = false)
        public String getX() {
            return super.getX();
        }
    }
}

@@ -1341,39 +1341,60 @@
 ng id;%0A%0A        
-priva
+@Transient%0A        protec
 te
+d
  String x;%0A%0A    
@@ -1457,45 +1457,8 @@
  %7D%0A%0A
-        @Access(AccessType.PROPERTY)%0A
     
@@ -1866,63 +1866,8 @@
 TY)%0A
-        @Column(insertable = false, updatable = false)%0A
     
@@ -1908,36 +1908,25 @@
      return 
-super.getX()
+x
 ;%0A        %7D%0A
@@ -2255,63 +2255,8 @@
 TY)%0A
-        @Column(insertable = false, updatable = false)%0A
     
@@ -2305,20 +2305,9 @@
 urn 
-super.getX()
+x
 ;%0A  

Make the base one transient