Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
b29620f2
Commit
b29620f2
authored
Dec 07, 2016
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow overriding case in SpringPhysicalNamingStrategy
Fixes gh-7410
parent
5d7453a6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
16 deletions
+54
-16
SpringPhysicalNamingStrategy.java
.../boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java
+27
-12
SpringPhysicalNamingStrategyTests.java
.../orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java
+27
-4
No files found.
spring-boot/src/main/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategy.java
View file @
b29620f2
...
...
@@ -27,6 +27,7 @@ import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
* conventions.
*
* @author Phillip Webb
* @author Madhura Bhave
* @since 1.4.0
*/
public
class
SpringPhysicalNamingStrategy
implements
PhysicalNamingStrategy
{
...
...
@@ -34,45 +35,59 @@ public class SpringPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public
Identifier
toPhysicalCatalogName
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
return
apply
(
name
);
return
apply
(
name
,
jdbcEnvironment
);
}
@Override
public
Identifier
toPhysicalSchemaName
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
return
apply
(
name
);
return
apply
(
name
,
jdbcEnvironment
);
}
@Override
public
Identifier
toPhysicalTableName
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
return
apply
(
name
);
return
apply
(
name
,
jdbcEnvironment
);
}
@Override
public
Identifier
toPhysicalSequenceName
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
return
apply
(
name
);
return
apply
(
name
,
jdbcEnvironment
);
}
@Override
public
Identifier
toPhysicalColumnName
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
return
apply
(
name
);
return
apply
(
name
,
jdbcEnvironment
);
}
private
Identifier
apply
(
Identifier
name
)
{
private
Identifier
apply
(
Identifier
name
,
JdbcEnvironment
jdbcEnvironment
)
{
if
(
name
==
null
)
{
return
null
;
}
StringBuilder
text
=
new
StringBuilder
(
name
.
getText
().
replace
(
'.'
,
'_'
));
for
(
int
i
=
1
;
i
<
text
.
length
()
-
1
;
i
++)
{
if
(
isUnderscoreRequired
(
text
.
charAt
(
i
-
1
),
text
.
charAt
(
i
),
text
.
charAt
(
i
+
1
)))
{
text
.
insert
(
i
++,
'_'
);
StringBuilder
builder
=
new
StringBuilder
(
name
.
getText
().
replace
(
'.'
,
'_'
));
for
(
int
i
=
1
;
i
<
builder
.
length
()
-
1
;
i
++)
{
if
(
isUnderscoreRequired
(
builder
.
charAt
(
i
-
1
),
builder
.
charAt
(
i
),
builder
.
charAt
(
i
+
1
)))
{
builder
.
insert
(
i
++,
'_'
);
}
}
return
new
Identifier
(
text
.
toString
().
toLowerCase
(
Locale
.
ROOT
),
name
.
isQuoted
());
String
text
=
builder
.
toString
();
String
finalText
=
isCaseInsensitive
(
jdbcEnvironment
)
?
text
.
toLowerCase
(
Locale
.
ROOT
)
:
text
;
return
new
Identifier
(
finalText
,
name
.
isQuoted
());
}
/**
* Specify whether the database is case sensitive.
* @param jdbcEnvironment The JDBC environment which can be used to determine case
* @return true if the database is case insensitive
* sensitivity
*/
protected
boolean
isCaseInsensitive
(
JdbcEnvironment
jdbcEnvironment
)
{
return
true
;
}
private
boolean
isUnderscoreRequired
(
char
before
,
char
current
,
char
after
)
{
...
...
spring-boot/src/test/java/org/springframework/boot/orm/jpa/hibernate/SpringPhysicalNamingStrategyTests.java
View file @
b29620f2
...
...
@@ -23,6 +23,7 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import
org.hibernate.boot.registry.StandardServiceRegistryBuilder
;
import
org.hibernate.cfg.AvailableSettings
;
import
org.hibernate.dialect.H2Dialect
;
import
org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
;
import
org.hibernate.mapping.PersistentClass
;
import
org.hibernate.service.ServiceRegistry
;
import
org.junit.Before
;
...
...
@@ -34,17 +35,22 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link SpringPhysicalNamingStrategy}.
*
* @author Phillip Webb
* @author Madhura Bhave
*/
public
class
SpringPhysicalNamingStrategyTests
{
private
Metadata
metadata
;
private
MetadataSources
metadataSources
;
private
StandardServiceRegistry
serviceRegistry
;
@Before
public
void
setup
()
throws
Exception
{
MetadataSources
metadataSources
=
new
MetadataSources
();
metadataSources
.
addAnnotatedClass
(
TelephoneNumber
.
class
);
StandardServiceRegistry
serviceRegistry
=
getServiceRegistry
(
metadataSources
);
this
.
metadata
=
metadataSources
.
getMetadataBuilder
(
serviceRegistry
)
this
.
metadataSources
=
new
MetadataSources
();
this
.
metadataSources
.
addAnnotatedClass
(
TelephoneNumber
.
class
);
this
.
serviceRegistry
=
getServiceRegistry
(
this
.
metadataSources
);
this
.
metadata
=
this
.
metadataSources
.
getMetadataBuilder
(
this
.
serviceRegistry
)
.
applyPhysicalNamingStrategy
(
new
SpringPhysicalNamingStrategy
()).
build
();
}
...
...
@@ -61,4 +67,21 @@ public class SpringPhysicalNamingStrategyTests {
assertThat
(
binding
.
getTable
().
getQuotedName
()).
isEqualTo
(
"telephone_number"
);
}
@Test
public
void
tableNameShouldNotBeLowerCaseIfCaseSensitive
()
throws
Exception
{
this
.
metadata
=
this
.
metadataSources
.
getMetadataBuilder
(
this
.
serviceRegistry
)
.
applyPhysicalNamingStrategy
(
new
TestSpringPhysicalNamingStrategy
()).
build
();
PersistentClass
binding
=
this
.
metadata
.
getEntityBinding
(
TelephoneNumber
.
class
.
getName
());
assertThat
(
binding
.
getTable
().
getQuotedName
()).
isEqualTo
(
"Telephone_Number"
);
}
private
class
TestSpringPhysicalNamingStrategy
extends
SpringPhysicalNamingStrategy
{
@Override
protected
boolean
isCaseInsensitive
(
JdbcEnvironment
jdbcEnvironment
)
{
return
false
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment