hibernate.globally_quoted_identifiers=true and Annotations tests

Description

If I use hibernate.globally_quoted_identifiers=true in order to quote all identifiers, then the mapping for column names breakes if I have a column with a @Column annotation but without a @Column(name=<COLUMN_NAME>). The mapping for the column name then turns out to be ``. Which leads to problems when creating new tables.

I was able to track the error back to

final String columnName = nameNormalizer.normalizeIdentifierQuoting( col.name() );

from Ejb3Column.java.

col.name() returns an empty string because the name was not explicitly set.
nameNormalizer.normalizeIdentifierQuoting( col.name() ) then makes `` from the empty string.

I was able to fix this problem by adding a guard to the nameNormalizer.normalizeIdentifierQuoting function:

if ( identifier.length() == 0) return null;

The function now looks like this:

public String normalizeIdentifierQuoting(String identifier) { if ( identifier == null ) { return null; } if ( identifier.length() == 0) return null; // Convert the JPA2 specific quoting character (double quote) to Hibernate's (back tick) if ( identifier.startsWith( "\"" ) && identifier.endsWith( "\"" ) ) { return '`' + identifier.substring( 1, identifier.length() - 1 ) + '`'; } // If the user has requested "global" use of quoted identifiers, quote this identifier (using back ticks) // if not already if ( isUseQuotedIdentifiersGlobally() && ! ( identifier.startsWith( "`" ) && identifier.endsWith( "`" ) ) ) { return '`' + identifier + '`'; } return identifier; }

Attachments

1
  • 09 May 2010, 09:03 PM

Activity

Steve EbersoleMay 10, 2010 at 2:14 PM

TBH, I do not see exactly where what you describe can actually happen. All the ObjectNameNormalizer calls I see from Ejb3Column are protected by `if ( StringHelper.isNotEmpty( columnName ) )`. Anyway the proposed change to ObjectNameNormalizer itself is reasonable, so I went ahead and did it.

Steve EbersoleMay 10, 2010 at 2:03 AM

ah empty string, how do I love thee. Let me count the ways... 🙂

Jaka JaksicMay 9, 2010 at 9:49 PM

The issue is still present in 3.5.1.

The problem stems from the fact that a @Column annotation without a name attribute returns an empty string as the name value, which somewhere down the road gets treated as if the value was set. If there are at least two such columns, it ultimately causes this exception:

org.hibernate.MappingException: Repeated column in mapping for entity: test.QuotingBug column: (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)

(This is because all such columns get assigned an empty name.)

Stephan's proposed fix seems to fix the issue.

Jaka JaksicMay 9, 2010 at 9:03 PM

An annotated POJO class that reproduces the bug.

Former userApril 22, 2010 at 12:31 AM

Is this still an issue in 3.5.1? If so, please attach a runnable unit test (Java + mapping) that reproduces this issue.

Fixed

Details

Assignee

Reporter

Time tracking

0.15h logged

Components

Fix versions

Affects versions

Priority

Created March 12, 2010 at 3:02 PM
Updated April 22, 2015 at 6:24 PM
Resolved May 10, 2010 at 2:30 PM

Flag notifications