/
GenericOracleJsonMapTest.java
115 lines (91 loc) · 3.15 KB
/
GenericOracleJsonMapTest.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
package com.vladmihalcea.hibernate.type.json.generic;
import com.vladmihalcea.hibernate.type.json.JsonType;
import com.vladmihalcea.hibernate.util.AbstractOracleIntegrationTest;
import com.vladmihalcea.hibernate.util.transaction.JPATransactionFunction;
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 java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class GenericOracleJsonMapTest 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")
.addProperty("title", "High-Performance Java Persistence")
.addProperty("author", "Vlad Mihalcea")
.addProperty("publisher", "Amazon")
.addProperty("price", "$44.95")
);
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");
Map<String, String> bookProperties = book.getProperties();
assertEquals(
"High-Performance Java Persistence",
bookProperties.get("title")
);
assertEquals(
"Vlad Mihalcea",
bookProperties.get("author")
);
return null;
}
});
}
@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "json", typeClass = JsonType.class)
public static class Book {
@Id
@GeneratedValue
private Long id;
@NaturalId
@Column(length = 15)
private String isbn;
@Type(type = "json")
@Column(columnDefinition = "VARCHAR2(1000) CONSTRAINT HT_BOOK_IS_VALID_JSON CHECK (properties IS JSON)")
private Map<String, String> properties = new HashMap<String, String>();
public String getIsbn() {
return isbn;
}
public Book setIsbn(String isbn) {
this.isbn = isbn;
return this;
}
public Map<String, String> getProperties() {
return properties;
}
public Book setProperties(Map<String, String> properties) {
this.properties = properties;
return this;
}
public Book addProperty(String key, String value) {
properties.put(key, value);
return this;
}
}
}