Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class AbstractJsonFormatMapper implements FormatMapper {
@Override
public final <T> T fromString(CharSequence charSequence, JavaType<T> javaType, WrapperOptions wrapperOptions) {
final Type type = javaType.getJavaType();
if ( type == String.class || type == Object.class ) {
if ( type == String.class ) {
return (T) charSequence.toString();
}
return fromString( charSequence, type );
Expand All @@ -27,7 +27,7 @@ public final <T> T fromString(CharSequence charSequence, JavaType<T> javaType, W
@Override
public final <T> String toString(T value, JavaType<T> javaType, WrapperOptions wrapperOptions) {
final Type type = javaType.getJavaType();
if ( type == String.class || type == Object.class ) {
if ( type == String.class ) {
return (String) value;
}
return toString( value, type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import jakarta.persistence.Inheritance;
import jakarta.persistence.Table;

import java.util.Map;

import static jakarta.persistence.InheritanceType.SINGLE_TABLE;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hibernate.type.SqlTypes.JSON;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DomainModel(
annotatedClasses = {
Expand All @@ -37,7 +40,7 @@ public class SingleTableAndGenericsTest {

@Test
public void testIt(SessionFactoryScope scope) {
String payload = "{\"book\": \"1\"}";
Map<?,?> payload = Map.of("book", "1");
String aId = "1";

scope.inTransaction(
Expand All @@ -53,9 +56,9 @@ public void testIt(SessionFactoryScope scope) {
session -> {
A a = session.find( A.class, aId );
assertThat( a ).isNotNull();
String payload1 = a.getPayload();
Map<?,?> payload1 = a.getPayload();
assertThat( payload1 ).isNotNull();
assertThat( payload1 ).contains( "book" );
assertTrue(payload1.containsKey("book") );
}
);
}
Expand Down Expand Up @@ -93,6 +96,9 @@ public void setPayload(T payload) {

@Entity(name = "C")
@DiscriminatorValue("child")
public static class A extends B<String> {
// Changed from <String> to <Map> since the fix for HHH-19969; the restriction '|| type == Object.class' inside
// AbstractFormatMapper.fromString() was removed, so now no cast to String happens, but instead the json is serialized
// to either Map or List (depending on the json format)
public static class A extends B<Map<?,?>> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.persistence.Table;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Root;
import org.assertj.core.api.AssertionsForClassTypes;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.community.dialect.AltibaseDialect;
Expand Down Expand Up @@ -42,8 +43,12 @@
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Clob;
import java.util.ArrayDeque;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -53,12 +58,14 @@
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hibernate.type.SqlTypes.JSON;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Christian Beikov
* @author Yanming Zhou
*/
@DomainModel(annotatedClasses = JsonMappingTests.EntityWithJson.class)
@DomainModel(annotatedClasses = {JsonMappingTests.EntityWithJson.class, JsonMappingTests.EntityWithObjectJson.class})
@SessionFactory
public abstract class JsonMappingTests {

Expand All @@ -76,6 +83,75 @@ public static class Jackson extends JsonMappingTests {
public Jackson() {
super( false );
}

@Test
@JiraKey( "https://hibernate.atlassian.net/browse/HHH-19969" )
public void jsonMappedToObjectTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
var entity = new EntityWithObjectJson();
entity.id = 1L;
entity.json = Map.of("a", 1, "b", 2);
session.persist(entity);

entity = new EntityWithObjectJson();
entity.id = 2L;
entity.json = List.<Object>of("c", 11, 22, "d");
session.persist(entity);

entity = new EntityWithObjectJson();
entity.id = 3L;
entity.json = Set.<Object>of("s1", 2, "s3");
session.persist(entity);

entity = new EntityWithObjectJson();
entity.id = 4L;
Queue<Integer> ad = new ArrayDeque<>();
ad.add(2);
ad.add(1);
ad.add(3);
entity.json = ad;
session.persist(entity);

entity = new EntityWithObjectJson();
entity.id = 5L;
Dictionary<Integer, String> ht = new Hashtable<>();
ht.put(1, "one");
ht.put(2, "two");
ht.put(3, "three");
entity.json = ht;
session.persist(entity);
}
);
scope.inTransaction(
session -> {
var entity = session.find( EntityWithObjectJson.class, 1L );
AssertionsForClassTypes.assertThat( entity ).isNotNull();
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( Map.class );
assertEquals( 2, ((Map<?,?>)entity.json).size() );

entity = session.find( EntityWithObjectJson.class, 2L );
AssertionsForClassTypes.assertThat( entity ).isNotNull();
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( List.class );
assertEquals( 4, ((List<?>)entity.json).size() );

entity = session.find( EntityWithObjectJson.class, 3L );
AssertionsForClassTypes.assertThat( entity ).isNotNull();
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( List.class );
assertEquals( 3, ((List<?>)entity.json).size() );

entity = session.find( EntityWithObjectJson.class, 4L );
AssertionsForClassTypes.assertThat( entity ).isNotNull();
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( List.class );
assertEquals( 3, ((List<?>)entity.json).size() );

entity = session.find( EntityWithObjectJson.class, 5L );
AssertionsForClassTypes.assertThat( entity ).isNotNull();
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( Map.class );
assertEquals( 3, ((Map<?,?>)entity.json).size() );
}
);
}
}

private final Map<String, String> stringMap;
Expand Down Expand Up @@ -228,15 +304,13 @@ public void verifyComparisonWorks(SessionFactoryScope scope) {
.get( 0 );
final String jsonText;
try {
if ( nativeJson instanceof Blob ) {
final Blob blob = (Blob) nativeJson;
if ( nativeJson instanceof Blob blob ) {
jsonText = new String(
blob.getBytes( 1L, (int) blob.length() ),
StandardCharsets.UTF_8
);
}
else if ( nativeJson instanceof Clob ) {
final Clob jsonClob = (Clob) nativeJson;
else if ( nativeJson instanceof Clob jsonClob ) {
jsonText = jsonClob.getSubString( 1L, (int) jsonClob.length() );
}
else {
Expand Down Expand Up @@ -363,4 +437,21 @@ public int hashCode() {
return string != null ? string.hashCode() : 0;
}
}

@Entity
public static class EntityWithObjectJson {
@Id
long id;

@JdbcTypeCode(JSON)
Object json;

public EntityWithObjectJson() {
}

public EntityWithObjectJson(long id, Object json) {
this.id = id;
this.json = json;
}
}
}