[jboss-user] [JBoss Tools] - Re: Hibernate Tools question

Alex Popa-Tesileanu do-not-reply at jboss.com
Tue Sep 25 07:10:56 EDT 2012


Alex Popa-Tesileanu [https://community.jboss.org/people/alexpote] created the discussion

"Re: Hibernate Tools question"

To view the discussion, visit: https://community.jboss.org/message/761353#761353

--------------------------------------------------------------
Hi,
The problem was how to get RuntimeInfo in order to get the connection.
The obvious response is to override configure method:
> @Override
>           public void configure(ReverseEngineeringRuntimeInfo runtimeInfo) {
>                     // TODO Auto-generated method stub
>                     super.configure(runtimeInfo);
>                     setRuntimeInfo(runtimeInfo);
>                     try {
>                               setConn(getRuntimeInfo().getConnectionProvider().getConnection());
>                     } catch (SQLException e) {
>                               // TODO Auto-generated catch block
>                               e.printStackTrace();
>                     }
>           }
> 
Problem solved. It work.
Bellow is all code, maybe useful for someone.
It is hardcoded for MySQL and doesn't include all data types. 
Can be improved if will possible to get column defaults and hibernate datatype in columnToMetaAttributes function.


> package ro.alex.reveng;
> 
> 
> import java.sql.Connection;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
> 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);
>           }
> 
> 
>           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;
>           }
> 
> 
>           @Override
>           public void configure(ReverseEngineeringRuntimeInfo runtimeInfo) {
>                     // TODO Auto-generated method stub
>                     super.configure(runtimeInfo);
>                     setRuntimeInfo(runtimeInfo);
>                     try {
>                               setConn(getRuntimeInfo().getConnectionProvider().getConnection());
>                     } catch (SQLException e) {
>                               // TODO Auto-generated catch block
>                               e.printStackTrace();
>                     }
>           }
> 
> 
>           public Map<String, MetaAttribute> columnToMetaAttributes(
>                               TableIdentifier identifier, String column) {
> 
> 
>                     Map<String, MetaAttribute> result = new HashMap<String, MetaAttribute>();
>                     MetaAttribute ma = new MetaAttribute("default-value");
>                     String defaultValue = getTableColumnDefault(identifier, column);
>                     if (defaultValue != null && !defaultValue.isEmpty()) {
>                               ma.addValue(defaultValue);
>                               result.put("default-value", ma);
>                     }
>                     return result;
>           }
> 
> 
>           public String tableToClassName(TableIdentifier tableIdentifier) {
>                     tableIdentifier = removePrefix(tableIdentifier);
>                     return super.tableToClassName(tableIdentifier);
>           }
> 
> 
>           private TableIdentifier removePrefix(TableIdentifier tableIdentifier) {
>                     String name = tableIdentifier.getName();
>                     if (name.toLowerCase().startsWith("aaa_")) {
>                               tableIdentifier = new TableIdentifier(tableIdentifier.getSchema(),
>                                                   tableIdentifier.getCatalog(), name.substring(4));
>                     }
>                     return tableIdentifier;
>           }
> 
> 
>           public String getTableColumnDefault(TableIdentifier identifier,
>                               String column) {
>                     String xdefault = "";
>                     String xType = "";
>                     String query = "SHOW COLUMNS FROM " + identifier.getName() + " 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);
>                               } // end while
>                     } // end try
>                     catch (SQLException e) {
>                               e.printStackTrace();
>                               logger.info("SQL Error: " + e.getMessage());
>                     }
>                     // Code to calculate default value
>                     if (xType.substring(0, Math.min(xType.length(), 4)).equals("char")) {
>                               if (xType.trim().equals("char(1)")) {
>                                         if (xdefault.isEmpty()) {
>                                                   xdefault = "\' \'";
>                                         } else {
>                                                   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 (xType.trim().equals("timestamp")) {
>                               xdefault = "";
>                     } else if (xdefault != null
>                                         && !xdefault.isEmpty()
>                                         && (xType.trim().equals("date") || xType.trim().equals(
>                                                             "datetime"))) {
>                               xdefault = "new Date(0)";
>                     } else if (xType.substring(0, Math.min(xType.length(), 7)).equals(
>                                         "decimal")) {
>                               if (xdefault != null && xdefault.isEmpty()) {
>                                         xdefault = "new BigDecimal(0)";
>                               } else {
>                                         xdefault = "new BigDecimal(" + xdefault + ")";
>                               }
>                     } else if (xType.substring(0, Math.min(xType.length(), 3))
>                                         .equals("int")
>                                         || xType.substring(0, Math.min(xType.length(), 8)).equals(
>                                                             "smallint")
>                                         || xType.substring(0, Math.min(xType.length(), 7)).equals(
>                                                             "tinyint")) {
>                               xdefault = "0";
>                     } else {
>                               xdefault = "\"ccc\""; //Dummy value for control
>                     }
> 
>                     ///For compositeId's don't need to initialize variables 
>                     boolean isPk = false;
>                     try {
>                               ResultSet PKColumns = conn.getMetaData().getPrimaryKeys(
>                                                   conn.getCatalog(), "conta", identifier.getName());
>                               while (PKColumns.next()) {
>                                         if (PKColumns.getString(4).equals(column)) {
>                                                   isPk = true;
>                                         }
>                               }
>                     } catch (SQLException e) {
>                               // TODO Auto-generated catch block
>                               e.printStackTrace();
>                     }
>                     if (isPk) {
>                               xdefault = "";
>                     }
>                     // }
>                     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();
>                     }
> 
> 
>           }
> 
> 
> }
> 

*Thank you very much for help,*
Alex
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/761353#761353]

Start a new discussion in JBoss Tools at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2128]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20120925/7fb09ad9/attachment-0001.html 


More information about the jboss-user mailing list