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