Fix String index out of range when wrapping an char

This commit is contained in:
Andrea Boriero 2022-02-16 12:11:05 +01:00 committed by Christian Beikov
parent cc750a9abd
commit 40bcb97232
2 changed files with 91 additions and 1 deletions

View File

@ -62,7 +62,10 @@ public class CharacterJavaType extends AbstractClassJavaType<Character> 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 );
}

View File

@ -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;
}
}
}