Skip to content

Commit 40bcb97

Browse files
dreab8beikov
authored andcommitted
Fix String index out of range when wrapping an char
1 parent cc750a9 commit 40bcb97

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public <X> Character wrap(X value, WrapperOptions options) {
6262
if (value instanceof Character) {
6363
return (Character) value;
6464
}
65-
if (value instanceof String) {
65+
if ( value instanceof String ) {
66+
if ( value.equals( "" ) ) {
67+
return ' ';
68+
}
6669
final String str = (String) value;
6770
return str.charAt( 0 );
6871
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.type;
8+
9+
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.SessionFactory;
11+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import jakarta.persistence.Entity;
16+
import jakarta.persistence.Id;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
22+
@DomainModel(
23+
annotatedClasses = CharacterTypeTest.TestEntity.class
24+
)
25+
@SessionFactory
26+
public class CharacterTypeTest {
27+
28+
@BeforeEach
29+
public void setUp(SessionFactoryScope scope) {
30+
scope.inTransaction(
31+
session -> {
32+
TestEntity dataTypes = new TestEntity( 1, ' ' );
33+
session.persist( dataTypes );
34+
}
35+
);
36+
}
37+
38+
@Test
39+
public void transientTest(SessionFactoryScope scope) {
40+
scope.inTransaction(
41+
session -> {
42+
TestEntity d1 = session.find( TestEntity.class, 1 );
43+
assertNotNull( d1 );
44+
assertEquals( ' ', d1.getCharacterData() );
45+
d1.setCharacterData( null );
46+
}
47+
);
48+
49+
scope.inTransaction(
50+
session -> {
51+
TestEntity d1 = session.find( TestEntity.class, 1 );
52+
assertNotNull( d1 );
53+
assertNull( d1.getCharacterData() );
54+
}
55+
);
56+
}
57+
58+
@Entity(name = "TestEntity")
59+
public static class TestEntity {
60+
61+
@Id
62+
private Integer id;
63+
64+
private Character characterData;
65+
66+
public TestEntity() {
67+
}
68+
69+
public TestEntity(Integer id, Character characterData) {
70+
this.id = id;
71+
this.characterData = characterData;
72+
}
73+
74+
public Integer getId() {
75+
return id;
76+
}
77+
78+
public void setCharacterData(Character characterData) {
79+
this.characterData = characterData;
80+
}
81+
82+
public Character getCharacterData() {
83+
return characterData;
84+
}
85+
}
86+
87+
}

0 commit comments

Comments
 (0)