Skip to content

Commit

Permalink
From Hibernate 6.2, ImmutableType needs to implement BasicDomainType #…
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Jun 17, 2023
1 parent e8997c8 commit 60b2ced
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import io.hypersistence.utils.hibernate.type.util.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.model.domain.BasicDomainType;
import org.hibernate.query.BindableType;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.usertype.UserType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -18,7 +20,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class DescriptorImmutableType<T, JDBC extends JdbcType, JAVA extends JavaType<T>> extends ImmutableType<T> implements BindableType<T>, SqmExpressible<T> {
public abstract class DescriptorImmutableType<T, JDBC extends JdbcType, JAVA extends JavaType<T>> extends ImmutableType<T> implements BindableType<T>, SqmExpressible<T>, BasicDomainType<T> {

private final JDBC jdbcTypeDescriptor;
private final JAVA javaTypeDescriptor;
Expand Down Expand Up @@ -70,4 +72,29 @@ public Class<T> getBindableJavaType() {
public JavaType<T> getExpressibleJavaType() {
return javaTypeDescriptor;
}

@Override
public Class<T> getJavaType() {
return returnedClass();
}

@Override
public boolean canDoExtraction() {
return true;
}

@Override
public JdbcType getJdbcType() {
return jdbcTypeDescriptor;
}

@Override
public T extract(CallableStatement callableStatement, int position, SharedSessionContractImplementor session) throws SQLException {
return jdbcTypeDescriptor.getExtractor(javaTypeDescriptor).extract(callableStatement, position, session);
}

@Override
public T extract(CallableStatement callableStatement, String position, SharedSessionContractImplementor session) throws SQLException {
return jdbcTypeDescriptor.getExtractor(javaTypeDescriptor).extract(callableStatement, position, session);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.hypersistence.utils.hibernate.type;

import io.hypersistence.utils.hibernate.type.util.Configuration;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.ParameterizedType;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.Properties;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.model.domain.BasicDomainType;
import org.hibernate.query.BindableType;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.type.descriptor.java.JavaType;
Expand All @@ -17,6 +18,7 @@
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -26,7 +28,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class MutableType<T, JDBC extends JdbcType, JAVA extends JavaType<T>> implements UserType<T>, BindableType<T>, SqmExpressible<T>, BasicValuedMapping {
public abstract class MutableType<T, JDBC extends JdbcType, JAVA extends JavaType<T>> implements UserType<T>, BindableType<T>, SqmExpressible<T>, BasicValuedMapping, BasicDomainType<T> {

private final Class<T> clazz;

Expand Down Expand Up @@ -175,4 +177,29 @@ public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
action.accept(offset, jdbcMapping);
return getJdbcTypeCount();
}

@Override
public Class<T> getJavaType() {
return returnedClass();
}

@Override
public boolean canDoExtraction() {
return true;
}

@Override
public JdbcType getJdbcType() {
return jdbcTypeDescriptor;
}

@Override
public T extract(CallableStatement callableStatement, int position, SharedSessionContractImplementor session) throws SQLException {
return jdbcTypeDescriptor.getExtractor(javaTypeDescriptor).extract(callableStatement, position, session);
}

@Override
public T extract(CallableStatement callableStatement, String position, SharedSessionContractImplementor session) throws SQLException {
return jdbcTypeDescriptor.getExtractor(javaTypeDescriptor).extract(callableStatement, position, session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import io.hypersistence.utils.hibernate.type.util.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.jdbc.JdbcType;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.io.Serializable;
import java.sql.*;

/**
* Maps a {@link String} object type to a PostgreSQL <a href="https://www.postgresql.org/docs/current/citext.html">citext</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.hypersistence.utils.hibernate.type.interval;

import io.hypersistence.utils.hibernate.type.array.EnumArrayType;
import io.hypersistence.utils.hibernate.type.array.ListArrayTypeTest;
import io.hypersistence.utils.hibernate.type.model.BaseEntity;
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import org.hibernate.annotations.Type;
import org.hibernate.jpa.boot.spi.TypeContributorList;
import org.hibernate.query.NativeQuery;
import org.junit.Test;

import java.time.Period;
import java.util.Collections;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

Expand All @@ -23,6 +29,16 @@ protected Class<?>[] entities() {
return new Class[]{WorkShift.class};
}

@Override
protected void additionalProperties(Properties properties) {
properties.put("hibernate.type_contributors",
(TypeContributorList) () -> Collections.singletonList(
(typeContributions, serviceRegistry) -> {
typeContributions.contributeType(PostgreSQLPeriodType.INSTANCE);
}
));
}

@Test
public void test() {
Period duration = Period.of(1, 2, 3);
Expand All @@ -39,6 +55,18 @@ public void test() {
WorkShift result = entityManager.find(WorkShift.class, 1L);
assertEquals(duration, result.getDuration());
});

doInJPA(entityManager -> {
Object result = entityManager.createNativeQuery(
"select duration " +
"from WorkShift " +
"where id = :id")
.setParameter("id", 1L)
.unwrap(NativeQuery.class)
.addScalar("duration", Period.class)
.getSingleResult();
assertEquals(duration, result);
});
}

@Entity(name = "WorkShift")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class OracleJsonStringPropertyTest extends AbstractOracleIntegrationTest
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Book.class
Book.class
};
}

Expand Down Expand Up @@ -97,6 +97,22 @@ public void test() {

assertEquals("High-Performance Java Persistence", properties.get("title").asText());
});

doInJPA(entityManager -> {
JsonNode properties = (JsonNode) entityManager
.createNativeQuery(
"SELECT " +
" properties AS properties " +
"FROM book " +
"WHERE " +
" isbn = :isbn")
.setParameter("isbn", "978-9730228236")
.unwrap(NativeQuery.class)
.addScalar("properties", JsonStringType.INSTANCE)
.getSingleResult();

assertEquals("High-Performance Java Persistence", properties.get("title").asText());
});
}

@Test
Expand Down

0 comments on commit 60b2ced

Please sign in to comment.