/
OracleJsonStringPropertyTest.java
164 lines (135 loc) · 5.15 KB
/
OracleJsonStringPropertyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.vladmihalcea.hibernate.type.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.util.AbstractOracleIntegrationTest;
import com.vladmihalcea.hibernate.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.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
public class OracleJsonStringPropertyTest extends AbstractOracleIntegrationTest {
@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 = entityManager
.createQuery(
"select b " +
"from Book b " +
"where b.isbn = :isbn", Book.class)
.setParameter("isbn", "978-9730228236")
.getSingleResult();
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 JsonStringType(JsonNode.class))
.uniqueResult();
assertEquals("High-Performance Java Persistence", properties.get("title").asText());
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");
book.setProperties(null);
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");
assertNull(book.getProperties());
return null;
}
});
}
@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "json", typeClass = JsonStringType.class)
public static class Book {
@Id
@GeneratedValue
private Long id;
@NaturalId
private String isbn;
@Type(type = "json")
@Column(columnDefinition = "VARCHAR2(1000) CONSTRAINT HT_BOOK_IS_VALID_JSON CHECK (properties IS JSON)")
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;
}
}
}