Skip to content

Commit f8097a6

Browse files
committed
Use Integer as database column type for java.time.Year.
Previously, that case was handled by the registration for Temporal, which maps the type to a Timestamp, which doesn't make sense for Year.
1 parent 3f06ab2 commit f8097a6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcColumnTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.sql.Timestamp;
1919
import java.time.LocalDateTime;
2020
import java.time.OffsetDateTime;
21+
import java.time.Year;
2122
import java.time.ZonedDateTime;
2223
import java.time.temporal.Temporal;
2324
import java.util.LinkedHashMap;
@@ -40,6 +41,7 @@ public enum JdbcColumnTypes {
4041

4142
private final ConcurrentLruCache<Class<?>, Class<?>> cache = new ConcurrentLruCache<>(64, this::doResolve);
4243

44+
@Override
4345
@SuppressWarnings({ "unchecked", "rawtypes" })
4446
public Class<?> resolvePrimitiveType(Class<?> type) {
4547
return cache.get(type);
@@ -62,6 +64,7 @@ private Class<?> doResolve(Class<?> type) {
6264
javaToDbType.put(ZonedDateTime.class, String.class);
6365
javaToDbType.put(OffsetDateTime.class, OffsetDateTime.class);
6466
javaToDbType.put(LocalDateTime.class, LocalDateTime.class);
67+
javaToDbType.put(Year.class, Integer.class);
6568
javaToDbType.put(Temporal.class, Timestamp.class);
6669
}
6770

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.core.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.time.Year;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* Unit tests for {@link JdbcColumnTypes}.
26+
*
27+
* @author Oliver Drotbohm
28+
*/
29+
class JdbcColumnTypesUnitTests {
30+
31+
@Test
32+
void usesIntegerForYear() {
33+
assertThat(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(Year.class)).isEqualTo(Integer.class);
34+
}
35+
}

0 commit comments

Comments
 (0)