Skip to content

Commit

Permalink
Add support for passing the mapping Class to Json Types #133
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Oct 8, 2019
1 parent b0112ed commit 797dda2
Show file tree
Hide file tree
Showing 21 changed files with 501 additions and 16 deletions.
Expand Up @@ -32,6 +32,13 @@ public JsonBinaryType() {
);
}

public JsonBinaryType(Type javaType) {
super(
JsonBinarySqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonBinaryType(Configuration configuration) {
super(
JsonBinarySqlTypeDescriptor.INSTANCE,
Expand Down
Expand Up @@ -31,6 +31,13 @@ public JsonBlobType() {
);
}

public JsonBlobType(Type javaType) {
super(
org.hibernate.type.descriptor.sql.BlobTypeDescriptor.DEFAULT,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonBlobType(Configuration configuration) {
super(
org.hibernate.type.descriptor.sql.BlobTypeDescriptor.DEFAULT,
Expand Down
Expand Up @@ -37,6 +37,13 @@ public JsonStringType() {
);
}

public JsonStringType(Type javaType) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonStringType(Configuration configuration) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
Expand Down
Expand Up @@ -44,6 +44,11 @@ protected Object deepCopyNotNull(Object value) {
});
}

public JsonTypeDescriptor(Type type) {
this();
this.type = type;
}

public JsonTypeDescriptor(final ObjectMapperWrapper objectMapperWrapper) {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
Expand All @@ -55,13 +60,7 @@ protected Object deepCopyNotNull(Object value) {
}

public JsonTypeDescriptor(final ObjectMapperWrapper objectMapperWrapper, Type type) {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
protected Object deepCopyNotNull(Object value) {
return objectMapperWrapper.clone(value);
}
});
this.objectMapperWrapper = objectMapperWrapper;
this(objectMapperWrapper);
this.type = type;
}

Expand Down
@@ -1,15 +1,17 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.util.AbstractOracleIntegrationTest;
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction;
import org.hibernate.Session;
import org.hibernate.SQLQuery;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.junit.Test;

import javax.persistence.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -74,6 +76,27 @@ public Void apply(EntityManager entityManager) {
return null;
}
});

doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(SQLQuery.class)
.addScalar("properties", new JsonStringType(JsonNode.class))
.uniqueResult();

assertEquals("High-Performance Java Persistence", properties.get("title").asText());

return null;
}
});
}

@Entity(name = "Book")
Expand Down
@@ -0,0 +1,133 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.util.AbstractPostgreSQLIntegrationTest;
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.junit.Test;

import javax.persistence.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

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

@Test
public void test() {
doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.setProperties(
"{" +
" \"title\": \"High-Performance Java Persistence\"," +
" \"author\": \"Vlad Mihalcea\"," +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99" +
"}"
)
);

return null;
}
});

doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
Book book = (Book) entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");

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

assertTrue(book.getProperties().contains("\"price\": 44.99"));

book.setProperties(
"{" +
" \"title\": \"High-Performance Java Persistence\"," +
" \"author\": \"Vlad Mihalcea\"," +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99," +
" \"url\": \"https://www.amazon.com/High-Performance-Java-Persistence-Vlad-Mihalcea/dp/973022823X/\"" +
"}"
);

return null;
}
});

doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(SQLQuery.class)
.addScalar("properties", new JsonBinaryType(JsonNode.class))
.uniqueResult();

assertEquals("High-Performance Java Persistence", properties.get("title").asText());

return null;
}
});
}

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

@Id
@GeneratedValue
private Long id;

@NaturalId
private String isbn;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private String properties;

public String getIsbn() {
return isbn;
}

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

public String getProperties() {
return properties;
}

public Book setProperties(String properties) {
this.properties = properties;
return this;
}
}
}
@@ -1,7 +1,9 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.util.AbstractSQLServerIntegrationTest;
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
Expand All @@ -10,6 +12,7 @@

import javax.persistence.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -74,6 +77,27 @@ public Void apply(EntityManager entityManager) {
return null;
}
});

doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(SQLQuery.class)
.addScalar("properties", new JsonStringType(JsonNode.class))
.uniqueResult();

assertEquals("High-Performance Java Persistence", properties.get("title").asText());

return null;
}
});
}

@Entity(name = "Book")
Expand Down
Expand Up @@ -32,6 +32,13 @@ public JsonBinaryType() {
);
}

public JsonBinaryType(Type javaType) {
super(
JsonBinarySqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonBinaryType(Configuration configuration) {
super(
JsonBinarySqlTypeDescriptor.INSTANCE,
Expand Down
Expand Up @@ -31,6 +31,13 @@ public JsonBlobType() {
);
}

public JsonBlobType(Type javaType) {
super(
org.hibernate.type.descriptor.sql.BlobTypeDescriptor.DEFAULT,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonBlobType(Configuration configuration) {
super(
org.hibernate.type.descriptor.sql.BlobTypeDescriptor.DEFAULT,
Expand Down
Expand Up @@ -37,6 +37,13 @@ public JsonStringType() {
);
}

public JsonStringType(Type javaType) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}

public JsonStringType(Configuration configuration) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
Expand Down
Expand Up @@ -44,6 +44,11 @@ protected Object deepCopyNotNull(Object value) {
});
}

public JsonTypeDescriptor(Type type) {
this();
this.type = type;
}

public JsonTypeDescriptor(final ObjectMapperWrapper objectMapperWrapper) {
super(Object.class, new MutableMutabilityPlan<Object>() {
@Override
Expand Down
@@ -1,7 +1,9 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.util.AbstractOracleIntegrationTest;
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
Expand All @@ -10,6 +12,7 @@

import javax.persistence.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -70,6 +73,27 @@ public Void apply(EntityManager entityManager) {
return null;
}
});

doInJPA(new JPATransactionFunction<Void>() {
@Override
public Void apply(EntityManager entityManager) {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(SQLQuery.class)
.addScalar("properties", new JsonStringType(JsonNode.class))
.uniqueResult();

assertEquals("High-Performance Java Persistence", properties.get("title").asText());

return null;
}
});
}

@Entity(name = "Book")
Expand Down

0 comments on commit 797dda2

Please sign in to comment.