Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for passing the mapping Class to Json Types #133
- Loading branch information
1 parent
b0112ed
commit 797dda2
Showing
21 changed files
with
501 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
.../src/test/java/com/vladmihalcea/hibernate/type/json/PostgreSQLJsonStringPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.