/
OracleJsonStringPropertyTest.java
169 lines (139 loc) · 5.02 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
165
166
167
168
169
package com.vladmihalcea.hibernate.type.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.util.AbstractOracleIntegrationTest;
import net.ttddyy.dsproxy.QueryCount;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.query.NativeQuery;
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
};
}
@Override
protected void afterInit() {
doInJPA(entityManager -> {
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.setProperties(
"{" +
" \"title\": \"High-Performance Java Persistence\"," +
" \"author\": \"Vlad Mihalcea\"," +
" \"publisher\": \"Amazon\"," +
" \"price\": 44.99" +
"}"
)
);
});
}
@Test
public void test() {
doInJPA(entityManager -> {
Book book = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");
QueryCountHolder.clear();
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/\"" +
"}"
);
});
QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getUpdate());
doInJPA(entityManager -> {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(NativeQuery.class)
.addScalar("properties", new JsonStringType(JsonNode.class))
.getSingleResult();
assertEquals("High-Performance Java Persistence", properties.get("title").asText());
});
}
@Test
public void testNullValue() {
doInJPA(entityManager -> {
Book book = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");
QueryCountHolder.clear();
book.setProperties(null);
});
QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getUpdate());
doInJPA(entityManager -> {
Book book = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");
assertNull(book.getProperties());
});
}
@Test
public void testLoad() {
QueryCountHolder.clear();
doInJPA(entityManager -> {
Session session = entityManager.unwrap(Session.class);
Book book = session
.bySimpleNaturalId(Book.class)
.load("978-9730228236");
assertTrue(book.getProperties().contains("\"price\": 44.99"));
});
QueryCount queryCount = QueryCountHolder.getGrandTotal();
assertEquals(1, queryCount.getTotal());
assertEquals(1, queryCount.getSelect());
assertEquals(0, queryCount.getUpdate());
}
@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;
}
}
}