1111import jakarta .persistence .Table ;
1212import jakarta .persistence .criteria .CriteriaUpdate ;
1313import jakarta .persistence .criteria .Root ;
14+ import org .assertj .core .api .AssertionsForClassTypes ;
1415import org .hibernate .annotations .JdbcTypeCode ;
1516import org .hibernate .cfg .AvailableSettings ;
1617import org .hibernate .community .dialect .AltibaseDialect ;
4243import java .nio .charset .StandardCharsets ;
4344import java .sql .Blob ;
4445import java .sql .Clob ;
46+ import java .util .ArrayDeque ;
47+ import java .util .Dictionary ;
48+ import java .util .Hashtable ;
4549import java .util .List ;
4650import java .util .Map ;
51+ import java .util .Queue ;
4752import java .util .Set ;
4853
4954import static org .hamcrest .MatcherAssert .assertThat ;
5358import static org .hamcrest .Matchers .isOneOf ;
5459import static org .hamcrest .Matchers .notNullValue ;
5560import 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
6370public 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