Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,11 @@ We'll have more to say about them in <<naming-strategies>>.
[[quoted-identifiers]]
=== Quoting SQL identifiers

By default, Hibernate never quotes SQL table and column names in generated SQL.
By default, Hibernate never quotes a SQL table or column name in generated SQL when the name contains only alphanumeric characters.
This behavior is usually much more convenient, especially when working with a legacy schema, since unquoted identifiers aren't case-sensitive, and so Hibernate doesn't need to know or care whether a column is named `NAME`, `name`, or `Name` on the database side.
On the other hand, any table or column name containing a punctuation character like `$` is automatically quoted by default.

The following settings enable automatic quoting:
The following settings enable additional automatic quoting:

.Settings for identifier quoting
[%breakable,cols="35,~"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package org.hibernate.boot.model.naming;

import org.hibernate.internal.util.StringHelper;
import static org.hibernate.internal.util.StringHelper.isEmpty;

/**
* Models an identifier (name), retrieved from the database.
Expand All @@ -24,13 +24,13 @@ protected DatabaseIdentifier(String text) {
}

public static DatabaseIdentifier toIdentifier(String text) {
if ( StringHelper.isEmpty( text ) ) {
if ( isEmpty( text ) ) {
return null;
}
else if ( isQuoted( text ) ) {
// exclude the quotes from text
final String unquotedtext = text.substring( 1, text.length() - 1 );
return new DatabaseIdentifier( unquotedtext );
final String unquoted = text.substring( 1, text.length() - 1 );
return new DatabaseIdentifier( unquoted );
}
else {
return new DatabaseIdentifier( text );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import java.util.Locale;

import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;

import static java.lang.Character.isLetter;
import static java.lang.Character.isLetterOrDigit;
import static java.lang.Character.isWhitespace;
import static org.hibernate.internal.util.StringHelper.isBlank;
import static org.hibernate.internal.util.StringHelper.isEmpty;

/**
* Models an identifier (name), which may or may not be quoted.
Expand Down Expand Up @@ -83,13 +86,13 @@ public static Identifier toIdentifier(String text, boolean quote, boolean quoteO
int start = 0;
int end = text.length();
while ( start < end ) {
if ( !Character.isWhitespace( text.charAt( start ) ) ) {
if ( !isWhitespace( text.charAt( start ) ) ) {
break;
}
start++;
}
while ( start < end ) {
if ( !Character.isWhitespace( text.charAt( end - 1 ) ) ) {
if ( !isWhitespace( text.charAt( end - 1 ) ) ) {
break;
}
end--;
Expand All @@ -102,14 +105,14 @@ public static Identifier toIdentifier(String text, boolean quote, boolean quoteO
else if ( quoteOnNonIdentifierChar && !quote ) {
// Check the letters to determine if we must quote the text
char c = text.charAt( start );
if ( !Character.isLetter( c ) && c != '_' ) {
if ( !isLetter( c ) && c != '_' ) {
// SQL identifiers must begin with a letter or underscore
quote = true;
}
else {
for ( int i = start + 1; i < end; i++ ) {
c = text.charAt( i );
if ( !Character.isLetterOrDigit( c ) && c != '_' ) {
if ( !isLetterOrDigit( c ) && c != '_' ) {
quote = true;
break;
}
Expand Down Expand Up @@ -163,7 +166,7 @@ public static String unQuote(String name) {
* @param quoted Is this a quoted identifier?
*/
public Identifier(String text, boolean quoted) {
if ( StringHelper.isEmpty( text ) ) {
if ( isEmpty( text ) ) {
throw new IllegalIdentifierException( "Identifier text cannot be null" );
}
if ( isQuoted( text ) ) {
Expand Down Expand Up @@ -234,11 +237,9 @@ public String toString() {

@Override
public boolean equals(Object o) {
if ( !(o instanceof Identifier) ) {
if ( !(o instanceof Identifier that) ) {
return false;
}

final Identifier that = (Identifier) o;
return getCanonicalName().equals( that.getCanonicalName() );
}

Expand Down
Loading
Loading