Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support UUID YDB type #165

Merged
merged 5 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hibernate-dialect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.0 ##

- Added support UUID YDB type

## 1.2.0 ##

- Added custom decimal jdbc codes `DECIMAL_31_9`, `DECIMAL_35_0`, `DECIMAL_35_9`
Expand Down
2 changes: 1 addition & 1 deletion hibernate-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tech.ydb.dialects</groupId>
<artifactId>hibernate-ydb-dialect</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE;
import static org.hibernate.type.SqlTypes.TIME_WITH_TIMEZONE;
import static org.hibernate.type.SqlTypes.TINYINT;
import static org.hibernate.type.SqlTypes.UUID;
import static org.hibernate.type.SqlTypes.VARBINARY;
import static org.hibernate.type.SqlTypes.VARCHAR;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.descriptor.java.UUIDJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.UUIDJdbcType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
Expand Down Expand Up @@ -111,6 +114,7 @@ protected String columnType(int sqlTypeCode) {
LONG32VARCHAR, LONG32NVARCHAR, LONGVARCHAR, LONGNVARCHAR -> "Text";
case BINARY, VARBINARY, BLOB, LONGVARBINARY, LONG32VARBINARY -> "Bytes";
case JSON -> "Json";
case UUID, YdbJdbcCode.UUID -> "Uuid";
default -> super.columnType(sqlTypeCode);
};
}
Expand All @@ -119,20 +123,22 @@ protected String columnType(int sqlTypeCode) {
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
super.contributeTypes(typeContributions, serviceRegistry);

typeContributions.contributeJavaType(UUIDJavaType.INSTANCE);
typeContributions.contributeJdbcType(UUIDJdbcType.INSTANCE);
typeContributions.contributeJavaType(LocalDateTimeJavaType.INSTANCE);
typeContributions.contributeJdbcType(LocalDateTimeJdbcType.INSTANCE);
typeContributions.contributeJavaType(LocalDateJavaType.INSTANCE);
typeContributions.contributeJdbcType(LocalDateJdbcType.INSTANCE);
typeContributions.contributeJavaType(InstantJavaType.INSTANCE);
typeContributions.contributeJdbcType(InstantJdbcType.INSTANCE);
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_31_9));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_0));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_9));

// custom jdbc codec
typeContributions.contributeJdbcType(Uint8JdbcType.INSTANCE);
typeContributions.contributeJavaType(BigDecimalJavaType.INSTANCE_22_9);
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_31_9));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_0));
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_9));
}

@Override
Expand All @@ -141,6 +147,7 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR

final DdlTypeRegistry ddlTypeRegistry = typeContributions.getTypeConfiguration().getDdlTypeRegistry();

ddlTypeRegistry.addDescriptor(new DdlTypeImpl(UUID, "Uuid", "Uuid", this));
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DATETIME, "Datetime", "Datetime", this));
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UINT8, "Uint8", "Uint8", this));
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_22_9, "Decimal(22, 9)", "Decimal(22, 9)", this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class Employee {
@Enumerated(EnumType.STRING)
private Enum bnEnum;

private UUID uuid;

public enum Enum {
ONE, TWO
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import org.hibernate.cfg.AvailableSettings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -26,7 +28,7 @@ public class TypesTest {
static void beforeAll() {
TestUtils.SESSION_FACTORY = basedConfiguration()
.addAnnotatedClass(Employee.class)
.setProperty(AvailableSettings.URL, jdbcUrl(ydb))
.setProperty(AvailableSettings.URL, jdbcUrl(ydb) + "?disablePrepareDataQuery=true")
.buildSessionFactory();
}

Expand All @@ -44,7 +46,8 @@ void integrationTypesTest() {
LocalDateTime.parse("2023-09-16T12:30:00"),
new byte[]{1, 2, 3, 4, 5},
Employee.Enum.ONE,
Employee.Enum.TWO
Employee.Enum.TWO,
UUID.randomUUID()
);

inTransaction(session -> session.persist(employee));
Expand All @@ -57,5 +60,22 @@ void integrationTypesTest() {
inTransaction(session -> assertEquals(employee, session
.createQuery("FROM Employee e WHERE e.isActive = false", Employee.class)
.getSingleResult()));

List<String> uuids = List.of(
"123e4567-e89b-12d3-a456-426614174000",
"2d9e498b-b746-9cfb-084d-de4e1cb4736e",
"6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
);

for (var uuid : uuids) {
employee.setUuid(UUID.fromString(uuid));
inTransaction(session -> session.merge(employee));
inTransaction(session -> {
var actualEmployee = session.find(Employee.class, employee.getId());

assertEquals(employee, actualEmployee);
assertEquals(uuid.toLowerCase(), actualEmployee.getUuid().toString());
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.Instant;
import java.util.concurrent.ThreadLocalRandom;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
Expand All @@ -14,9 +16,6 @@
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.SqlTypes;

import java.time.Instant;
import java.util.concurrent.ThreadLocalRandom;

/**
* @author Kirill Kurdyukov
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,18 @@ void integrationTest() {
user.setJson(json);

inTransaction(session -> session.persist(user));

inTransaction(
session -> {
User findUser = session.find(User.class, user.getId());

assertEquals("Kirill", findUser.getName());
assertEquals(json, findUser.getJson());

assertTrue(Instant.now().compareTo(findUser.getCreatedAt()) >= 0);
assertTrue(Instant.now().compareTo(findUser.getUpdatedAt()) >= 0);
}
);

User rollbackUser = new User();

user.setId(10);
user.setName("Kirill");

try {
Expand All @@ -73,7 +69,7 @@ void integrationTest() {
} catch (RuntimeException ignored) {
}

inTransaction(session -> assertNull(session.find(User.class, 10)));
inTransaction(session -> assertNull(session.find(User.class, rollbackUser.getId())));
}

@Test
Expand Down