package com.vladmihalcea.book.hpjp.hibernate.type.json;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.book.hpjp.util.AbstractPostgreSQLIntegrationTest;

/**
 * @author Vlad Mihalcea
 */
public class PostgreSQLJsonNodeBinaryTypeTest extends AbstractPostgreSQLIntegrationTest {

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

    @Test
    public void test() {

        doInJPA(entityManager -> {

            Book configuration = new Book();
            configuration.setIsbn( "978-9730228236" );
            configuration.setProperties(
                JacksonUtil.toJsonNode(
                    "{" +
                    "   \"title\": \"High-Performance Java Persistence\"," +
                    "   \"author\": \"Vlad Mihalcea\"," +
                    "   \"publisher\": \"Amazon\", " +
                    "   \"price\": 44.99" +
                    "}"
                )
            );

            entityManager.persist( configuration );
        });
        doInJPA(entityManager -> {
            Session session = entityManager.unwrap( Session.class );
            Book configuration = session
                .bySimpleNaturalId( Book.class )
                .load( "978-9730228236" );

            LOGGER.info( "Book details; {}", configuration.getProperties() );
        });
    }

    @Entity(name = "Book")
    @Table(name = "book")
    @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class)
    public static class Book {

        @Id
        @GeneratedValue
        private Long id;

        @NaturalId
        private String isbn;

        @Type( type = "jsonb-node" )
        @Column(columnDefinition = "jsonb")
        private JsonNode properties;

        public String getIsbn() {
            return isbn;
        }

        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }

        public JsonNode getProperties() {
            return properties;
        }

        public void setProperties(JsonNode properties) {
            this.properties = properties;
        }
    }
}

@@ -860,29 +860,20 @@
    Book 
-configuration
+book
  = new B
@@ -887,37 +887,28 @@
             
-configuration
+book
 .setIsbn( %229
@@ -937,29 +937,20 @@
         
-configuration
+book
 .setProp
@@ -1208,17 +1208,16 @@
 mazon%5C%22,
- 
 %22 +%0A    
@@ -1349,29 +1349,20 @@
 ersist( 
-configuration
+book
  );%0A    
@@ -1490,29 +1490,20 @@
    Book 
-configuration
+book
  = sessi
@@ -1640,44 +1640,551 @@
 ails
-;
+:
  %7B%7D%22, 
-configuration.getProperties()
+book.getProperties() );%0A%0A            book.setProperties(%0A                JacksonUtil.toJsonNode(%0A                    %22%7B%22 +%0A                    %22   %5C%22title%5C%22: %5C%22High-Performance Java Persistence%5C%22,%22 +%0A                    %22   %5C%22author%5C%22: %5C%22Vlad Mihalcea%5C%22,%22 +%0A                    %22   %5C%22publisher%5C%22: %5C%22Amazon%5C%22,%22 +%0A                    %22   %5C%22price%5C%22: 44.99,%22 +%0A                    %22   %5C%22url%5C%22: %5C%22https://www.amazon.com/High-Performance-Java-Persistence-Vlad-Mihalcea/dp/973022823X/%5C%22%22 +%0A                    %22%7D%22%0A                )%0A           
  );%0A

Update JSON test for update as well