Skip to content

Commit 3159c80

Browse files
Anilabha Baralgavinking
authored andcommitted
HHH-17310: Bug with names with digits in CamelCaseToUnderscoresNamingStrategy
1 parent 850d266 commit 3159c80

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/naming/CamelCaseToUnderscoresNamingStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
8686
}
8787

8888
private boolean isUnderscoreRequired(final char before, final char current, final char after) {
89-
return Character.isLowerCase( before ) && Character.isUpperCase( current ) && Character.isLowerCase( after );
89+
return ( Character.isLowerCase( before ) || Character.isDigit( before ) ) && Character.isUpperCase( current ) && ( Character.isLowerCase(
90+
after ) || Character.isDigit( after ) );
9091
}
9192

9293
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.hibernate.orm.test.annotations.namingstrategy;
2+
3+
import org.hibernate.boot.Metadata;
4+
import org.hibernate.boot.MetadataSources;
5+
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
6+
import org.hibernate.cfg.Environment;
7+
import org.hibernate.mapping.PersistentClass;
8+
import org.hibernate.mapping.Selectable;
9+
import org.hibernate.service.ServiceRegistry;
10+
11+
import org.hibernate.testing.ServiceRegistryBuilder;
12+
import org.hibernate.testing.junit4.BaseUnitTestCase;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.Table;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
/**
24+
* Test harness for HHH-17310.
25+
*
26+
* @author Anilabha Baral
27+
*/
28+
public class CamelCaseToUnderscoresNamingStrategyTest extends BaseUnitTestCase {
29+
30+
private ServiceRegistry serviceRegistry;
31+
32+
@Before
33+
public void setUp() {
34+
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
if ( serviceRegistry != null ) {
40+
ServiceRegistryBuilder.destroy( serviceRegistry );
41+
}
42+
}
43+
44+
@Test
45+
public void testWithWordWithDigitNamingStrategy() throws Exception {
46+
Metadata metadata = new MetadataSources( serviceRegistry )
47+
.addAnnotatedClass( B.class )
48+
.getMetadataBuilder()
49+
.applyPhysicalNamingStrategy( new CamelCaseToUnderscoresNamingStrategy() )
50+
.build();
51+
52+
PersistentClass entityBinding = metadata.getEntityBinding( B.class.getName() );
53+
assertEquals(
54+
"word_with_digit_d1",
55+
( (Selectable) entityBinding.getProperty( "wordWithDigitD1" ).getSelectables().get( 0 ) ).getText()
56+
);
57+
assertEquals(
58+
"abcd_efgh_i21",
59+
( (Selectable) entityBinding.getProperty( "AbcdEfghI21" ).getSelectables().get( 0 ) ).getText()
60+
);
61+
assertEquals(
62+
"hello1",
63+
( (Selectable) entityBinding.getProperty( "hello1" ).getSelectables().get( 0 ) ).getText()
64+
);
65+
assertEquals(
66+
"hello1_d2",
67+
( (Selectable) entityBinding.getProperty( "hello1D2" ).getSelectables().get( 0 ) ).getText()
68+
);
69+
assertEquals(
70+
"hello3d4",
71+
( (Selectable) entityBinding.getProperty( "hello3d4" ).getSelectables().get( 0 ) ).getText()
72+
);
73+
}
74+
75+
@Entity
76+
@Table(name = "ABCD")
77+
class B implements java.io.Serializable {
78+
@Id
79+
protected String AbcdEfghI21;
80+
protected String wordWithDigitD1;
81+
protected String hello1;
82+
protected String hello1D2;
83+
protected String hello3d4;
84+
85+
public String getAbcdEfghI21() {
86+
return AbcdEfghI21;
87+
}
88+
89+
public void setAbcdEfghI21(String abcdEfghI21) {
90+
AbcdEfghI21 = abcdEfghI21;
91+
}
92+
93+
public String getWordWithDigitD1() {
94+
return wordWithDigitD1;
95+
}
96+
97+
public void setWordWithDigitD1(String wordWithDigitD1) {
98+
this.wordWithDigitD1 = wordWithDigitD1;
99+
}
100+
101+
public String getHello1() {
102+
return hello1;
103+
}
104+
105+
public void setHello1(String hello1) {
106+
this.hello1 = hello1;
107+
}
108+
109+
public String getHello1D2() {
110+
return hello1D2;
111+
}
112+
113+
public void setHello1D2(String hello1D2) {
114+
this.hello1D2 = hello1D2;
115+
}
116+
117+
public String getHello3d4() {
118+
return hello3d4;
119+
}
120+
121+
public void setHello3d4(String hello3d4) {
122+
this.hello3d4 = hello3d4;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)