Hi developers,

In the JBoss 2.4.x releases is a bug with the pk-constraint JAWS-option. In 
case of a compound primary key no constraint is generated. In the JBoss 3.0b 
this is fixed. Is it possible to downport the code of JDBCInitCommand.java in 
package org.jboss.ejb.plugins.jaws.jdbc for the 2.4.4 final release?

For my JBoss 2.4.3 I patched the source and it works for me. Find this little 
hack in the attachment. Maybe someone of you find the time to review it and 
put it in the cvs.

Thanks
Marco
-- 
Marco Ladermann
dpa, Deutsche Presse-Agentur GmbH
[EMAIL PROTECTED] Tel. +49 40 4113 2414   Fax  +49 40 4113 2479
PGP: http://blackhole.pca.dfn.de:11371/pks/lookup?op=get&search=0x4908CB66
/*
 * JBoss, the OpenSource EJB server
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.ejb.plugins.jaws.jdbc;

import java.util.Iterator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DatabaseMetaData;


import org.jboss.ejb.plugins.jaws.JPMInitCommand;
import org.jboss.ejb.plugins.jaws.metadata.CMPFieldMetaData;
import org.jboss.ejb.plugins.jaws.metadata.PkFieldMetaData;

/**
 * JAWSPersistenceManager JDBCInitCommand
 *
 * @see <related>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Joe Shevland</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Justin Forder</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Michel de Groot</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>danch (Dan Christopherson</a>
 * @version $Revision: 1.12.6.4 $
 *
 * Revision:
 * 20010621 danch: fixed bug where mapping a PK field to a different column name
 *    resulted in an improper PK constraint.
 */
public class JDBCInitCommand
   extends JDBCUpdateCommand
   implements JPMInitCommand
{
   // Constructors --------------------------------------------------

   public JDBCInitCommand(JDBCCommandFactory factory)
   {
      super(factory, "Init");

      // Create table SQL
      String sql = "CREATE TABLE " + jawsEntity.getTableName() + " (";

      Iterator it = jawsEntity.getCMPFields();
      boolean first = true;
      while (it.hasNext())
      {
         CMPFieldMetaData cmpField = (CMPFieldMetaData)it.next();

         sql += (first ? "" : ",") +
                cmpField.getColumnName() + " " +
                cmpField.getSQLType();


         first = false;
      }

      // If there is a primary key field,
      // and the bean has explicitly <pk-constraint>true</pk-constraint> in jaws.xml
      // add primary key constraint.
      if (jawsEntity.hasPkConstraint())  {
         sql += ",CONSTRAINT pk"+jawsEntity.getTableName()+" PRIMARY KEY (";
         for (Iterator i = jawsEntity.getPkFields();i.hasNext();) {
            String keyCol = ((PkFieldMetaData)i.next()).getColumnName();
            sql += keyCol;
            sql += i.hasNext()?",":"";
         }
         sql +=")";
      }

      sql += ")";

      setSQL(sql);
   }

   // JPMInitCommand implementation ---------------------------------

   public void execute() throws Exception
   {
      // Create table if necessary
      if (jawsEntity.getCreateTable())
      {
         // first check if the table already exists...
         // (a j2ee spec compatible jdbc driver has to fully
         // implement the DatabaseMetaData)
         boolean created = false;
         Connection con = null;
         ResultSet rs = null;
         try
         {
             con = getConnection();
             DatabaseMetaData dmd = con.getMetaData();
             rs = dmd.getTables(con.getCatalog(), null, jawsEntity.getTableName(), 
null);
             if (rs.next ())
                created = true;

             rs.close ();
             con.close ();
         }
         catch(Exception e)
         {
            throw e;
         }
         finally
         {
             if(rs != null) try {rs.close(); rs = null;}catch(SQLException e) {}
             if(con != null) try {con.close();con = null;}catch(SQLException e) {}
         }

         // Try to create it
         if(created) {
             log.info("Table '"+jawsEntity.getTableName()+"' already exists");
         } else {
             try
             {

                 // since we use the pools, we have to do this within a transaction
                factory.getContainer().getTransactionManager().begin ();
                jdbcExecute(null);
                factory.getContainer().getTransactionManager().commit ();

                // Create successful, log this
                log.info("Created table '"+jawsEntity.getTableName()+"' 
successfully.");
                String pkStr;
                if (jawsEntity.getPrimKeyField() != null)
                  pkStr = "'"+jawsEntity.getPrimKeyField()+"'";
                else {
                  pkStr = "[";
                  for (Iterator i = jawsEntity.getPkFields();i.hasNext();) {
                    String keyCol = ((PkFieldMetaData)i.next()).getColumnName();
                    pkStr += keyCol;
                    pkStr += i.hasNext()?",":"]";
                  }
                }
                log.debug("Primary key of table '"+jawsEntity.getTableName()+"' is "
                  +pkStr);
             } catch (Exception e)
             {
                log.debug("Could not create table " +
                          jawsEntity.getTableName() + ": " + e.getMessage());
                try
                {
                   factory.getContainer().getTransactionManager().rollback ();
                }
                catch (Exception _e)
                {
                   log.error("Could not roll back transaction: "+ _e.getMessage());
                }
             }
         }
      }
   }

   // JDBCUpdateCommand overrides -----------------------------------

   protected Object handleResult(int rowsAffected, Object argOrArgs)
      throws Exception
   {
      log.debug("Table " + jawsEntity.getTableName() + " created");
      return null;
   }
}

Reply via email to