Mapping a table called "group" in Hibernate for DB2 and HSQLDB

I have a table called a group that I am trying to map using hibernate for DB2 and HSQLDB . The table name group is a reserved word and must be specified in HSQLDB. However, DB2 does not like the quoted table name.

So this mapping works in HSQLDB, but not in DB2:

@Entity @Table(name="`group`") public class Group { 

Showing results in the following error in DB2 (creating a query that includes a Group table):

  Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -204, SQLSTATE: 42704, SQLERRMC: SCHEMA_NAME.group
     at com.ibm.db2.jcc.b.hh.c (hh.java:1662)
     at com.ibm.db2.jcc.b.hh.d (hh.java:1650)
     at com.ibm.db2.jcc.b.hh.a (hh.java:1219)
     at com.ibm.db2.jcc.c.db.g (db.java:139)
     at com.ibm.db2.jcc.c.db.a (db.java:39)
     at com.ibm.db2.jcc.cta (t.java:34)
     at com.ibm.db2.jcc.c.sb.f (sb.java:142)
     at com.ibm.db2.jcc.b.hh.n (hh.java:1190)
     at com.ibm.db2.jcc.b.ih.eb (ih.java:1997)
     at com.ibm.db2.jcc.b.ih.d (ih.java:2439)
     at com.ibm.db2.jcc.b.ih.V (ih.java:492)
     at com.ibm.db2.jcc.b.ih.executeQuery (ih.java:475)
     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery (NewProxyPreparedStatement.java:76)
     at org.hibernate.jdbc.AbstractBatcher.getResultSet (AbstractBatcher.java:186)
     at org.hibernate.loader.Loader.getResultSet (Loader.java:1787)
     at org.hibernate.loader.Loader.doQuery (Loader.java:674)
     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections (Loader.java:236)
     at org.hibernate.loader.Loader.loadCollection (Loader.java:1994)
     ... 71 more

And this works in DB2, but not in HSQLDB:

 @Entity @Table(name="group") public class Group { 

Showing results in the following error in HSQLDB (creating a Group table):

  WARN hibernate.ExtendedAnnotatedSessionFactoryBean - Unsuccessful schema statement: create table group (* details omitted *)
 java.sql.SQLException: Unexpected token: GROUP in statement [create table group]
     at org.hsqldb.jdbc.Util.sqlException (Unknown Source)
     at org.hsqldb.jdbc.jdbcStatement.fetchResult (Unknown Source)
     at org.hsqldb.jdbc.jdbcStatement.executeUpdate (Unknown Source)
     at org.springframework.orm.hibernate3.LocalSessionFactoryBean.executeSchemaStatement (LocalSessionFactoryBean.java:1000)
     at org.springframework.orm.hibernate3.LocalSessionFactoryBean.executeSchemaScript (LocalSessionFactoryBean.java:972)
     at org.springframework.orm.hibernate3.LocalSessionFactoryBean $ 2.doInHibernate (LocalSessionFactoryBean.java:912)
     at org.springframework.orm.hibernate3.HibernateTemplate.execute (HibernateTemplate.javahaps73)
     at org.springframework.orm.hibernate3.HibernateTemplate.execute (HibernateTemplate.javahaps38)
     at org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema (LocalSessionFactoryBean.java:906)
   ...

I use org.hibernate.dialect.DB2Dialect and org.hibernate.dialect.HSQLDialect for DB2 and HSQLDB, respectively.

How can I do the same mapping job for both databases at the same time?

+3
source share
2 answers

I ended up working with create alias groups for group; in DB2 and changed the table name in HSQLDB, which solved my problem. Now I am mapping the group table and this problem no longer occurs.

+2
source

Using backticks (as in your first example) should work, but you should use them everywhere . The table name is specified.

Dialect has openQuote () and closeQuote () , which translate these backlinks into quotes that match the given database. The default is double quotation mark (") for most databases, including DB2 and HSQL. I don't have a single hand right now to try this, but it worked for me for a combination of DB2 / Oracle / MySQL in the past.

Can you post the specific error and stack trace you get?

All that said, if you can change the name of the table to an unreserved word, I would strongly recommend that you do this - in the end you will save a lot of headache. p>

0
source

All Articles