Skip to content

Commit 7a4be7c

Browse files
Fixed @JdbcTypeCode annotation on fields: Json, JsonDocument and others with YdbJdbcCode (#195)
1 parent 67eae91 commit 7a4be7c

File tree

4 files changed

+328
-2
lines changed

4 files changed

+328
-2
lines changed

hibernate-dialect/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Fixed @JdbcTypeCode annotation on fields: Json, JsonDocument and others with YdbJdbcCode
2+
13
## 1.5.0 ##
24

35
- Support for `GenerationType.IDENTITY` with JDBC YDB Driver **2.3.11** and later.

hibernate-dialect/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>tech.ydb.dialects</groupId>
88
<artifactId>hibernate-ydb-dialect</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010

1111
<packaging>jar</packaging>
1212

@@ -42,7 +42,7 @@
4242
<log4j2.version>2.17.2</log4j2.version>
4343

4444
<ydb.sdk.version>2.3.13</ydb.sdk.version>
45-
<ydb.jdbc.version>2.3.11</ydb.jdbc.version>
45+
<ydb.jdbc.version>2.3.18</ydb.jdbc.version>
4646
</properties>
4747

4848
<licenses>

hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
144144
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.DATETIME_64, LocalDateTime.class));
145145
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.TIMESTAMP_64, Instant.class));
146146
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INTERVAL_64, Duration.class));
147+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.JSON, String.class));
148+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.JSON_DOCUMENT, String.class));
149+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.BOOL, Boolean.class));
150+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INT8, Integer.class));
151+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INT16, Integer.class));
152+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.UINT16, Integer.class));
153+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INT32, Integer.class));
154+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.UINT32, Integer.class));
155+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INT64, Integer.class));
156+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.UINT64, Integer.class));
157+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.FLOAT, Float.class));
158+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.DOUBLE, Double.class));
159+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.BYTES, byte[].class));
160+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.TEXT, String.class));
161+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.YSON, byte[].class));
162+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.JSON, String.class));
163+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.UUID, java.util.UUID.class));
164+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.DATE, LocalDate.class));
165+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.DATETIME, LocalDateTime.class));
166+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.TIMESTAMP, Instant.class));
167+
typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INTERVAL, Duration.class));
147168

148169
typeContributions.contributeJavaType(BigDecimalJavaType.INSTANCE_22_9);
149170
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9));
@@ -171,6 +192,26 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
171192
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_31_9, "Decimal(31, 9)", "Decimal(31, 9)", this));
172193
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_0, "Decimal(35, 0)", "Decimal(35, 0)", this));
173194
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_9, "Decimal(35, 9)", "Decimal(35, 9)", this));
195+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.JSON, "Json", "Json", this));
196+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.JSON_DOCUMENT, "JsonDocument", "JsonDocument", this));
197+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.BOOL, "Bool", "Bool", this));
198+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INT8, "Int8", "Int8", this));
199+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INT16, "Int16", "Int16", this));
200+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UINT16, "Uint16", "Uint16", this));
201+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INT32, "Int32", "Int32", this));
202+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UINT32, "Uint32", "Uint32", this));
203+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INT64, "Int64", "Int64", this));
204+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UINT64, "Uint64", "Uint64", this));
205+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.FLOAT, "Float", "Float", this));
206+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DOUBLE, "Double", "Double", this));
207+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.BYTES, "Bytes", "Bytes", this));
208+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.TEXT, "Text", "Text", this));
209+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.YSON, "Yson", "Yson", this));
210+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UUID, "Uuid", "Uuid", this));
211+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DATE, "Date", "Date", this));
212+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DATETIME, "Datetime", "Datetime", this));
213+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.TIMESTAMP, "Timestamp", "Timestamp", this));
214+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INTERVAL, "Interval", "Interval", this));
174215
}
175216

176217
@Override
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
package tech.ydb.hibernate.types;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.Table;
7+
import java.nio.charset.StandardCharsets;
8+
import java.time.Duration;
9+
import java.time.Instant;
10+
import java.time.LocalDate;
11+
import java.time.LocalDateTime;
12+
import java.time.ZoneOffset;
13+
import java.util.UUID;
14+
import lombok.AllArgsConstructor;
15+
import lombok.Data;
16+
import lombok.NoArgsConstructor;
17+
import org.hibernate.annotations.JdbcTypeCode;
18+
import org.hibernate.cfg.AvailableSettings;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
23+
import tech.ydb.hibernate.TestUtils;
24+
import static tech.ydb.hibernate.TestUtils.basedConfiguration;
25+
import static tech.ydb.hibernate.TestUtils.inTransaction;
26+
import static tech.ydb.hibernate.TestUtils.jdbcUrl;
27+
import tech.ydb.hibernate.dialect.code.YdbJdbcCode;
28+
import tech.ydb.test.junit5.YdbHelperExtension;
29+
30+
/**
31+
* @author Kirill Kurdyukov
32+
*/
33+
public class YdbJdbcCodeTest {
34+
35+
@RegisterExtension
36+
private static final YdbHelperExtension ydb = new YdbHelperExtension();
37+
38+
@BeforeAll
39+
static void beforeAll() {
40+
TestUtils.SESSION_FACTORY = basedConfiguration()
41+
.addAnnotatedClass(YdbAllTypes.class)
42+
.setProperty(AvailableSettings.URL, jdbcUrl(ydb))
43+
.buildSessionFactory();
44+
}
45+
46+
@Test
47+
public void integrationTest() {
48+
/*
49+
* create table ydb_all_types (
50+
* aDouble Double,
51+
* aFloat Float,
52+
* bool Bool,
53+
* bytes Bytes,
54+
* date Date,
55+
* date32 Date32,
56+
* datetime Datetime,
57+
* datetime64 Datetime64,
58+
* int16 Int16,
59+
* int32 Int32,
60+
* int64 Int64,
61+
* int8 Int8,
62+
* interval Interval,
63+
* interval64 Interval64,
64+
* json Json,
65+
* jsonDocument JsonDocument,
66+
* text Text,
67+
* timestamp Timestamp,
68+
* timestamp64 Timestamp64,
69+
* uint16 Uint16,
70+
* uint32 Uint32,
71+
* uint64 Uint64,
72+
* uint8 Uint8,
73+
* uuid Uuid not null,
74+
* yson Yson,
75+
* primary key (uuid)
76+
* )
77+
*/
78+
var ydbAllEntity = new YdbAllTypes(
79+
UUID.randomUUID(),
80+
true, 1, 1, 1, 1, 1, 1, 1, 1,
81+
LocalDate.now(),
82+
LocalDateTime.of(2000, 1, 1, 1, 1, 1),
83+
Instant.parse("2025-10-02T11:06:32.779087Z"),
84+
Duration.ofSeconds(1),
85+
LocalDate.ofYearDay(1000, 1),
86+
LocalDateTime.of(1000, 1, 1, 1, 1, 1),
87+
LocalDateTime.of(1000, 1, 1, 1, 1, 1).toInstant(ZoneOffset.UTC),
88+
Duration.ofSeconds(-1),
89+
"text",
90+
"{\"a\":\"a\"}",
91+
"{\"a\":\"a\"}",
92+
new byte[]{10, 20},
93+
"{a=1u}".getBytes(StandardCharsets.UTF_8),
94+
1.1f, 1.1
95+
);
96+
97+
inTransaction(session -> session.persist(ydbAllEntity));
98+
inTransaction(session -> assertEquals(ydbAllEntity, session.find(YdbAllTypes.class, ydbAllEntity.getUuid())));
99+
100+
var newYdbAllEntity = new YdbAllTypes(
101+
ydbAllEntity.getUuid(),
102+
false, 2, 2, 2, 2, 2, 2, 2, 2,
103+
LocalDate.now(),
104+
LocalDateTime.of(2000, 2, 2, 2, 2, 2),
105+
Instant.parse("2023-10-02T11:06:32.779087Z"),
106+
Duration.ofSeconds(2),
107+
LocalDate.ofYearDay(1000, 2),
108+
LocalDateTime.of(1000, 2, 2, 2, 2, 2),
109+
LocalDateTime.of(1000, 2, 2, 2, 2, 2).toInstant(ZoneOffset.UTC),
110+
Duration.ofSeconds(-2),
111+
"new_text",
112+
"{\"b\":\"b\"}",
113+
"{\"b\":\"b\"}",
114+
new byte[]{20, 20},
115+
"{a=2u}".getBytes(StandardCharsets.UTF_8),
116+
2.2f, 2.2
117+
);
118+
inTransaction(session -> {
119+
session.createQuery(
120+
"""
121+
UPDATE YdbAllTypes e
122+
SET
123+
e.bool = :bool,
124+
e.uint8 = :uint8,
125+
e.int8 = :int8,
126+
e.uint16 = :uint16,
127+
e.int16 = :int16,
128+
e.uint32 = :uint32,
129+
e.int32 = :int32,
130+
e.uint64 = :uint64,
131+
e.int64 = :int64,
132+
e.date = :date,
133+
e.datetime = :datetime,
134+
e.timestamp = :timestamp,
135+
e.interval = :interval,
136+
e.date32 = :date32,
137+
e.datetime64 = :datetime64,
138+
e.timestamp64 = :timestamp64,
139+
e.interval64 = :interval64,
140+
e.text = :text,
141+
e.json = :json,
142+
e.jsonDocument = :jsonDocument,
143+
e.bytes = :bytes,
144+
e.yson = :yson,
145+
e.aFloat = :aFloat,
146+
e.aDouble = :aDouble
147+
""")
148+
.setParameter("bool", newYdbAllEntity.isBool())
149+
.setParameter("uint8", newYdbAllEntity.getUint8())
150+
.setParameter("int8", newYdbAllEntity.getInt8())
151+
.setParameter("uint16", newYdbAllEntity.getUint16())
152+
.setParameter("int16", newYdbAllEntity.getInt16())
153+
.setParameter("uint32", newYdbAllEntity.getUint32())
154+
.setParameter("int32", newYdbAllEntity.getInt32())
155+
.setParameter("uint64", newYdbAllEntity.getUint64())
156+
.setParameter("int64", newYdbAllEntity.getInt64())
157+
.setParameter("date", newYdbAllEntity.getDate())
158+
.setParameter("datetime", newYdbAllEntity.getDatetime())
159+
.setParameter("timestamp", newYdbAllEntity.getTimestamp())
160+
.setParameter("interval", newYdbAllEntity.getInterval())
161+
.setParameter("date32", newYdbAllEntity.getDate32())
162+
.setParameter("datetime64", newYdbAllEntity.getDatetime64())
163+
.setParameter("timestamp64", newYdbAllEntity.getTimestamp64())
164+
.setParameter("interval64", newYdbAllEntity.getInterval64())
165+
.setParameter("text", newYdbAllEntity.getText())
166+
.setParameter("json", newYdbAllEntity.getJson())
167+
.setParameter("jsonDocument", newYdbAllEntity.getJsonDocument())
168+
.setParameter("bytes", newYdbAllEntity.getBytes())
169+
.setParameter("yson", newYdbAllEntity.getYson())
170+
.setParameter("aFloat", newYdbAllEntity.getAFloat())
171+
.setParameter("aDouble", newYdbAllEntity.getADouble())
172+
.executeUpdate();
173+
assertEquals(newYdbAllEntity, session.find(YdbAllTypes.class, newYdbAllEntity.getUuid()));
174+
});
175+
}
176+
177+
@Entity(name = "YdbAllTypes")
178+
@Table(name = "ydb_all_types")
179+
@Data
180+
@AllArgsConstructor
181+
@NoArgsConstructor
182+
public static class YdbAllTypes {
183+
@Id
184+
@JdbcTypeCode(YdbJdbcCode.UUID)
185+
private UUID uuid;
186+
187+
@Column
188+
@JdbcTypeCode(YdbJdbcCode.BOOL)
189+
private boolean bool;
190+
191+
@Column
192+
@JdbcTypeCode(YdbJdbcCode.UINT8)
193+
private int uint8;
194+
195+
@Column
196+
@JdbcTypeCode(YdbJdbcCode.INT8)
197+
private int int8;
198+
199+
@Column
200+
@JdbcTypeCode(YdbJdbcCode.UINT16)
201+
private int uint16;
202+
203+
@Column
204+
@JdbcTypeCode(YdbJdbcCode.INT16)
205+
private int int16;
206+
207+
@Column
208+
@JdbcTypeCode(YdbJdbcCode.UINT32)
209+
private int uint32;
210+
211+
@Column
212+
@JdbcTypeCode(YdbJdbcCode.INT32)
213+
private int int32;
214+
215+
@Column
216+
@JdbcTypeCode(YdbJdbcCode.UINT64)
217+
private int uint64;
218+
219+
@Column
220+
@JdbcTypeCode(YdbJdbcCode.INT64)
221+
private int int64;
222+
223+
@Column
224+
@JdbcTypeCode(YdbJdbcCode.DATE)
225+
private LocalDate date;
226+
227+
@Column
228+
@JdbcTypeCode(YdbJdbcCode.DATETIME)
229+
private LocalDateTime datetime;
230+
231+
@Column
232+
@JdbcTypeCode(YdbJdbcCode.TIMESTAMP)
233+
private Instant timestamp;
234+
235+
@Column
236+
@JdbcTypeCode(YdbJdbcCode.INTERVAL)
237+
private Duration interval;
238+
239+
@Column
240+
@JdbcTypeCode(YdbJdbcCode.DATE_32)
241+
private LocalDate date32;
242+
243+
@Column
244+
@JdbcTypeCode(YdbJdbcCode.DATETIME_64)
245+
private LocalDateTime datetime64;
246+
247+
@Column
248+
@JdbcTypeCode(YdbJdbcCode.TIMESTAMP_64)
249+
private Instant timestamp64;
250+
251+
@Column
252+
@JdbcTypeCode(YdbJdbcCode.INTERVAL_64)
253+
private Duration interval64;
254+
255+
@Column
256+
@JdbcTypeCode(YdbJdbcCode.TEXT)
257+
private String text;
258+
259+
@Column
260+
@JdbcTypeCode(YdbJdbcCode.JSON)
261+
private String json;
262+
263+
@Column
264+
@JdbcTypeCode(YdbJdbcCode.JSON_DOCUMENT)
265+
private String jsonDocument;
266+
267+
@Column
268+
@JdbcTypeCode(YdbJdbcCode.BYTES)
269+
private byte[] bytes;
270+
271+
@Column
272+
@JdbcTypeCode(YdbJdbcCode.YSON)
273+
private byte[] yson;
274+
275+
@Column
276+
@JdbcTypeCode(YdbJdbcCode.FLOAT)
277+
private float aFloat;
278+
279+
@Column
280+
@JdbcTypeCode(YdbJdbcCode.DOUBLE)
281+
private double aDouble;
282+
}
283+
}

0 commit comments

Comments
 (0)