JBoss Community

Re: Hibernate Tools question

created by Alex Popa-Tesileanu in JBoss Tools - View the full discussion

Hi,

I tried to get the connection from RuntimeInfo without success.

I created two functions: initConn and initConn1.

In first one I get the connection from RuntimeInfo. The result is:

    

java.lang.NullPointerException

          at ro.alex.CustReveng.initConn(CustReveng.java:49)

          at ro.alex.CustReveng.<init>(CustReveng.java:30)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

          at java.lang.reflect.Constructor.newInstance(Unknown Source)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.loadreverseEngineeringStrategy(ConsoleExtension4_0.java:297)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.access$1(ConsoleExtension4_0.java:293)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0$3.execute(ConsoleExtension4_0.java:256)

          at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)

          at org.jboss.tools.hibernate4_0.HibernateExtension4_0.execute(HibernateExtension4_0.java:211)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.buildConfiguration(ConsoleExtension4_0.java:237)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.runExporters(ConsoleExtension4_0.java:167)

          at org.jboss.tools.hibernate4_0.console.ConsoleExtension4_0.launchExporters(ConsoleExtension4_0.java:126)

          at org.hibernate.eclipse.launch.CodeGenerationLaunchDelegate.launch(CodeGenerationLaunchDelegate.java:270)

          at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:855)

          at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:704)

          at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1047)

          at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)

          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

 

 

In the initConn1 I created a new con nection and it work.

Bellow is the code:

    

package ro.alex;

 

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

 

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;

import org.hibernate.cfg.reveng.ReverseEngineeringRuntimeInfo;

import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;

import org.hibernate.cfg.reveng.TableIdentifier;

import org.hibernate.mapping.MetaAttribute;

import org.apache.log4j.Logger;

import org.apache.log4j.BasicConfigurator;

 

 

 

 

public class CustReveng extends DelegatingReverseEngineeringStrategy {

private ReverseEngineeringRuntimeInfo RuntimeInfo;

private Connection conn;

static Logger logger = Logger.getLogger(CustReveng.class);

 

 

public CustReveng(ReverseEngineeringStrategy strategy) {    

    super (strategy);    

    BasicConfigurator.configure();

    configure(getRuntimeInfo());

    initConn();

    //initConn1();

}

 

 

public Connection getConn() {

          return conn;

}

public void setConn(Connection conn) {

          this.conn = conn;

}

public void setRuntimeInfo(ReverseEngineeringRuntimeInfo runtimeInfo) {

          RuntimeInfo = runtimeInfo;

}

public ReverseEngineeringRuntimeInfo getRuntimeInfo() {

          return RuntimeInfo;

}

 

 

public void initConn(){

          try {

                    setConn(getRuntimeInfo().getConnectionProvider().getConnection());

          } catch (SQLException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

          }

}

 

 

public void initConn1(){

          //Create a new database connection

           String dbUrl = "jdbc:mysql://localhost:3307/conta";

           try {

                     setConn(DriverManager.getConnection (dbUrl,"user","password"));

           }

           catch(SQLException e) {

                     e.printStackTrace();

                     logger.info("test"+e.getMessage());

           }

}

 

  public Map<String, MetaAttribute> columnToMetaAttributes(TableIdentifier identifier, String column) {

      Map<String, MetaAttribute> result = new HashMap<String, MetaAttribute>();

 

      MetaAttribute ma = new MetaAttribute("default-value");

      String table;

      table=identifier.getName();

      String defaultValue = getTableColumnDefault(table, column);

      if(defaultValue!=null && !defaultValue.isEmpty()){

                ma.addValue(defaultValue);

                result.put("default-value", ma);

      }

      return result;

}

 

public String getTableColumnDefault(String table, String column){

           String xdefault = "";

           String xType = "";

           String query = "SHOW COLUMNS FROM "+table+" LIKE '"+column+"'";

           try {

                     Connection con = getConn();

                     Statement stmt = con.createStatement();

                     ResultSet rs = stmt.executeQuery(query);

                     while (rs.next()) {

                               xdefault = rs.getString(5);

                               xType = rs.getString(2);

                               //logger.info("Default value for "+column+" "+xdefault);

                     } //end while

           } //end try

           catch(SQLException e) {

           e.printStackTrace();

           logger.info("SQL Error: "+e.getMessage());

           }

           //Code to calculate default value

           //logger.info("Type value for "+column+" "+(xType.substring(0,Math.min(xType.length(), 7))));

           if(xType.substring(0,Math.min(xType.length(), 4)).equals("char")){

                     if(xType.trim().equals("char(1)")){

                               xdefault="\'"+xdefault+"\'";

                     }else{

                               xdefault="\""+xdefault+"\"";

                     }

           }else if(xType.substring(0,Math.min(xType.length(), 7)).equals("varchar")){

                     if(xType.trim().equals("varchar(1)")){

                               xdefault="\'"+xdefault+"\'";

                     }else{

                               xdefault="\""+xdefault+"\"";

                     }                    

           }else if(xType.trim()=="text"){

                     xdefault="\""+xdefault+"\"";

           }else if(xdefault!=null && !xdefault.isEmpty() && (xType.trim().equals("date") || xType.trim().equals("datetime")) ){

                     xdefault="new Date(0)";

           }else if(xdefault!=null && !xdefault.isEmpty() && (xType=="decimal" || xType.trim()=="int" || xType.trim()=="smallint" || xType.trim()=="tinyint" )){

                     xdefault="0";

           }else{

                     xdefault="\"ccc\"";

           }

          

                    

return xdefault;

}  

 

 

public void finalize(){

          try {

                    getConn().close();

          } catch (SQLException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

          }

          try {

                    super.finalize();

          } catch (Throwable e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

          }

}

 

}

 

 

I can't figure out where is the problem.

getTableColumnDefault function is not yet finished. Can be helpful if  I can get JDBC type and Hibernate type.

It is possible ?

 

Thank You,

Alex

Reply to this message by going to Community

Start a new discussion in JBoss Tools at Community