|
| 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 | +} |
0 commit comments