From 40bcb9723263cb2e69e4e4d29019950a961a909a Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 16 Feb 2022 12:11:05 +0100 Subject: [PATCH] Fix String index out of range when wrapping an char --- .../descriptor/java/CharacterJavaType.java | 5 +- .../orm/test/type/CharacterTypeTest.java | 87 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/CharacterTypeTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java index d8d3de2b26..15041a34d5 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java @@ -62,7 +62,10 @@ public class CharacterJavaType extends AbstractClassJavaType implemen if (value instanceof Character) { return (Character) value; } - if (value instanceof String) { + if ( value instanceof String ) { + if ( value.equals( "" ) ) { + return ' '; + } final String str = (String) value; return str.charAt( 0 ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/CharacterTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/CharacterTypeTest.java new file mode 100644 index 0000000000..c4c4e610ec --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/CharacterTypeTest.java @@ -0,0 +1,87 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.type; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +@DomainModel( + annotatedClasses = CharacterTypeTest.TestEntity.class +) +@SessionFactory +public class CharacterTypeTest { + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + TestEntity dataTypes = new TestEntity( 1, ' ' ); + session.persist( dataTypes ); + } + ); + } + + @Test + public void transientTest(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + TestEntity d1 = session.find( TestEntity.class, 1 ); + assertNotNull( d1 ); + assertEquals( ' ', d1.getCharacterData() ); + d1.setCharacterData( null ); + } + ); + + scope.inTransaction( + session -> { + TestEntity d1 = session.find( TestEntity.class, 1 ); + assertNotNull( d1 ); + assertNull( d1.getCharacterData() ); + } + ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + + @Id + private Integer id; + + private Character characterData; + + public TestEntity() { + } + + public TestEntity(Integer id, Character characterData) { + this.id = id; + this.characterData = characterData; + } + + public Integer getId() { + return id; + } + + public void setCharacterData(Character characterData) { + this.characterData = characterData; + } + + public Character getCharacterData() { + return characterData; + } + } + +}