HHH-18709 Add test for issue

This commit is contained in:
Marco Belladelli 2024-10-29 11:13:21 +01:00
parent e0c95b77d6
commit 2fa1765f5c
1 changed files with 79 additions and 44 deletions

View File

@ -4,48 +4,51 @@
*/
package org.hibernate.orm.test.mapping.basic;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.json.JsonValue;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Root;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.community.dialect.AltibaseDialect;
import org.hibernate.community.dialect.DerbyDialect;
import org.hibernate.dialect.HANADialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.MutationQuery;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Clob;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.json.JsonValue;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.community.dialect.AltibaseDialect;
import org.hibernate.dialect.HANADialect;
import org.hibernate.community.dialect.DerbyDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@ -120,7 +123,8 @@ public abstract class JsonMappingTests {
"objectMap" );
final BasicAttributeMapping listAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping(
"list" );
final BasicAttributeMapping jsonAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping( "jsonString" );
final BasicAttributeMapping jsonAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping(
"jsonString" );
assertThat( stringMapAttribute.getJavaType().getJavaTypeClass(), equalTo( Map.class ) );
assertThat( objectMapAttribute.getJavaType().getJavaTypeClass(), equalTo( Map.class ) );
@ -187,11 +191,16 @@ public abstract class JsonMappingTests {
}
@Test
@SkipForDialect(dialectClass = DerbyDialect.class, reason = "Derby doesn't support comparing CLOBs with the = operator")
@SkipForDialect(dialectClass = HANADialect.class, matchSubTypes = true, reason = "HANA doesn't support comparing LOBs with the = operator")
@SkipForDialect(dialectClass = SybaseDialect.class, matchSubTypes = true, reason = "Sybase doesn't support comparing LOBs with the = operator")
@SkipForDialect(dialectClass = OracleDialect.class, matchSubTypes = true, reason = "Oracle doesn't support comparing JSON with the = operator")
@SkipForDialect(dialectClass = AltibaseDialect.class, reason = "Altibase doesn't support comparing CLOBs with the = operator")
@SkipForDialect(dialectClass = DerbyDialect.class,
reason = "Derby doesn't support comparing CLOBs with the = operator")
@SkipForDialect(dialectClass = HANADialect.class, matchSubTypes = true,
reason = "HANA doesn't support comparing LOBs with the = operator")
@SkipForDialect(dialectClass = SybaseDialect.class, matchSubTypes = true,
reason = "Sybase doesn't support comparing LOBs with the = operator")
@SkipForDialect(dialectClass = OracleDialect.class, matchSubTypes = true,
reason = "Oracle doesn't support comparing JSON with the = operator")
@SkipForDialect(dialectClass = AltibaseDialect.class,
reason = "Altibase doesn't support comparing CLOBs with the = operator")
public void verifyComparisonWorks(SessionFactoryScope scope) {
scope.inTransaction(
(session) -> {
@ -239,6 +248,32 @@ public abstract class JsonMappingTests {
);
}
@Test
@Jira("https://hibernate.atlassian.net/browse/HHH-18709")
public void verifyCriteriaUpdateQueryWorks(SessionFactoryScope scope) {
final Map<String, String> newMap = Map.of( "name", "ABC" );
final List<StringNode> newList = List.of( new StringNode( "ABC" ) );
final String newJson = "{\"count\":123}";
scope.inTransaction( session -> {
final HibernateCriteriaBuilder builder = session.getCriteriaBuilder();
final CriteriaUpdate<EntityWithJson> criteria = builder.createCriteriaUpdate( EntityWithJson.class );
final Root<EntityWithJson> root = criteria.from( EntityWithJson.class );
criteria.set( root.get( "stringMap" ), newMap );
criteria.set( "list", newList );
criteria.set( root.get( "jsonString" ), newJson );
criteria.where( builder.equal( root.get( "id" ), 1 ) );
final MutationQuery query = session.createMutationQuery( criteria );
final int count = query.executeUpdate();
assertThat( count, is( 1 ) );
} );
scope.inSession( session -> {
final EntityWithJson entityWithJson = session.find( EntityWithJson.class, 1 );
assertThat( entityWithJson.stringMap, is( newMap ) );
assertThat( entityWithJson.list, is( newList ) );
assertThat( entityWithJson.jsonString.replaceAll( "\\s", "" ), is( newJson ) );
} );
}
@Entity(name = "EntityWithJson")
@Table(name = "EntityWithJson")
public static class EntityWithJson {