Skip to content

Commit 453e00c

Browse files
committed
DATAJDBC-example - basic reading and writing seems to work!.
including id used a foreign key and custom conversion!!!
1 parent 8761975 commit 453e00c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

jdbc/basics/src/main/java/example/springdata/jdbc/basics/AggregatesApplication.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package example.springdata.jdbc.basics;
1717

1818
import javax.sql.DataSource;
19+
import java.sql.Clob;
20+
import java.sql.SQLException;
1921
import java.time.Period;
2022
import java.util.Collections;
2123
import java.util.HashMap;
@@ -27,23 +29,29 @@
2729
import org.springframework.boot.SpringApplication;
2830
import org.springframework.context.ApplicationListener;
2931
import org.springframework.context.annotation.Bean;
32+
import org.springframework.core.convert.ConversionService;
33+
import org.springframework.core.convert.converter.Converter;
3034
import org.springframework.data.jdbc.mapping.event.BeforeSave;
35+
import org.springframework.data.jdbc.mapping.model.ConversionCustomizer;
3136
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
3237
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
3338
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
3439
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
3540
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3641
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
42+
import org.springframework.lang.Nullable;
3743

3844
import example.springdata.jdbc.basics.domain.LegoSet;
3945
import example.springdata.jdbc.basics.domain.LegoSetRepository;
46+
import example.springdata.jdbc.basics.domain.Manual;
4047

4148
/**
4249
* Demonstrates non trivial usage of Spring Data JDBC especially handling of collections and references crossing aggregate boundaries.
4350
* It tries to showcase the following
4451
* <ul>
4552
* <li>Custom Names for columns and tables via NamingStrategy</li>
4653
* <li>Manual id generation</li>
54+
* <li>Custom conversions</li>
4755
* </ul>
4856
*
4957
* @author Jens Schauder
@@ -59,10 +67,18 @@ public class AggregatesApplication implements CommandLineRunner {
5967
@Override
6068
public void run(String... args) throws Exception {
6169

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+
6275
LegoSet smallCar = new LegoSet();
6376
smallCar.setMinimumAge(Period.ofYears(5));
6477
smallCar.setMaximumAge(Period.ofYears(12));
6578

79+
smallCar.setManual(manual);
80+
81+
6682
Output.list(Collections.singleton(smallCar), "debugging");
6783

6884
repository.save(smallCar);
@@ -86,14 +102,19 @@ DataSource dataSource() {
86102
}
87103

88104
@Bean
89-
public ApplicationListener<?> timeStampingSaveTime() {
105+
public ApplicationListener<?> idSetting() {
90106

91107
return (ApplicationListener<BeforeSave>) event -> {
92108

93109
Object entity = event.getEntity();
94110
if (entity instanceof LegoSet) {
95111
LegoSet legoSet = (LegoSet) entity;
96112
legoSet.setId(id.incrementAndGet());
113+
114+
Manual manual = legoSet.getManual();
115+
if (manual != null) {
116+
manual.setId((long)legoSet.getId());
117+
}
97118
}
98119
};
99120
}
@@ -137,4 +158,23 @@ public String getReverseColumnName(JdbcPersistentProperty property) {
137158
}
138159
};
139160
}
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+
}
140180
}

jdbc/basics/src/main/java/example/springdata/jdbc/basics/domain/Manual.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package example.springdata.jdbc.basics.domain;
1717

1818
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.annotation.Transient;
1920

2021
import lombok.Data;
2122

@@ -26,7 +27,7 @@
2627
public class Manual {
2728

2829
@Id
29-
private final Long id;
30+
private Long id;
3031

3132
private String author;
3233
private String text;

jdbc/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>org.springframework.data</groupId>
3030
<artifactId>spring-data-jdbc</artifactId>
31-
<version>1.0.0.BUILD-SNAPSHOT</version>
31+
<version>1.0.0.WIP-SNAPSHOT</version>
3232
</dependency>
3333

3434
<dependency>

0 commit comments

Comments
 (0)