IncrementGenerator broken for tables with blanks in their names after TODO-173

Description

The following change to IncrementGenerator.java

revision 1.11
date: 2005/04/26 06:37:53; author: oneovthafew; state: Exp; lines: +24 -5
TODO-173, increment for union-subclass
SQL logging from id generators
----------------------------
seems to have broken this generator when used on tables with blanks in their names.

This update tries to allow access to a comma-separated list of tables to identify the currently max-value of an id column.

I have a with the following mapping (in extract):

<hibernate-mapping>

<class name="dk.statsbiblioteket.navision.integration.Transaction" table="`REGNSKAB$SB Transaktioner`" dynamic-update="true">
<id name="lbNummer">
<column name="`Løbenr`" />
<generator class="increment" />
</id>
<discriminator column="`Trans type`" />
....

When trying to save a new entry of this mapped class, I get the following failure:

2006-03-19 10:52:09,828 [main] DEBUG org.hibernate.id.IncrementGenerator - fetching initial value: select max(ids_.[Løbenr]) from ( select [Løbenr] from [Statsbiblioteket$SB union select [Løbenr] from Transaktioner] ) ids_
Hibernate: select max(ids_.[Løbenr]) from ( select [Løbenr] from [Statsbiblioteket$SB union select [Løbenr] from Transaktioner] ) ids_
2006-03-19 10:52:09,852 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 156, SQLState: S1000
2006-03-19 10:52:09,852 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Incorrect syntax near the keyword 'from'.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not fetch initial value for increment generator
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:44)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:90)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:522)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
at dk.statsbiblioteket.navision.integration.UpdateItemPriceTransactionDAO.save(UpdateItemPriceTransactionDAO.java:45)
at dk.statsbiblioteket.videnstafetten.TidsskriftAar.requestNavisionItemPriceUpdate(TidsskriftAar.java:1188)
at dk.statsbiblioteket.videnstafetten.TidsskriftAar.verifyNavisionItemContents(TidsskriftAar.java:1132)
at dk.statsbiblioteket.videnstafetten.TidsskriftAar.synchronizeNavisionItem(TidsskriftAar.java:1077)
at synchronizeNavisionVare.sync(synchronizeNavisionVare.java:95)
at synchronizeNavisionVare.main(synchronizeNavisionVare.java:31)
Caused by: java.sql.SQLException: Incorrect syntax near the keyword 'from'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:418)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:693)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:85)
... 22 more

Note that the table name contains a blank and that this causes the configure method of the generator to think that it must access two tables.

The following patch seems to fix the problem, at least for me:

svn diff
Index: IncrementGenerator.java
===================================================================
— IncrementGenerator.java (revision 9658)
+++ IncrementGenerator.java (working copy)
@@ -51,7 +51,7 @@

String tableList = params.getProperty("tables");
if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);

  • String[] tables = StringHelper.split(", ", tableList);
    + String[] tables = StringHelper.split(",", tableList);
    String column = params.getProperty("column");
    if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
    String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);

What causes the problem is that StringHelper.split will tokenize the table name based on any comma and any blank. My change tells it to only split on commas (as the documentation indicates).

If you want to allow blanks after the table name separating commas, you probably need to parse the list of table names more carefully to handle quoted names with blanks in them. A slightly different solution that will probably work in most cases would be to split on the regex ",\s*", i.e. allowing any number of blanks after a comma.

  • Erik

Environment

Hibernate since ca. 3.1, verified at CVS of 24 Aug 2006

Activity

Show:
Brett Meyer
December 18, 2013, 7:57 PM

If this is still an issue on ORM 4.2 or 4.3, anyone have a test case to attach?

Brett Meyer
December 19, 2013, 10:31 PM

Note: In an attempt to clean up the HHH JIRA, we will be rejecting any ticket that sits in the "Awaiting Test Case" state for 2-3 months with no response. So, if this issue is critical to you, please attach a reproducing test case ASAP. Thanks!

Erik Bertelsen
December 20, 2013, 1:55 PM

I'm no longer working in the somewhat bizarre environment where I originally reported this bug and am consequently not in a position to test against that database with current ORM releases.

You may as well just close this bug.

Brett Meyer
December 20, 2013, 2:51 PM

Thanks for the update!

Assignee

Unassigned

Reporter

Erik Bertelsen

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure