Skip to content

Commit 11df130

Browse files
committed
HHH-17404 : add schema compatibility test
1 parent 67742fb commit 11df130

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/OracleOsonJacksonJdbcType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) thro
182182
OracleJsonDatum ojd = rs.getObject( paramIndex, OracleJsonDatum.class );
183183
return doExtraction(ojd,options);
184184
} catch (SQLException exc) {
185-
if ( exc.getErrorCode() == DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
185+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
186186
// this may happen if we are fetching data from an existing schema
187187
// that use CBLOB for JSON column In that case we assume byte are
188188
// UTF-8 bytes (i.e not OSON)
@@ -203,7 +203,7 @@ protected X doExtract(CallableStatement statement, int index, WrapperOptions opt
203203
OracleJsonDatum ojd = statement.getObject( index, OracleJsonDatum.class );
204204
return doExtraction(ojd,options);
205205
} catch (SQLException exc) {
206-
if ( exc.getErrorCode() == DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
206+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
207207
// this may happen if we are fetching data from an existing schema
208208
// that use CBLOB for JSON column. In that case we assume byte are
209209
// UTF-8 bytes (i.e not OSON)
@@ -225,7 +225,7 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
225225
OracleJsonDatum ojd = statement.getObject( name, OracleJsonDatum.class );
226226
return doExtraction(ojd,options);
227227
} catch (SQLException exc) {
228-
if ( exc.getErrorCode() == DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
228+
if ( exc.getErrorCode() == DatabaseError.JDBC_ERROR_BASE + DatabaseError.EOJ_INVALID_COLUMN_TYPE) {
229229
// this may happen if we are fetching data from an existing schema
230230
// that use CBLOB for JSON column In that case we assume byte are
231231
// // UTF-8 bytes (i.e not OSON)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.hhh17404;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Table;
10+
import org.hibernate.annotations.JdbcTypeCode;
11+
import org.hibernate.dialect.OracleDialect;
12+
import org.hibernate.orm.test.mapping.basic.JsonMappingTests;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.RequiresDialect;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.hibernate.type.SqlTypes;
18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hibernate.testing.orm.junit.DialectContext.getDialect;
26+
27+
/**
28+
* This test class is about testing that legacy schema that use BLO for JSON column
29+
* can be safely read even when Oracle Oson extention is in place.
30+
* In Such a situation, the JSON type will expect JSON a JSON column and should
31+
* silently fall back to String deserialization.
32+
*
33+
* @author Emmanuel Jannetti
34+
*/
35+
@DomainModel(annotatedClasses = JsonCBLOBToOsonTest.JsonEntity.class)
36+
@SessionFactory
37+
@RequiresDialect( value = OracleDialect.class, majorVersion = 23 )
38+
public class JsonCBLOBToOsonTest {
39+
40+
@Entity(name = "JsonEntity")
41+
@Table(name = "TEST_OSON_COMPAT")
42+
public static class JsonEntity {
43+
@Id
44+
private Integer id;
45+
@JdbcTypeCode( SqlTypes.JSON )
46+
private JsonMappingTests.StringNode jsonName;
47+
48+
public JsonEntity() {
49+
super();
50+
}
51+
public JsonEntity(Integer id, JsonMappingTests.StringNode node) {
52+
this.id = id;
53+
this.jsonName = node;
54+
}
55+
}
56+
57+
@BeforeEach
58+
public void setup(SessionFactoryScope scope) {
59+
scope.inTransaction(
60+
(session) -> {
61+
// force creation of a BLOB column type by creating the table ourselves
62+
session.createNativeQuery( getDialect().getDropTableString( "TEST_OSON_COMPAT" ) )
63+
.executeUpdate();
64+
session.createNativeQuery( "CREATE TABLE TEST_OSON_COMPAT (id NUMBER, jsonName BLOB CHECK (jsonName is json) ,primary key (id))" )
65+
.executeUpdate();
66+
67+
String insert = "INSERT INTO TEST_OSON_COMPAT (id, jsonName) VALUES(:id,:json)";
68+
String jsonstr = "{\"string\":\"john\"}";
69+
session.createNativeQuery(insert).setParameter("id",1)
70+
.setParameter( "json", jsonstr).executeUpdate();
71+
}
72+
);
73+
}
74+
75+
@AfterEach
76+
public void tearDown(SessionFactoryScope scope) {
77+
scope.inTransaction(
78+
(session) -> {
79+
session.createNativeQuery( getDialect().getDropTableString( "TEST_OSON_COMPAT" ) ).executeUpdate();
80+
}
81+
);
82+
}
83+
84+
@Test
85+
public void verifyReadWorks(SessionFactoryScope scope) {
86+
scope.inTransaction(
87+
(session) -> {
88+
JsonEntity entity = session.find( JsonCBLOBToOsonTest.JsonEntity.class, 1 );
89+
assertThat( entity.jsonName.getString(), is( "john" ) );
90+
91+
}
92+
);
93+
}
94+
95+
}

hibernate-core/src/test/java/org/hibernate/orm/test/util/StringJsonDocumentReaderTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,39 @@ public void testNestedArrayDocument() {
381381
assertEquals( JsonDocumentItemType.OBJECT_END, reader.next());
382382
}
383383

384+
@Test
385+
public void testUnicode() {
386+
final StringJsonDocumentReader reader = new StringJsonDocumentReader( """
387+
{
388+
"myUnicode1": "\\u0074\\u0068\\u0069\\u0073\\u005f\\u0069\\u0073\\u005f\\u0075\\u006e\\u0069\\u0063\\u006f\\u0064\\u0065",
389+
"myUnicode2": "this_\\u0069\\u0073_unicode",
390+
"myUnicode3": "this_is_unicode"
391+
}
392+
393+
394+
""");
395+
396+
assertTrue(reader.hasNext(), "should have more element");
397+
assertEquals( JsonDocumentItemType.OBJECT_START, reader.next());
398+
399+
assertEquals( JsonDocumentItemType.VALUE_KEY,reader.next());
400+
assertEquals("myUnicode1", reader.getObjectKeyName());
401+
assertEquals( JsonDocumentItemType.VALUE, reader.next());
402+
assertEquals("this_is_unicode", reader.getStringValue());
403+
404+
assertEquals( JsonDocumentItemType.VALUE_KEY,reader.next());
405+
assertEquals("myUnicode2", reader.getObjectKeyName());
406+
assertEquals( JsonDocumentItemType.VALUE, reader.next());
407+
assertEquals("this_is_unicode", reader.getStringValue());
408+
409+
assertEquals( JsonDocumentItemType.VALUE_KEY,reader.next());
410+
assertEquals("myUnicode3", reader.getObjectKeyName());
411+
assertEquals( JsonDocumentItemType.VALUE, reader.next());
412+
assertEquals("this_is_unicode", reader.getStringValue());
413+
414+
assertEquals( JsonDocumentItemType.OBJECT_END, reader.next());
415+
416+
}
417+
418+
384419
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ dependencyResolutionManagement {
226226
def mssqlVersion = version "mssql", "12.8.1.jre11"
227227
def mysqlVersion = version "mysql", "9.1.0"
228228
def oracleVersion = version "oracle", "23.7.0.25.01"
229-
def oracleJacksonOsonExtension = version "oracleJacksonOsonExtension", "1.0.3"
229+
def oracleJacksonOsonExtension = version "oracleJacksonOsonExtension", "1.0.4"
230230
def pgsqlVersion = version "pgsql", "42.7.4"
231231
def sybaseVersion = version "sybase", "1.3.1"
232232
def tidbVersion = version "tidb", mysqlVersion

0 commit comments

Comments
 (0)