16
16
package example .springdata .jdbc .basics ;
17
17
18
18
import javax .sql .DataSource ;
19
+ import java .sql .Clob ;
20
+ import java .sql .SQLException ;
19
21
import java .time .Period ;
20
22
import java .util .Collections ;
21
23
import java .util .HashMap ;
27
29
import org .springframework .boot .SpringApplication ;
28
30
import org .springframework .context .ApplicationListener ;
29
31
import org .springframework .context .annotation .Bean ;
32
+ import org .springframework .core .convert .ConversionService ;
33
+ import org .springframework .core .convert .converter .Converter ;
30
34
import org .springframework .data .jdbc .mapping .event .BeforeSave ;
35
+ import org .springframework .data .jdbc .mapping .model .ConversionCustomizer ;
31
36
import org .springframework .data .jdbc .mapping .model .DefaultNamingStrategy ;
32
37
import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
33
38
import org .springframework .data .jdbc .mapping .model .NamingStrategy ;
34
39
import org .springframework .data .jdbc .repository .config .EnableJdbcRepositories ;
35
40
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
36
41
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
42
+ import org .springframework .lang .Nullable ;
37
43
38
44
import example .springdata .jdbc .basics .domain .LegoSet ;
39
45
import example .springdata .jdbc .basics .domain .LegoSetRepository ;
46
+ import example .springdata .jdbc .basics .domain .Manual ;
40
47
41
48
/**
42
49
* Demonstrates non trivial usage of Spring Data JDBC especially handling of collections and references crossing aggregate boundaries.
43
50
* It tries to showcase the following
44
51
* <ul>
45
52
* <li>Custom Names for columns and tables via NamingStrategy</li>
46
53
* <li>Manual id generation</li>
54
+ * <li>Custom conversions</li>
47
55
* </ul>
48
56
*
49
57
* @author Jens Schauder
@@ -59,10 +67,18 @@ public class AggregatesApplication implements CommandLineRunner {
59
67
@ Override
60
68
public void run (String ... args ) throws Exception {
61
69
70
+
71
+ Manual manual = new Manual ();
72
+ manual .setAuthor ("Jens Schauder" );
73
+ manual .setText ("Just put all the pieces together in the right order" );
74
+
62
75
LegoSet smallCar = new LegoSet ();
63
76
smallCar .setMinimumAge (Period .ofYears (5 ));
64
77
smallCar .setMaximumAge (Period .ofYears (12 ));
65
78
79
+ smallCar .setManual (manual );
80
+
81
+
66
82
Output .list (Collections .singleton (smallCar ), "debugging" );
67
83
68
84
repository .save (smallCar );
@@ -86,14 +102,19 @@ DataSource dataSource() {
86
102
}
87
103
88
104
@ Bean
89
- public ApplicationListener <?> timeStampingSaveTime () {
105
+ public ApplicationListener <?> idSetting () {
90
106
91
107
return (ApplicationListener <BeforeSave >) event -> {
92
108
93
109
Object entity = event .getEntity ();
94
110
if (entity instanceof LegoSet ) {
95
111
LegoSet legoSet = (LegoSet ) entity ;
96
112
legoSet .setId (id .incrementAndGet ());
113
+
114
+ Manual manual = legoSet .getManual ();
115
+ if (manual != null ) {
116
+ manual .setId ((long )legoSet .getId ());
117
+ }
97
118
}
98
119
};
99
120
}
@@ -137,4 +158,23 @@ public String getReverseColumnName(JdbcPersistentProperty property) {
137
158
}
138
159
};
139
160
}
161
+
162
+ @ Bean
163
+ public ConversionCustomizer conversionCustomizer () {
164
+ return conversions -> conversions .addConverter (new Converter <Clob , String >() {
165
+ @ Nullable
166
+ @ Override
167
+ public String convert (Clob clob ) {
168
+
169
+ try {
170
+ int length = Math .toIntExact (clob .length ());
171
+ if (length == 0 ) return "" ;
172
+
173
+ return clob .getSubString (1 , length );
174
+ } catch (SQLException e ) {
175
+ throw new IllegalStateException ("Failed to convert CLOB to String." , e );
176
+ }
177
+ }
178
+ });
179
+ }
140
180
}
0 commit comments