<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: Hibernate Tools question
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/alexpote">Alex Popa-Tesileanu</a> in <i>JBoss Tools</i> - <a href="https://community.jboss.org/message/761036#761036">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi,</p><p>I tried to get the connection from RuntimeInfo without success.</p><p>I created two functions: initConn and initConn1.</p><p>In first one I get the connection from RuntimeInfo. The result is:</p><p>     </p><blockquote class="jive-quote"><p>java.lang.NullPointerException</p><p>          at ro.alex.CustReveng.initConn(CustReveng.java:49)</p><p>          at ro.alex.CustReveng.<init>(CustReveng.java:30)</p><p>          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</p><p>          at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)</p><p>          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)</p><p>          at java.lang.reflect.Constructor.newInstance(Unknown Source)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.loadreverseEngineeringStrategy(ConsoleExtension4_0.java:297)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.access$1(ConsoleExtension4_0.java:293)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0$3.execute(ConsoleExtension4_0.java:256)</p><p>          at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)</p><p>          at org.jboss.tools.hibernate4_0.HibernateExtension4_0.execute(HibernateExtension4_0.java:211)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.buildConfiguration(ConsoleExtension4_0.java:237)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.runExporters(ConsoleExtension4_0.java:167)</p><p>          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.launchExporters(ConsoleExtension4_0.java:126)</p><p>          at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.launch(CodeGenerationLaunchDelegate.java:270)</p><p>          at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:855)</p><p>          at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:704)</p><p>          at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1047)</p><p>          at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)</p><p>          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>In the initConn1 I created a new con nection and it work.</p><p>Bellow is the code:</p><p>     </p><blockquote class="jive-quote"><p><span style="font-family: 'courier new', courier; font-size: 10pt;">package ro.alex;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.sql.Connection;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.sql.DriverManager;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.sql.ResultSet;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.sql.SQLException;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.sql.Statement;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.util.Date;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.util.HashMap;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import java.util.Map;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy; </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.hibernate.cfg.reveng.ReverseEngineeringRuntimeInfo;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.hibernate.cfg.reveng.ReverseEngineeringStrategy; </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.hibernate.cfg.reveng.TableIdentifier; </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.hibernate.mapping.MetaAttribute;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.apache.log4j.Logger;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">import org.apache.log4j.BasicConfigurator;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public class CustReveng extends DelegatingReverseEngineeringStrategy { </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">private ReverseEngineeringRuntimeInfo RuntimeInfo;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">private Connection conn;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">static Logger logger = Logger.getLogger(CustReveng.class);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public CustReveng(ReverseEngineeringStrategy strategy) {     </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">    super (strategy);     </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">    BasicConfigurator.configure();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">    configure(getRuntimeInfo());</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">    initConn();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">    //initConn1();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public Connection getConn() {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          return conn;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public void setConn(Connection conn) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          this.conn = conn;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public void setRuntimeInfo(ReverseEngineeringRuntimeInfo runtimeInfo) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          RuntimeInfo = runtimeInfo;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public ReverseEngineeringRuntimeInfo getRuntimeInfo() {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          return RuntimeInfo;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public void initConn(){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    setConn(getRuntimeInfo().getConnectionProvider().getConnection());</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          } catch (SQLException e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    // TODO Auto-generated catch block</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          } </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public void initConn1(){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          //Create a new database connection</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           String dbUrl = "jdbc:mysql://localhost:3307/conta";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     setConn(DriverManager.getConnection (dbUrl,"user","password"));</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           catch(SQLException e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     logger.info("test"+e.getMessage());</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           } </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">  public Map<String, MetaAttribute> columnToMetaAttributes(TableIdentifier identifier, String column) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      Map<String, MetaAttribute> result = new HashMap<String, MetaAttribute>();</span></p><p>  </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      MetaAttribute ma = new MetaAttribute("default-value");</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      String table;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      table=identifier.getName();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      String defaultValue = getTableColumnDefault(table, column);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      if(defaultValue!=null && !defaultValue.isEmpty()){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                ma.addValue(defaultValue);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                result.put("default-value", ma);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">      return result;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p>  </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;"> public String getTableColumnDefault(String table, String column){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           String xdefault = "";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           String xType = "";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           String query = "SHOW COLUMNS FROM "+table+" LIKE '"+column+"'";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     Connection con = getConn();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     Statement stmt = con.createStatement();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     ResultSet rs = stmt.executeQuery(query);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     while (rs.next()) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xdefault = rs.getString(5);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xType = rs.getString(2);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               //logger.info("Default value for "+column+" "+xdefault);</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     } //end while</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           } //end try</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           catch(SQLException e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           logger.info("SQL Error: "+e.getMessage());</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           //Code to calculate default value </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           //logger.info("Type value for "+column+" "+(xType.substring(0,Math.min(xType.length(), 7))));</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           if(xType.substring(0,Math.min(xType.length(), 4)).equals("char")){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     if(xType.trim().equals("char(1)")){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xdefault="\'"+xdefault+"\'";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     }else{</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xdefault="\""+xdefault+"\"";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }else if(xType.substring(0,Math.min(xType.length(), 7)).equals("varchar")){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     if(xType.trim().equals("varchar(1)")){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xdefault="\'"+xdefault+"\'";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     }else{</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                               xdefault="\""+xdefault+"\"";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     }                     </span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }else if(xType.trim()=="text"){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     xdefault="\""+xdefault+"\"";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }else if(xdefault!=null && !xdefault.isEmpty() && (xType.trim().equals("date") || xType.trim().equals("datetime")) ){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     xdefault="new Date(0)";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }else if(xdefault!=null && !xdefault.isEmpty() && (xType=="decimal" || xType.trim()=="int" || xType.trim()=="smallint" || xType.trim()=="tinyint" )){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     xdefault="0";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }else{</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                     xdefault="\"ccc\"";</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">           }</span></p><p>           </p><p>                     </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;"> return xdefault;</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;"> }   </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">public void finalize(){</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    getConn().close();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          } catch (SQLException e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    // TODO Auto-generated catch block</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    super.finalize();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          } catch (Throwable e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    // TODO Auto-generated catch block</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">                    e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">          }</span></p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: 'courier new', courier; font-size: 10pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></blockquote><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I can't figure out where is the problem.</p><p><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">getTableColumnDefault</span> function is not yet finished. Can be helpful if  I can get JDBC type and Hibernate type.</p><p>It is possible ? </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thank You,</p><p>Alex</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/761036#761036">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss Tools at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2128">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>