-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Using Oracle and enhanced table generator
PostPosted: Fri Nov 02, 2007 10:28 am 
Newbie

Joined: Tue Jul 05, 2005 12:42 pm
Posts: 5
I am trying to use the enhanced table generator with Oracle 10g. When initializing the database I get an error about not being able to instantiate an id generator. It works fine against mysql. The reason I am doing this is that I am using union-subclasses and was having problems with just using native for the id generators did not work. I like the enhance table generator because it keeps multiple sequence number in one table and that seems cleaner. The system I am writing needs to support multiple backend databases and I would like to stay away form different mapping docs for each flavor. Here is an edited version of the mapping document along with the full stacktrace. Any help or advice would be greatly appreciated.



Hibernate version:3.2

Mapping documents:
<hibernate-mapping package="org.archiviststoolkit.model">
<class name="org.archiviststoolkit.model.BasicNames" abstract="true">

<id name="nameId" column="nameId" type="java.lang.Long">
<generator class="org.hibernate.id.enhanced.TableGenerator">
<param name="optimizer">hilo</param>
<param name="segment_value">names</param>
<param name="increment_size">10</param>
</generator>
</id>

<version name="version" type="java.lang.Long" column="version"/>


<!-- core name fields-->
<property name="nameType"
column="nameType"
not-null="true"
type="string"/>

<property name="sortName"
column="sortName"
not-null="true"
index="sortName"
type="string"/>


<union-subclass name="NonPreferredNames" table="NonPreferredNames">
<many-to-one name="primaryName"
class="Names"
column="primaryNameId"
access="field"
fetch="join"
not-null="true"
outer-join="true"/>
</union-subclass>

<union-subclass name="Names" table="Names" lazy = "false">

<property name="nameSource"
column="nameSource"
length="50"
type="string"/>

<property name="nameRule"
column="nameRule"
length="50"
type="string"/>


</union-subclass>

</class>

</hibernate-mapping>





Full stack trace of any exception that occurs:

SEVERE: Failed to startup hibernate engine
org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.archiviststoolkit.hibernate.SessionFactory.<init>(SessionFactory.java:114)
at org.archiviststoolkit.hibernate.SessionFactory.getInstance(SessionFactory.java:145)
at org.archiviststoolkit.hibernate.SessionFactory.testHibernate(SessionFactory.java:127)
at org.archiviststoolkit.maintenance.initDatabase.InitDatabaseBackgroundResultCreator.initializeDatabase(InitDatabaseBackgroundResultCreator.java:187)
at org.archiviststoolkit.maintenance.initDatabase.InitDatabaseBackgroundResultCreator.start(InitDatabaseBackgroundResultCreator.java:99)
at org.netbeans.api.wizard.DefaultWizardDisplayer$1ButtonListener$1.run(DefaultWizardDisplayer.java:407)
at java.lang.Thread.run(Thread.java:613)
Caused by: java.lang.IllegalArgumentException: alias not found: tbl
at org.hibernate.sql.ForUpdateFragment.<init>(ForUpdateFragment.java:36)
at org.hibernate.dialect.Dialect.applyLocksToSql(Dialect.java:970)
at org.hibernate.id.enhanced.TableGenerator.configure(TableGenerator.java:194)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)
... 10 more
Exception in thread "Wizard Background Result Thread org.archiviststoolkit.maintenance.initDatabase.InitDatabaseBackgroundResultCreator@2aec21" java.lang.RuntimeException: org.hibernate.MappingException: could not instantiate id generator
at org.archiviststoolkit.hibernate.SessionFactory.<init>(SessionFactory.java:118)
at org.archiviststoolkit.hibernate.SessionFactory.getInstance(SessionFactory.java:145)
at org.archiviststoolkit.hibernate.SessionFactory.testHibernate(SessionFactory.java:127)
at org.archiviststoolkit.maintenance.initDatabase.InitDatabaseBackgroundResultCreator.initializeDatabase(InitDatabaseBackgroundResultCreator.java:187)
at org.archiviststoolkit.maintenance.initDatabase.InitDatabaseBackgroundResultCreator.start(InitDatabaseBackgroundResultCreator.java:99)
at org.netbeans.api.wizard.DefaultWizardDisplayer$1ButtonListener$1.run(DefaultWizardDisplayer.java:407)
at java.lang.Thread.run(Thread.java:613)
Caused by: org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.archiviststoolkit.hibernate.SessionFactory.<init>(SessionFactory.java:114)
... 6 more
Caused by: java.lang.IllegalArgumentException: alias not found: tbl
at org.hibernate.sql.ForUpdateFragment.<init>(ForUpdateFragment.java:36)
at org.hibernate.dialect.Dialect.applyLocksToSql(Dialect.java:970)
at org.hibernate.id.enhanced.TableGenerator.configure(TableGenerator.java:194)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)
... 10 more


Name and version of the database you are using:Oracle 10g


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 8:17 am 
Newbie

Joined: Wed Dec 13, 2006 9:54 am
Posts: 5
any news in this subject ? i'm stuck with the same problem.

When I checked through the sources, it seemed that this is hard to solve, because :

The enhanced Table Generator does ( lines 192-194):
Code:
HashMap lockMap = new HashMap();
      lockMap.put( "tbl", LockMode.UPGRADE );
      this.query = dialect.applyLocksToSql( query, lockMap, CollectionHelper.EMPTY_MAP );


gives an empty map for the keyColumns!

The Dialect class passes this on to org.hibernate.sql.ForUpdateFragment
(lines 33-42)
Code:
if ( dialect.forUpdateOfColumns() ) {
               String[] keyColumns = ( String[] ) keyColumnNames.get( tableAlias ); //use the id column alias
               if ( keyColumns == null ) {
                  throw new IllegalArgumentException( "alias not found: " + tableAlias );
               }
               keyColumns = StringHelper.qualify( tableAlias, keyColumns );
               for ( int i = 0; i < keyColumns.length; i++ ) {
                  addTableAlias( keyColumns[i] );
               }
            }



Now I think, only the Oracle Dialects return true on dialect.forUpdateOfColumns().
The enhanced TableGenerator always has an empty keyColumnMap ( because of CollectionHelper.EMPTY_MAP ).
Now
Code:
String[] keyColumns = ( String[] ) keyColumnNames.get( tableAlias );   
can only produce keyColumns==null;

Therefor I cant see any way, u get around running into this error.

Not sure though :)






Sources:
-----------------------------------------
org.hibernate.sql.ForUpdateFragment
Code:
/**
* @author Gavin King
*/
public class ForUpdateFragment {
   private final StringBuffer aliases = new StringBuffer();
   private boolean isNowaitEnabled;
   private final Dialect dialect;

   public ForUpdateFragment(Dialect dialect) {
      this.dialect = dialect;
   }

   public ForUpdateFragment(Dialect dialect, Map lockModes, Map keyColumnNames) throws QueryException {
      this( dialect );
      LockMode upgradeType = null;
      Iterator iter = lockModes.entrySet().iterator();
      while ( iter.hasNext() ) {
         final Map.Entry me = ( Map.Entry ) iter.next();
         final LockMode lockMode = ( LockMode ) me.getValue();
         if ( LockMode.READ.lessThan( lockMode ) ) {
            final String tableAlias = ( String ) me.getKey();
            if ( dialect.forUpdateOfColumns() ) {
               String[] keyColumns = ( String[] ) keyColumnNames.get( tableAlias ); //use the id column alias
               if ( keyColumns == null ) {
                  throw new IllegalArgumentException( "alias not found: " + tableAlias );
               }
               keyColumns = StringHelper.qualify( tableAlias, keyColumns );
               for ( int i = 0; i < keyColumns.length; i++ ) {
                  addTableAlias( keyColumns[i] );
               }
            }
            else {
               addTableAlias( tableAlias );
            }
            if ( upgradeType != null && lockMode != upgradeType ) {
               throw new QueryException( "mixed LockModes" );
            }
            upgradeType = lockMode;
         }
      }

      if ( upgradeType == LockMode.UPGRADE_NOWAIT ) {
         setNowaitEnabled( true );
      }
   }



org.hibernate.id.enhanced.TableGenerator
Code:
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
      tableName = PropertiesHelper.getString( TABLE_PARAM, params, DEF_TABLE );
      if ( tableName.indexOf( '.' ) < 0 ) {
         String schemaName = params.getProperty( SCHEMA );
         String catalogName = params.getProperty( CATALOG );
         tableName = Table.qualify( catalogName, schemaName, tableName );
      }

      segmentColumnName = PropertiesHelper.getString( SEGMENT_COLUMN_PARAM, params, DEF_SEGMENT_COLUMN );
      segmentValue = params.getProperty( SEGMENT_VALUE_PARAM );
      if ( StringHelper.isEmpty( segmentValue ) ) {
         log.debug( "explicit segment value for id generator [" + tableName + '.' + segmentColumnName + "] suggested; using default [" + DEF_SEGMENT_VALUE + "]" );
         segmentValue = DEF_SEGMENT_VALUE;
      }
      segmentValueLength = PropertiesHelper.getInt( SEGMENT_LENGTH_PARAM, params, DEF_SEGMENT_LENGTH );
      valueColumnName = PropertiesHelper.getString( VALUE_COLUMN_PARAM, params, DEF_VALUE_COLUMN );
      initialValue = PropertiesHelper.getInt( INITIAL_PARAM, params, DEFAULT_INITIAL_VALUE );
      incrementSize = PropertiesHelper.getInt( INCREMENT_PARAM, params, DEFAULT_INCREMENT_SIZE );
      identifierType = type;

      String query = "select " + valueColumnName +
            " from " + tableName + " tbl" +
            " where tbl." + segmentColumnName + "=?";
      HashMap lockMap = new HashMap();
      lockMap.put( "tbl", LockMode.UPGRADE );
      this.query = dialect.applyLocksToSql( query, lockMap, CollectionHelper.EMPTY_MAP );

      update = "update " + tableName +
            " set " + valueColumnName + "=? " +
            " where " + valueColumnName + "=? and " + segmentColumnName + "=?";

      insert = "insert into " + tableName + " (" + segmentColumnName + ", " + valueColumnName + ") " + " values (?,?)";

      String defOptStrategy = incrementSize <= 1 ? OptimizerFactory.NONE : OptimizerFactory.POOL;
      String optimizationStrategy = PropertiesHelper.getString( OPT_PARAM, params, defOptStrategy );
      optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy, identifierType.getReturnedClass(), incrementSize );
   }



Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 12:56 pm 
Newbie

Joined: Wed Dec 13, 2006 9:54 am
Posts: 5
I was able to fix that problem for me by changing the following line in the org.hibernate.id.enhanced.TableGenerator class from

Code:
this.query = dialect.applyLocksToSql( query, lockMap, CollectionHelper.EMPTY_MAP );


to

Code:
this.query = dialect.applyLocksToSql( query, lockMap, Collections.singletonMap("tbl", new String[] {valueColumnName}) );


This has not been fully tested or whatever, it just seems to be working for now.

I just hope this doesn't get back at me at some other point.
So just take it as a suggestion for testing, not as a solution ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 18, 2007 3:49 am 
Newbie

Joined: Fri Dec 07, 2007 5:52 am
Posts: 4
Location: Budapest, Hungary, EU
I also run into this problem. It would be nice to see this fixed in an upcoming release.

Stomp's solution seems to solve the problem for me too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 10:24 am 
Newbie

Joined: Wed Dec 13, 2006 9:54 am
Posts: 5
any news on this topic?

we ran into some problems trying to apply that "fix" to the new version of hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 6:55 am 
Newbie

Joined: Mon Oct 06, 2008 6:51 am
Posts: 2
Really Thank you all...

_________________
Oracle Security Assessment


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 6:58 am 
Newbie

Joined: Mon Oct 06, 2008 6:51 am
Posts: 2
Really Thank you all...

_________________
Oracle Security Assessment


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.