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,75 @@ public static class Jackson extends JsonMappingTests {
7683 public Jackson () {
7784 super ( false );
7885 }
86+
87+ @ Test
88+ @ JiraKey ( "https://hibernate.atlassian.net/browse/HHH-19969" )
89+ public void jsonMappedToObjectTest (SessionFactoryScope scope ) {
90+ scope .inTransaction (
91+ session -> {
92+ var entity = new EntityWithObjectJson ();
93+ entity .id = 1L ;
94+ entity .json = Map .of ("a" , 1 , "b" , 2 );
95+ session .persist (entity );
96+
97+ entity = new EntityWithObjectJson ();
98+ entity .id = 2L ;
99+ entity .json = List .<Object >of ("c" , 11 , 22 , "d" );
100+ session .persist (entity );
101+
102+ entity = new EntityWithObjectJson ();
103+ entity .id = 3L ;
104+ entity .json = Set .<Object >of ("s1" , 2 , "s3" );
105+ session .persist (entity );
106+
107+ entity = new EntityWithObjectJson ();
108+ entity .id = 4L ;
109+ Queue <Integer > ad = new ArrayDeque <>();
110+ ad .add (2 );
111+ ad .add (1 );
112+ ad .add (3 );
113+ entity .json = ad ;
114+ session .persist (entity );
115+
116+ entity = new EntityWithObjectJson ();
117+ entity .id = 5L ;
118+ Dictionary <Integer , String > ht = new Hashtable <>();
119+ ht .put (1 , "one" );
120+ ht .put (2 , "two" );
121+ ht .put (3 , "three" );
122+ entity .json = ht ;
123+ session .persist (entity );
124+ }
125+ );
126+ scope .inTransaction (
127+ session -> {
128+ var entity = session .find ( EntityWithObjectJson .class , 1L );
129+ AssertionsForClassTypes .assertThat ( entity ).isNotNull ();
130+ AssertionsForClassTypes .assertThat ( entity .json ).isInstanceOf ( Map .class );
131+ assertEquals ( 2 , ((Map <?,?>)entity .json ).size () );
132+
133+ entity = session .find ( EntityWithObjectJson .class , 2L );
134+ AssertionsForClassTypes .assertThat ( entity ).isNotNull ();
135+ AssertionsForClassTypes .assertThat ( entity .json ).isInstanceOf ( List .class );
136+ assertEquals ( 4 , ((List <?>)entity .json ).size () );
137+
138+ entity = session .find ( EntityWithObjectJson .class , 3L );
139+ AssertionsForClassTypes .assertThat ( entity ).isNotNull ();
140+ AssertionsForClassTypes .assertThat ( entity .json ).isInstanceOf ( List .class );
141+ assertEquals ( 3 , ((List <?>)entity .json ).size () );
142+
143+ entity = session .find ( EntityWithObjectJson .class , 4L );
144+ AssertionsForClassTypes .assertThat ( entity ).isNotNull ();
145+ AssertionsForClassTypes .assertThat ( entity .json ).isInstanceOf ( List .class );
146+ assertEquals ( 3 , ((List <?>)entity .json ).size () );
147+
148+ entity = session .find ( EntityWithObjectJson .class , 5L );
149+ AssertionsForClassTypes .assertThat ( entity ).isNotNull ();
150+ AssertionsForClassTypes .assertThat ( entity .json ).isInstanceOf ( Map .class );
151+ assertEquals ( 3 , ((Map <?,?>)entity .json ).size () );
152+ }
153+ );
154+ }
79155 }
80156
81157 @ ServiceRegistry (settings = @ Setting (name = AvailableSettings .JSON_FORMAT_MAPPER , value = "jackson3" ))
@@ -238,15 +314,13 @@ public void verifyComparisonWorks(SessionFactoryScope scope) {
238314 .get ( 0 );
239315 final String jsonText ;
240316 try {
241- if ( nativeJson instanceof Blob ) {
242- final Blob blob = (Blob ) nativeJson ;
317+ if ( nativeJson instanceof Blob blob ) {
243318 jsonText = new String (
244319 blob .getBytes ( 1L , (int ) blob .length () ),
245320 StandardCharsets .UTF_8
246321 );
247322 }
248- else if ( nativeJson instanceof Clob ) {
249- final Clob jsonClob = (Clob ) nativeJson ;
323+ else if ( nativeJson instanceof Clob jsonClob ) {
250324 jsonText = jsonClob .getSubString ( 1L , (int ) jsonClob .length () );
251325 }
252326 else {
@@ -376,4 +450,21 @@ public int hashCode() {
376450 return string != null ? string .hashCode () : 0 ;
377451 }
378452 }
453+
454+ @ Entity
455+ public static class EntityWithObjectJson {
456+ @ Id
457+ long id ;
458+
459+ @ JdbcTypeCode (JSON )
460+ Object json ;
461+
462+ public EntityWithObjectJson () {
463+ }
464+
465+ public EntityWithObjectJson (long id , Object json ) {
466+ this .id = id ;
467+ this .json = json ;
468+ }
469+ }
379470}
0 commit comments