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.  [ 4 posts ] 
Author Message
 Post subject: ReverseEngineeringStrategy docummentation ?
PostPosted: Mon May 23, 2011 5:45 am 
Newbie

Joined: Fri May 20, 2011 3:41 am
Posts: 5
Hello all,

I'm looking for a (real) documentation about ReverseEngineeringStrategy and/or DelegatingReverseEngineeringStrategy ...

I have been searching for hours on internet but I only find the list of methods ... but neither a fully documented content about them nor complete examples.

I only find again and again the same tricky example about the 'column to attribute' function ... but nothing about the other methods of the classes.

Anyone can help please ?


Thx


Cédric.


Last edited by Cédric VIDREQUIN on Tue May 24, 2011 7:57 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: ReverseEngineeringStrategy docummentation ?
PostPosted: Mon May 23, 2011 3:46 pm 
Beginner
Beginner

Joined: Mon Jul 28, 2008 4:36 pm
Posts: 24
Try:
http://www.docjar.com/docs/api/org/hibe ... ategy.html

That gave me what I needed to implement that class and add debugging to figure out which columns went to which methods and how to use them. It took me about 4-7 years (depending how you count) to get to have the guts to do this because it seemed so mysterious, but once I did, it really wasn't so hard. It required a little experimentation.

Then, in my ant build.xml file:
Code:
   <path id="hibernate.lib.path">
      <pathelement path="${compile.dir}" />
      <!-- Only seems to work with all jars in this dir -->
      <fileset dir="./lib/">
         <include name="**/*.jar" />
      </fileset>
   </path>

   <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="hibernate.lib.path" />

   <!-- Build starts here -->

   <target name="genSchema">
      <hibernatetool destdir="${build.dir}" templatepath=".">
         <classpath refid="hibernate.lib.path" />
         <jdbcconfiguration configurationfile="./hibernate1.cfg.xml" packagename="my.app.db" revengfile="./hibernate.reveng.xml" reversestrategy="RevEngStrategy" />
         <hbm2hbmxml destdir="${build.dir}" />
         <hbm2java jdk5="true" />
         <hbm2cfgxml />
      </hibernatetool>
   </target>
   
   <target name="archive">
      <exec executable="${winzip.exe}">
         <arg line="-ybc -r -p ${env.MY_HOME}\app\backups\schemaGenBackup_${tstamp.now}.zip ." />
      </exec>
   </target>

   <target name="main" depends="compile, genSchema" />

   <target name="compile">
      <!-- <delete dir="${compile.dir}" includeEmptyDirs="true" /> -->
      <mkdir dir="${compile.dir}" />
      
      <javac srcdir="${src.dir}" destdir="${compile.dir}" debug="on" deprecation="on" encoding="UTF-8">
         <include name="**/*.java" />
         <classpath refid="hibernate.lib.path" />
      </javac>
   </target>


Top
 Profile  
 
 Post subject: Re: ReverseEngineeringStrategy docummentation ?
PostPosted: Tue May 24, 2011 8:00 am 
Newbie

Joined: Fri May 20, 2011 3:41 am
Posts: 5
Thx for your answer glenpeterson, but I already found this site ... and I don't think it has understandable informations ... sorry.

Only few (one in fact) methods are documented ... no example ... I'll continue to search.

But again : thanks for taking time to answer :)


Top
 Profile  
 
 Post subject: Re: ReverseEngineeringStrategy docummentation ?
PostPosted: Tue May 24, 2011 9:32 am 
Beginner
Beginner

Joined: Mon Jul 28, 2008 4:36 pm
Posts: 24
ReverseEngineeringStrategy is great way to affect the naming of fields (particularly xref tables that make many-to-many relationships), but it's only one piece of the puzzle. I have a blog entry on controlling Hibernate reverse engineering. I've since turned those notes into a script and should probably make a new blog entry for the script, but here's the old one:
http://glenpeterson.blogspot.com/2008/07/mysql-data-types-and-naming-for-use.html

Here's an example ReverseEngineeringStrategy implementation which I actually use. The way the API is structured, there were going to be a lot of checks to see if a given field was the one I wanted. Instead of checking each field individually, I just concatenate all the information into a string and do a string comparison. About half of the code in this example is the concatenation, the rest are overridden methods

I hope this helps:

Code:
public class RevEngStrategy extends DelegatingReverseEngineeringStrategy {

    public RevEngStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    /**
       Concetanates a list of column names using a hyphen as a separator.
    */
    private static String figureColName(List<Column> cols) {
        if (cols == null) {
            return null;
        } else if (cols.size() < 1) {
            return "";
        } else if (cols.size() == 1) {
            return cols.get(0).getName();
        } else {
            StringBuilder sB = new StringBuilder();
            boolean isFirst = true;
            for (Column col : cols) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    sB.append("-");
                }
                sB.append(col.getName());
            }
            return sB.toString();
        }
    }

    private static String concatColInfo(String keyname,
                                        TableIdentifier fromTable,
                                        String fromCol,
                                        TableIdentifier referencedTable,
                                        String refCol,
                                        boolean uniqueRef) {
        StringBuilder sB = new StringBuilder();
        sB.append(keyname);
        sB.append("|");
        sB.append(fromTable.getName());
        sB.append("|");
        sB.append(fromCol);
        sB.append("|");
        sB.append(referencedTable.getName());
        sB.append("|");
        sB.append(refCol);
        sB.append("|");
        if (uniqueRef) {
            sB.append("1");
        } else {
            sB.append("0");
        }
        return sB.toString();
    }

    @Override
    public String foreignKeyToCollectionName(String keyname,
                                             TableIdentifier fromTable,
                                             List fromColumns,
                                             TableIdentifier refTable,
                                             List refColumns,
                                             boolean uniqueRef) {

        // Concatenate a hyphen-separated columnName string from the list Hibernate gives us.
        String fromCol = figureColName( (List<Column>) fromColumns);
        String refCol = figureColName( (List<Column>) refColumns);

        // Concatenate all column info using pipes as separators
        String val = concatColInfo(keyname, fromTable, fromCol, refTable, refCol, uniqueRef);

        String ret = null;

        // Check if it's the column we want
        // Instead of "subsets" I want "thisEntitysSubsets".
        // Because an entity can have subsets and also belong to another
        // entity's subsets and I want to be clear which is which!
        if ("m_subset_fk_entity|metric_subset|entity_id|entity|id|1".equals(val) ) {
            System.out.println("******* thisEntitysSubsets *******");
            ret = "thisEntitysSubsets";
        } else {
            ret = super.foreignKeyToCollectionName(keyname, fromTable, fromColumns, refTable, refColumns, uniqueRef);
        }

        System.out.println("fk2Coll: " + val + ": " + ret);
        return ret;
    }

    public String foreignKeyToEntityName(String keyname,
                                         TableIdentifier fromTable,
                                         List fromCols,
                                         TableIdentifier refTable,
                                         List refCols,
                                         boolean uniqueRef)
    {
        String fromCol = figureColName( (List<Column>) fromCols);
        String refCol = figureColName( (List<Column>) refCols);

        String val = concatColInfo(keyname, fromTable, fromCol, refTable, refCol, uniqueRef);

        String ret = null;

        // I want to call last modifiers, "lastModifier" not "UserByLastModifierId"
        // Similarly, I want "owner" not "UserByOwnerId"
        // And I want to make this change to every table.
        if ( "last_modifier_id".equals(fromCol) &&
             "user".equals(refTable.getName()) &&
             "id".equals(refCol) &&
             uniqueRef &&
             keyname.endsWith("fk_modifier") ) {
            System.out.println("******* lastModifier *******");
            ret = "lastModifier";

        } else if ( "owner_id".equals(fromCol) &&
                    "user".equals(refTable.getName()) &&
                    "id".equals(refCol) &&
                    !"sc_metric".equals(fromTable.getName()) &&
                    !uniqueRef &&
                    keyname.endsWith("fk_owner") ) {
            System.out.println("******* owner *******");
            ret = "owner";
        } else {
            // Accept default name
            ret = super.foreignKeyToEntityName(keyname, fromTable, fromCols, refTable, refCols, uniqueRef);
        }

        System.out.println("fk2Ent: " + val + ": " + ret);
        return ret;
    }

    private static String figureForeignKeyName(ForeignKey fk) {
        StringBuilder sB = new StringBuilder(fk.getTable().getName());
        sB.append(",");
        sB.append(figureColName( (List<Column>) fk.getColumns()) );
        sB.append(",");
        sB.append(figureColName( (List<Column>) fk.getReferencedColumns() ));
        sB.append(",");
        sB.append(fk.getName());
      return sB.toString();
    }

    public String foreignKeyToManyToManyName(ForeignKey fromKey,
                                             TableIdentifier middleTable,
                                             ForeignKey toKey,
                                             boolean uniqueRef)
    {
        StringBuilder sB = new StringBuilder();
        sB.append(figureForeignKeyName(fromKey));
        sB.append("|");
        sB.append(middleTable.getName());
        sB.append("|");
        sB.append(figureForeignKeyName(toKey));
        sB.append("|");
        if (uniqueRef) {
            sB.append("1");
        } else {
            sB.append("0");
        }
        String val = sB.toString();

        String ret = null;

        // Instead of "subsets1" I want "subsetsThisEntityBelongsTo"
        // Because an entity can have subsets and also belong to another
        // entity's subsets and I want to be clear which is which!
        if ("m_subset_entity_xref,entity_id,id,m_subset_ent_fk_ent|m_subset_entity_xref|m_subset_entity_xref,m_subset_id,id,m_subset_ent_fk_metric_subset|1"
            .equals(val)) {
            ret = "subsetsThisEntityBelongsTo";
        } else {
            // Accept default name
            ret = super.foreignKeyToManyToManyName(fromKey, middleTable, toKey, uniqueRef);
        }

        System.out.println("fk_many: " + val + " Becomes: " + ret);
        return ret;
    }
}


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

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.