Skip to content

Commit d97ffb6

Browse files
committed
HHH-19964 - Add test for json serialization of Object valued attribute mapping
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 0d3e834 commit d97ffb6

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableAndGenericsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.hibernate.annotations.JdbcTypeCode;
88

99
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.FailureExpected;
1011
import org.hibernate.testing.orm.junit.JiraKey;
1112
import org.hibernate.testing.orm.junit.SessionFactory;
1213
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -36,6 +37,8 @@
3637
public class SingleTableAndGenericsTest {
3738

3839
@Test
40+
// just a trial, not meant to stay here!
41+
@FailureExpected
3942
public void testIt(SessionFactoryScope scope) {
4043
String payload = "{\"book\": \"1\"}";
4144
String aId = "1";

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/JsonMappingTests.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import jakarta.persistence.Table;
1212
import jakarta.persistence.criteria.CriteriaUpdate;
1313
import jakarta.persistence.criteria.Root;
14+
import org.assertj.core.api.AssertionsForClassTypes;
1415
import org.hibernate.annotations.JdbcTypeCode;
1516
import org.hibernate.cfg.AvailableSettings;
1617
import org.hibernate.community.dialect.AltibaseDialect;
@@ -42,8 +43,12 @@
4243
import java.nio.charset.StandardCharsets;
4344
import java.sql.Blob;
4445
import java.sql.Clob;
46+
import java.util.ArrayDeque;
47+
import java.util.Dictionary;
48+
import java.util.Hashtable;
4549
import java.util.List;
4650
import java.util.Map;
51+
import java.util.Queue;
4752
import java.util.Set;
4853

4954
import static org.hamcrest.MatcherAssert.assertThat;
@@ -53,12 +58,14 @@
5358
import static org.hamcrest.Matchers.isOneOf;
5459
import static org.hamcrest.Matchers.notNullValue;
5560
import static org.hamcrest.Matchers.nullValue;
61+
import static org.hibernate.type.SqlTypes.JSON;
62+
import static org.junit.jupiter.api.Assertions.assertEquals;
5663

5764
/**
5865
* @author Christian Beikov
5966
* @author Yanming Zhou
6067
*/
61-
@DomainModel(annotatedClasses = JsonMappingTests.EntityWithJson.class)
68+
@DomainModel(annotatedClasses = {JsonMappingTests.EntityWithJson.class, JsonMappingTests.EntityWithObjectJson.class})
6269
@SessionFactory
6370
public abstract class JsonMappingTests {
6471

@@ -76,6 +83,74 @@ public static class Jackson extends JsonMappingTests {
7683
public Jackson() {
7784
super( false );
7885
}
86+
87+
@Test
88+
public void jsonMappedToObjectTest(SessionFactoryScope scope) {
89+
scope.inTransaction(
90+
session -> {
91+
var entity = new EntityWithObjectJson();
92+
entity.id = 1L;
93+
entity.json = Map.of("a", 1, "b", 2);
94+
session.persist(entity);
95+
96+
entity = new EntityWithObjectJson();
97+
entity.id = 2L;
98+
entity.json = List.<Object>of("c", 11, 22, "d");
99+
session.persist(entity);
100+
101+
entity = new EntityWithObjectJson();
102+
entity.id = 3L;
103+
entity.json = Set.<Object>of("s1", 2, "s3");
104+
session.persist(entity);
105+
106+
entity = new EntityWithObjectJson();
107+
entity.id = 4L;
108+
Queue<Integer> ad = new ArrayDeque<>();
109+
ad.add(2);
110+
ad.add(1);
111+
ad.add(3);
112+
entity.json = ad;
113+
session.persist(entity);
114+
115+
entity = new EntityWithObjectJson();
116+
entity.id = 5L;
117+
Dictionary<Integer, String> ht = new Hashtable<>();
118+
ht.put(1, "one");
119+
ht.put(2, "two");
120+
ht.put(3, "three");
121+
entity.json = ht;
122+
session.persist(entity);
123+
}
124+
);
125+
scope.inTransaction(
126+
session -> {
127+
var entity = session.find( EntityWithObjectJson.class, 1L );
128+
AssertionsForClassTypes.assertThat( entity ).isNotNull();
129+
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( Map.class );
130+
assertEquals( 2, ((Map<?,?>)entity.json).size() );
131+
132+
entity = session.find( EntityWithObjectJson.class, 2L );
133+
AssertionsForClassTypes.assertThat( entity ).isNotNull();
134+
AssertionsForClassTypes.assertThat( entity.json ).isInstanceOf( List.class );
135+
assertEquals( 4, ((List<?>)entity.json).size() );
136+
137+
entity = session.find( EntityWithObjectJson.class, 3L );
138+
AssertionsForClassTypes.assertThat( entity ).isNotNull();
139+
// assertThat( entity.json ).isInstanceOf( Set.class );
140+
// assertEquals( 3, ((Set<?>)entity.json).size() );
141+
142+
entity = session.find( EntityWithObjectJson.class, 4L );
143+
AssertionsForClassTypes.assertThat( entity ).isNotNull();
144+
// assertThat( entity.json ).isInstanceOf( Queue.class );
145+
// assertEquals( 3, ((Queue<?>)entity.json).size() );
146+
147+
entity = session.find( EntityWithObjectJson.class, 5L );
148+
AssertionsForClassTypes.assertThat( entity ).isNotNull();
149+
// assertThat( entity.json ).isInstanceOf( Dictionary.class );
150+
// assertEquals( 3, ((Dictionary<?,?>)entity.json).size() );
151+
}
152+
);
153+
}
79154
}
80155

81156
private final Map<String, String> stringMap;
@@ -363,4 +438,21 @@ public int hashCode() {
363438
return string != null ? string.hashCode() : 0;
364439
}
365440
}
441+
442+
@Entity
443+
public static class EntityWithObjectJson {
444+
@Id
445+
long id;
446+
447+
@JdbcTypeCode(JSON)
448+
Object json;
449+
450+
public EntityWithObjectJson() {
451+
}
452+
453+
public EntityWithObjectJson(long id, Object json) {
454+
this.id = id;
455+
this.json = json;
456+
}
457+
}
366458
}

0 commit comments

Comments
 (0)