2
2
3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
4
import com .vladmihalcea .hibernate .type .json .JsonBinaryType ;
5
+ import com .vladmihalcea .hibernate .type .json .JsonType ;
5
6
import com .vladmihalcea .hibernate .type .util .ObjectMapperWrapper ;
6
7
import com .vladmihalcea .hibernate .util .AbstractPostgreSQLIntegrationTest ;
7
8
import org .hibernate .Session ;
12
13
13
14
import javax .persistence .*;
14
15
import java .io .Serializable ;
15
- import java .sql .Timestamp ;
16
- import java .time .LocalDateTime ;
16
+ import java .math .BigDecimal ;
17
17
import java .util .*;
18
+ import java .util .function .Function ;
19
+ import java .util .stream .Collectors ;
18
20
19
21
import static org .junit .Assert .assertEquals ;
20
22
@@ -37,15 +39,19 @@ protected void additionalProperties(Properties properties) {
37
39
(TypeContributorList ) () -> Collections .singletonList (
38
40
(typeContributions , serviceRegistry ) ->
39
41
typeContributions .contributeType (
40
- new JsonBinaryType (
42
+ new JsonType (
41
43
objectMapper .activateDefaultTypingAsProperty (
42
44
objectMapper .getPolymorphicTypeValidator (),
43
45
ObjectMapper .DefaultTyping .OBJECT_AND_NON_CONCRETE ,
44
- "className "
46
+ "type "
45
47
),
46
48
ArrayList .class
47
- ),
48
- "json-polymorphic-list"
49
+ ) {
50
+ @ Override
51
+ public String getName () {
52
+ return "json-polymorphic-list" ;
53
+ }
54
+ }
49
55
)
50
56
)
51
57
);
@@ -54,17 +60,15 @@ protected void additionalProperties(Properties properties) {
54
60
@ Test
55
61
public void test () {
56
62
57
- Timestamp validUntil = Timestamp .valueOf (LocalDateTime .now ().plusDays (10 ));
58
-
59
63
doInJPA (entityManager -> {
60
64
entityManager .persist (
61
65
new Book ()
62
66
.setIsbn ("978-9730228236" )
63
- .addTopic (new Post ( "High-Performance Java Persistence " )
64
- .setContent ( "It rocks!" )
67
+ .addCoupon (new AmountDiscountCoupon ( "PPP " )
68
+ .setAmount ( new BigDecimal ( "4.99" ) )
65
69
)
66
- .addTopic (new Announcement ("Black Friday - 50% discount " )
67
- .setValidUntil ( validUntil )
70
+ .addCoupon (new PercentageDiscountCoupon ("Black Friday" )
71
+ .setPercentage ( BigDecimal . valueOf ( 0.02 ) )
68
72
)
69
73
);
70
74
});
@@ -74,14 +78,28 @@ public void test() {
74
78
.bySimpleNaturalId (Book .class )
75
79
.load ("978-9730228236" );
76
80
77
- List <Topic > topics = book .getTopics ();
81
+ Map <String , DiscountCoupon > topics = book .getCoupons ()
82
+ .stream ()
83
+ .collect (
84
+ Collectors .toMap (
85
+ DiscountCoupon ::getName ,
86
+ Function .identity ()
87
+ )
88
+ );
89
+
78
90
assertEquals (2 , topics .size ());
79
- Post post = (Post ) topics .get (0 );
80
- assertEquals ("It rocks!" , post .getContent ());
81
- Announcement announcement = (Announcement ) topics .get (1 );
91
+ AmountDiscountCoupon amountDiscountCoupon = (AmountDiscountCoupon )
92
+ topics .get ("PPP" );
93
+ assertEquals (
94
+ new BigDecimal ("4.99" ),
95
+ amountDiscountCoupon .getAmount ()
96
+ );
97
+
98
+ PercentageDiscountCoupon percentageDiscountCoupon = (PercentageDiscountCoupon )
99
+ topics .get ("Black Friday" );
82
100
assertEquals (
83
- validUntil . getTime ( ),
84
- announcement . getValidUntil (). getTime ()
101
+ BigDecimal . valueOf ( 0.02 ),
102
+ percentageDiscountCoupon . getPercentage ()
85
103
);
86
104
});
87
105
}
@@ -100,7 +118,7 @@ public static class Book {
100
118
101
119
@ Type (type = "json-polymorphic-list" )
102
120
@ Column (columnDefinition = "jsonb" )
103
- private List <Topic > topics = new ArrayList <>();
121
+ private List <DiscountCoupon > coupons = new ArrayList <>();
104
122
105
123
public String getIsbn () {
106
124
return isbn ;
@@ -111,83 +129,92 @@ public Book setIsbn(String isbn) {
111
129
return this ;
112
130
}
113
131
114
- public List <Topic > getTopics () {
115
- return topics ;
132
+ public List <DiscountCoupon > getCoupons () {
133
+ return coupons ;
116
134
}
117
135
118
- public Book setTopics (List <Topic > topics ) {
119
- this .topics = topics ;
136
+ public Book setCoupons (List <DiscountCoupon > coupons ) {
137
+ this .coupons = coupons ;
120
138
return this ;
121
139
}
122
140
123
- public Book addTopic ( Topic topic ) {
124
- topics .add (topic );
141
+ public Book addCoupon ( DiscountCoupon topic ) {
142
+ coupons .add (topic );
125
143
return this ;
126
144
}
127
145
}
128
146
129
- public static abstract class Topic implements Serializable {
147
+ public abstract static class DiscountCoupon implements Serializable {
130
148
131
- private String title ;
149
+ private String name ;
150
+
151
+ public DiscountCoupon () {
152
+ }
132
153
133
- public Topic () {
154
+ public DiscountCoupon (String name ) {
155
+ this .name = name ;
134
156
}
135
157
136
- public Topic ( String title ) {
137
- this . title = title ;
158
+ public String getName ( ) {
159
+ return name ;
138
160
}
139
161
140
- public String getTitle ( ) {
141
- return title ;
162
+ public void setName ( String name ) {
163
+ this . name = name ;
142
164
}
143
165
144
- public void setTitle (String title ) {
145
- this .title = title ;
166
+ @ Override
167
+ public boolean equals (Object o ) {
168
+ if (this == o ) return true ;
169
+ if (!(o instanceof DiscountCoupon )) return false ;
170
+ DiscountCoupon that = (DiscountCoupon ) o ;
171
+ return Objects .equals (getName (), that .getName ());
146
172
}
147
173
148
- String getType () {
149
- return getClass ().getSimpleName ();
174
+ @ Override
175
+ public int hashCode () {
176
+ return Objects .hash (getName ());
150
177
}
151
178
}
152
179
153
- public static class Post extends Topic {
180
+ public static class AmountDiscountCoupon extends DiscountCoupon {
154
181
155
- private String content ;
182
+ private BigDecimal amount ;
156
183
157
- public Post () {
184
+ public AmountDiscountCoupon () {
158
185
}
159
186
160
- public Post (String title ) {
161
- super (title );
187
+ public AmountDiscountCoupon (String name ) {
188
+ super (name );
162
189
}
163
190
164
- public String getContent () {
165
- return content ;
191
+ public BigDecimal getAmount () {
192
+ return amount ;
166
193
}
167
194
168
- public Post setContent ( String content ) {
169
- this .content = content ;
195
+ public AmountDiscountCoupon setAmount ( BigDecimal amount ) {
196
+ this .amount = amount ;
170
197
return this ;
171
198
}
172
199
}
173
200
174
- public static class Announcement extends Topic {
201
+ public static class PercentageDiscountCoupon extends DiscountCoupon {
175
202
176
- private Date validUntil ;
203
+ private BigDecimal percentage ;
177
204
178
- public Announcement () {
205
+ public PercentageDiscountCoupon () {
179
206
}
180
207
181
- public Announcement (String title ) {
182
- super (title );
208
+ public PercentageDiscountCoupon (String name ) {
209
+ super (name );
183
210
}
184
211
185
- public Date getValidUntil () {
186
- return validUntil ;
212
+ public BigDecimal getPercentage () {
213
+ return percentage ;
187
214
}
188
215
189
- public Announcement setValidUntil ( Date validUntil ) {
190
- this .validUntil = validUntil ;
216
+ public PercentageDiscountCoupon setPercentage ( BigDecimal amount ) {
217
+ this .percentage = amount ;
191
218
return this ;
192
219
}
193
220
}
0 commit comments