-->
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.  [ 1 post ] 
Author Message
 Post subject: Reverse Engineering MySQL not applying identity annotation
PostPosted: Wed Nov 16, 2016 10:59 am 
Newbie

Joined: Wed Nov 16, 2016 10:40 am
Posts: 1
Java 1.8
Hibernate 5.2.4.Final
HibernateTools 5.2.0.CR1
MySQL-Connector 5.1.40
MySQL 5.6.25

We are doing a simple reverse engineering.

The ID column which is defined as NotNull AutoIncrement is being created at an int vs an Integer. In addition it is missing the following annotation. @GeneratedValue(strategy=IDENTITY)

I have applied my own MetaDataDialect which is just a copy of the MySQLMetaDataDialect with additional debug to determine if AutoIncrement is seen. I have applied this in the hibernate.cfg.xml file via the hibernatetool.metadatadialect property.

I see that the class does determine the column is AutoIncrement and it is able to return that information from the Dialect.


If i place the following in the table def of the reveng file then it will correctly apply the annotation and generate the primary key as an Integer. We would like to avoid this as it requires us to define this for every table.

Code:
       <primary-key>
          <generator class="identity"/>
       </primary-key>



Code:
package org.hibernate.cfg.reveng.dialect;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AdelphicMetaDataDialect extends JDBCMetaDataDialect {

   /**
    * Based on info from http://dev.mysql.com/doc/refman/5.0/en/show-table-status.html
    * Should work on pre-mysql 5 too since it uses the "old" SHOW TABLE command instead of SELECT from infotable.
    */
   public Iterator<Map<String, Object>> getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String table) {
      String sql = null;
         try {         
            catalog = caseForSearch( catalog );
            schema = caseForSearch( schema );
            table = caseForSearch( table );
            
            log.debug("geSuggestedPrimaryKeyStrategyName(" + catalog + "." + schema + "." + table + ")");
            
            sql = "show table status " + (catalog==null?"":" from " + catalog + " ") + (table==null?"":" like '" + table + "' ");
            PreparedStatement statement = getConnection().prepareStatement( sql );
            
            final String sc = schema;
            final String cat = catalog;
            return new ResultSetIterator(statement.executeQuery(), getSQLExceptionConverter()) {
               
               Map<String, Object> element = new HashMap<String, Object>();
               protected Map<String, Object> convertRow(ResultSet tableRs) throws SQLException {
                  element.clear();
                  String tableName = tableRs.getString("NAME");
                  element.put("TABLE_NAME", tableRs.getString("NAME"));
                  element.put("TABLE_SCHEM", sc);
                  element.put("TABLE_CAT", cat);
                  
                  String string = tableRs.getString("AUTO_INCREMENT");
                  
                  
                  System.out.println("Adelphic MetaDataDialect Catelog [" + cat + "] Schema [" + sc + "] Table [" + tableName + "] AutoIncrement [" + string +"] ");
                  
                  if(string==null) {
                     element.put("HIBERNATE_STRATEGY", null);
                  } else {
                     
                     System.out.println("Adelphic MetaDataDialect Catelog [" + cat + "] Schema [" + sc + "] Table [" + tableName + "] AutoIncrement [apply] ");
                     
                     element.put("HIBERNATE_STRATEGY", "identity");
                  }
                  return element;               
               }
               protected Throwable handleSQLException(SQLException e) {
                  // schemaRs and catalogRs are only used for error reporting if
                  // we get an exception
                  throw getSQLExceptionConverter().convert( e,
                        "Could not get list of suggested identity strategies from database. Probably a JDBC driver problem. ", null);               
               }
            };
         } catch (SQLException e) {
            throw getSQLExceptionConverter().convert(e, "Could not get list of suggested identity strategies from database. Probably a JDBC driver problem. ", sql);               
         }       
      }
   }



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

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.