-->
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: SchemaExport not creating multiple hilo columns in a table
PostPosted: Tue Jan 08, 2008 6:58 am 
Newbie

Joined: Tue Jan 08, 2008 6:29 am
Posts: 2
Hi all,

We're trying to unit test our hibernate DAOs and controllers and we're going down the route of using an in-memory HSQLDB to run the tests.

However, it seems that we're having problems with hbm2ddl/SchemaExport.

Several of our mappings use a hilo generator for the primary key (see examples below). The problem is that the SchemaExport is only creating the column for the first one alphabetically (in this case nextbackinstockemailid). Obviously this means that any tests that depend on the other generator columns fail.

Is this something that the SchemaExport just can't do? Is it something that can be fixed by tweaking the mappings. An obvious fix would be to use a different table for each generator but this isn't ideal since hibernate itself can use the schema just fine and we don't want to be changing our mappings just to satisfy the tests unless it turns out that our mappings are actually wrong.

Hibernate version: 3.2

Mapping documents:

Hibernate properties bean:
Code:
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
       
        <!-- Display generated SQL -->
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">false</prop>

        <prop key="default-lazy">true</prop>
       
        <!-- Create this database in-memory each time -->
        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
       
        <!-- Enable Hibernate's automatic session context management -->
        <prop key="hibernate.current_session_context_class">thread</prop>

        <!-- Apparently this makes HSQL work properly -->
        <prop key="hibernate.jdbc.batch_size">0</prop>
       
        <!-- Disable the second-level cache  -->
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
       
        <!-- get set info thing -->
        <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
      </props>     
    </property>     
  </bean>


Several mapping files use generated hilo IDs in the table NextKey.

Code:
     <id name="id" column="basketid">
       <generator class="hilo">
         <param name="table">NextKey</param>
         <param name="column">nextbasketid</param>
       </generator>
     </id>


Code:
      <id name="id" column="backinstockemailid">
          <generator class="hilo">
            <param name="table">NextKey</param>
            <param name="column">nextbackinstockemailid</param>
          </generator>
        </id>


Generated create statement for NextKey:

Code:
create table NextKey ( nextbackinstockemailid integer )
INSERT INTO NEXTKEY VALUES(0)


and several more, all written in the same format.

Full stack trace of any exception that occurs:

Console output from running the tests:

Code:
10:18:14,296 ERROR SchemaExport:272 - Unsuccessful: alter table CharacterProduct add constraint FK97C6BDA6AEA3B6D8 foreign key (productid) references Product
10:18:14,296 ERROR SchemaExport:273 - Constraint already exists in statement [alter table CharacterProduct add constraint FK97C6BDA6AEA3B6D8 foreign key (productid) references Product]
10:18:14,312 ERROR SchemaExport:272 - Unsuccessful: alter table Product add constraint FK50C664CFBDCD6E6A foreign key (groupid) references Product
10:18:14,312 ERROR SchemaExport:273 - Constraint already exists in statement [alter table Product add constraint FK50C664CFBDCD6E6A foreign key (groupid) references Product]
10:18:14,312 ERROR SchemaExport:272 - Unsuccessful: alter table ProductArtist add constraint FK4EFD18F6AEA3B6D8 foreign key (productid) references Product
10:18:14,312 ERROR SchemaExport:273 - Constraint already exists in statement [alter table ProductArtist add constraint FK4EFD18F6AEA3B6D8 foreign key (productid) references Product]
10:18:14,312 ERROR SchemaExport:272 - Unsuccessful: alter table PromotionProduct add constraint FK3B39BEECAEA3B6D8 foreign key (productid) references Product
10:18:14,312 ERROR SchemaExport:273 - Constraint already exists in statement [alter table PromotionProduct add constraint FK3B39BEECAEA3B6D8 foreign key (productid) references Product]
10:18:14,312 ERROR SchemaExport:272 - Unsuccessful: alter table Signed add constraint FK939A5B3CAEA3B6D8 foreign key (productid) references Product
10:18:14,312 ERROR SchemaExport:273 - Constraint already exists in statement [alter table Signed add constraint FK939A5B3CAEA3B6D8 foreign key (productid) references Product]
10:18:14,906  WARN JDBCExceptionReporter:71 - SQL Error: -28, SQLState: S0022
10:18:14,906 ERROR JDBCExceptionReporter:72 - Column not found: NEXTBASKETID in statement [select nextbasketid from NextKey]


Name and version of the database you are using:

hsqldb (latest version).

Any help would be greatly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 08, 2008 12:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I would think it should work otherwise our unittests should fail too...

but if it does not work it sounds like a nice enhancement to contribute a patch for.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 12:05 pm 
Newbie

Joined: Tue Jan 08, 2008 6:29 am
Posts: 2
Can you think of any obvious avenues to explore to determine if it's an actual bug or just a configuration issue on my end?

I'd love to go diving into patching Hibernate but I'd prefer to figure out if there's anything to patch first.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Look in the SchemaExport class code and how it calls out to the various generateDDL and see what key generators actually do.

that is what I would have to do to determine if it was a bug or not...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Same Problem Facing
PostPosted: Thu Jan 10, 2008 12:36 pm 
Newbie

Joined: Wed Jan 02, 2008 6:11 am
Posts: 6
Hi I am facing same problem. However I am facing it only on the classes having mappings other than one to one mapping.

I am using Hibernate annotations, Spring, HSQLDB for unittest.

My Console output is like

**** 01-10@17:42:45 ERROR (SchemaExport.java:274) - Unsuccessful: alter table QuestAvatar add constraint FK58FEBCBB1A82126F foreign key (avatarId) references Avatar
**** 01-10@17:42:45 ERROR (SchemaExport.java:275) - Unexpected token: ALTER in statement [alter table QuestAvatar add constraint FK58FEBCBB1A82126F foreign key (avatarId) references Avatar]
**** 01-10@17:42:45 ERROR (SchemaExport.java:274) - Unsuccessful: alter table QuestAvatar add constraint FK58FEBCBB4660F53D foreign key (questId) references Quest
**** 01-10@17:42:45 ERROR (SchemaExport.java:275) - Unexpected token: ALTER in statement [alter table QuestAvatar add constraint FK58FEBCBB4660F53D foreign key (questId) references Quest]
**** 01-10@17:42:45 ERROR (SchemaExport.java:274) - Unsuccessful: alter table QuestItem add constraint FKD28E6F5D284555C foreign key (spawnPoint_id) references SpawnPoint
**** 01-10@17:42:45 ERROR (SchemaExport.java:275) - Unexpected token: ALTER in statement [alter table QuestItem add constraint FKD28E6F5D284555C foreign key (spawnPoint_id) references SpawnPoint]

Best Regards



Farooq Ahmad


Top
 Profile  
 
 Post subject: Same Problem Facing
PostPosted: Tue Jan 15, 2008 8:41 am 
Newbie

Joined: Wed Jan 02, 2008 6:11 am
Posts: 6
max wrote:
I would think it should work otherwise our unittests should fail too...

but if it does not work it sounds like a nice enhancement to contribute a patch for.


Hi Max

I am facing same problem. Whether you got any solution or not.
Best Regards


Farooq Ahmad


Top
 Profile  
 
 Post subject: A fix for hbm2ddl surrogate key table
PostPosted: Tue Feb 12, 2008 11:13 pm 
Newbie

Joined: Tue Feb 12, 2008 9:46 pm
Posts: 1
I think I know what the problem is.

It's in the class org/hibernate/cfg/Configuration.java. In the method iterateGenerators, it gathers the generators in a TreeMap with the surrogate key table name as the map key. If you reuse the same table name for multiple surrogate key definitions, the map only saves the last one put into its collection because, as you know, maps cannot have duplicate keys. So the result is, the final collection contains 1 element with the table name and only the last columnName it saw. This generates the create SQL with only one columnName as such:

Code:
create table NextKey ( nextbackinstockemailid integer )
INSERT INTO NEXTKEY VALUES(0)


I don't know if there are any configurations to change this behavior, but I have managed to hacked the 3.2.6.ga source to fix the problem for us. It's not pretty, but it works. I know your not suppose to change interfaces but, like I said, it works for us.

I'm not 100% sure how to create patches but here is an attempt.....

org.hibernate.cfg.Configuration.java patch
Code:
--- /cygdrive/c/temp/hibernate-3.2/src/org/hibernate/cfg/Configuration.java   2008-01-24 20:34:42.000000000 -0500
+++ Configuration.java   2008-02-12 21:23:04.363924500 -0500
@@ -722,7 +722,14 @@
                     );

            if ( ig instanceof PersistentIdentifierGenerator ) {
-               generators.put( ( (PersistentIdentifierGenerator) ig ).generatorKey(), ig );
+               PersistentIdentifierGenerator pidg = (PersistentIdentifierGenerator) ig;
+               if (generators.containsKey(pidg.generatorKey())) {
+                  PersistentIdentifierGenerator old_pidg = (PersistentIdentifierGenerator)generators.get(pidg.generatorKey());
+                  old_pidg.setColumnName(old_pidg.getColumnName() + " integer, " + pidg.getColumnName());
+                  generators.put(pidg.generatorKey(), old_pidg);
+               } else {
+                  generators.put( ( (PersistentIdentifierGenerator) ig ).generatorKey(), ig );
+               }
            }

         }



org.hibernate.id.PersistentIdentifierGenerator.java patch
Code:
--- /cygdrive/c/temp/hibernate-3.2/src/org/hibernate/id/PersistentIdentifierGenerator.java   2005-04-26 02:37:54.000000000 -0400
+++ PersistentIdentifierGenerator.java   2008-02-12 21:33:26.457674500 -0500
@@ -71,6 +71,8 @@
   
   static final Log SQL = LogFactory.getLog("org.hibernate.SQL");

+   public String getColumnName();
+   public void setColumnName(String columnName);
}



org.hibernate.id.TableGenerator.java patch
Code:
--- /cygdrive/c/temp/hibernate-3.2/src/org/hibernate/id/TableGenerator.java   2007-03-19 18:06:46.000000000 -0400
+++ TableGenerator.java   2008-02-12 21:22:19.817049500 -0500
@@ -97,9 +97,14 @@


   public String[] sqlCreateStrings(Dialect dialect) {
+      String[] numOfColumns = columnName.split(",");
+      String cols = "0";
+      for (int x=1; x<numOfColumns.length; x++) {
+         cols += ",0";
+      }
      return new String[] {
         dialect.getCreateTableString() + " " + tableName + " ( " + columnName + " " + dialect.getTypeName(Types.INTEGER) + " )",
-         "insert into " + tableName + " values ( 0 )"
+         "insert into " + tableName + " values ( " + cols + " )"
      };
   }

@@ -167,4 +172,13 @@
      while (rows==0);
      return new Integer(result);
   }
+
+   public void setColumnName(String columnName) {
+      this.columnName = columnName;
+   }
+
+   public String getColumnName() {
+      return this.columnName;
+   }
+
}

[/code]


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.