-->
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.  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Middlegen plugin:
PostPosted: Fri Sep 05, 2003 2:41 pm 
Newbie

Joined: Fri Sep 05, 2003 2:24 pm
Posts: 3
I know this is a very generic question...but I'll try...
Does anybody know where I can find a guide / tutorial that explains step by step the files generation process for Hibernate using Middlegen?...The sample (http://www.hibernate.org/98.html) gives me a brief guideline but It doesn't helpme to much in order to get the thing working...any help will be appreciated....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 2:45 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
what kind of help are you looking for.

middlegen will only create hibernate hbm.xml files for you as it is written. I wrote my own template to make it generate java files with xdoclet tags. The only real issue I found was the postgresql driver shipped with either xdoclet or middlegen was too old.

You need to dl the latest middlegen client. Go to the samples dir. Alter build.xml and build.properties to suit your db. Run ant. Click generate. Kill the gui. Your files should be in samples/build


Top
 Profile  
 
 Post subject: Ok...this is what I want:
PostPosted: Fri Sep 05, 2003 3:42 pm 
Newbie

Joined: Fri Sep 05, 2003 2:24 pm
Posts: 3
I want to create the hbm.xml file...I have a database with lots of tables / fields and I wanted to know how can generate this files and related classes getting this information directly from Database.

I undertood that I must change the "middlegen" ant task in the sample that comes in Middlegen-Hibernate-r1.zip (?) and run ant tool with this files...but I don't know what paramaters I have to change and where...(I just only modified the database user, pass, driver and URL parameters)...

Now I am using "ReverseGenerator.bat"...It seems that it serves for the same prpouse...the only strange thing that I could see is that the mapping xml file doesn't refect the relationship between field (Foreign keys)....I am using Point Base Database.

If I look the database model using a DbVisualizer (that uses the same driver) all table relations are reflected Ok...so I I think the driver works well capturing the Db metadata....

Sorry if not very specific / clear with my issue...I have just only a couple of hours using this framework that seem a very good one but I couldn't easy see where are the first steps that I have to do in order to use it...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 3:52 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
i don't use reverseenginer.

here is a template i made for the middlegen hibernate task

Code:
package ${table.package};

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

#set ($pkg = $table.package)
/**
*
* @author
* @hibernate.class table="${table.getSqlName()}"
*
**/
public class ${table.destinationClassName} implements Serializable {

#set ($KeyIdFlag = true)
#set ($VersionId = true)
#set ($haspk = false)
#foreach( $column in $table.basicColumns )
#if ($column.pk)
#set ($haspk = true)
#end
    private ${column.javaType} ${column.variableName};   
#end
#if (!$haspk)
//it don't have a pk
#foreach( $column in $table.columns )
#if (!$haspk && $column.fk)
        #set ($haspk = true)
    private ${column.javaType} ${column.variableName};
    /**
     * Standard getter
     * @return ${column.variableName}
     * @hibernate.id type="$column.javaType" column="$column.sqlName" generator-class="sequence"
     * @hibernate.generator-param name="sequence" value="${column.sqlName}_seq"                 
     **/
    public ${column.javaType} ${column.getGetterName()}() {
        return this.${column.variableName};
    }   
#end
#end
#end
#foreach( $column in $table.basicColumns )
        #set ($CompositeKeyFlag = false)
        #if ($column.pk)
                #set ($haspk = true)
                #if ($KeyIdFlag && $table.compositeKey)
                        #set ($KeyIdFlag = false)
                        #set ($CompositeKeyFlag = true)
                            //<composite-id name="comp_id" class="${table.package}.${table.compoundKeyDestinationClassName}">
                        #foreach ( $compKeysClass in $table.primaryKeyColumns )
                            //<key-property name="${compKeysClass.variableName}" column="${compKeysClass.sqlName}" />
                        #end
                    //</composite-id>   
                #elseif ($KeyIdFlag)
   
    /**
     * Standard getter
     * @return ${column.variableName}
     * @hibernate.id type="$column.javaType" column="$column.sqlName" generator-class="sequence"
     * @hibernate.generator-param name="sequence" value="${column.sqlName}_seq"                 
     **/
    public ${column.javaType} ${column.getGetterName()}() {
        return this.${column.variableName};
    }
    /**
     * Standard setter
     *
     **/                     
    public void ${column.getSetterName()}(${column.javaType} inval) {
        this.${column.variableName} = inval;
    }                       
                #end
                #if ($table.versioningPresent && $VersionId)
                #set ($VersionId = false)
                #set ($versionCol = $table.versioningField)
                #if ($versionCol.specialtyVersionProperty)
                    <version
                        name="${versionCol.variableName}"
                        type="$versionCol.javaType"
                        column="$versionCol.sqlName"
                    />
                #elseif ($versionCol.specialtyTimestampProperty)
                    <timestamp
                        name="${versionCol.variableName}"
                        column="$versionCol.sqlName"
                    />   
                #end
                #end
        #else
                #if ($column.specialtyProperty)
       
    /**
     *
     * Standard getter
     * @return ${column.variableName}
     * @hibernate.property type="$column.javaType"
     *      column="$column.sqlName"
#if (!$column.nullable)
     *      not-null="true"
#end
#if ($column.unique)
     *      unique="true"
#end
#if ($column.sized)
     *      length="$column.size"
#end
     *
     **/
     public $column.javaType ${column.getGetterName()} () {
          return this.${column.variableName};
     }
     /**
      * Standard setter
      *
      **/                     
     public void ${column.getSetterName()}(${column.javaType} inval) {
          this.${column.variableName} = inval;
     }
                #end
        #end
#end
#foreach($relationshipRole in $table.relationshipRoles)
        #if( $relationshipRole.enabled )
                #set( $one2one = $relationshipRole.relation.one2One )
                #set( $many2many = $relationshipRole.relation.many2Many )
                #set( $bidirectional = $relationshipRole.relation.bidirectional )
                #set( $target = $relationshipRole.getTarget($plugin))
                ########## Compute cardinality strings for javadocs (useful for humans)
                #if( $bidirectional )
                        #set( $directionalityText = "bi-directional" )
                #else
                        #set( $directionalityText = "uni-directional" )
                #end
                #if( $many2many )
                        #set( $cardinalityText = "many-to-many" )
                #else
                        #if( $one2one )
                                #set( $cardinalityText = "one-to-one" )
                        #else
                                #if($relationshipRole.targetMany)
                                        #set( $cardinalityText = "one-to-many" )
                                #else
                                        #set( $cardinalityText = "many-to-one" )
                                #end
                        #end
                #end
    // $directionalityText $cardinalityText association to $target.destinationClassName
#if( !$relationshipRole.targetMany )
        #if( $one2one )
     private ${target.package}.${target.destinationClassName} $table.getVariableName($relationshipRole);
     /**
      *
      * Standard getter
      *
      * @hibernate.one-to-one class="${table.getVariableName($relationshipRole)}" outer-join="auto"
        #if ($bidirectional)
      *       not-null="true"
        #end
      *
     **/
     public ${target.package}.${target.destinationClassName} $table.getGetterName($relationshipRole)() {
          return this.$table.getVariableName($relationshipRole);
     }
     public void $table.getSetterName($relationshipRole)(${target.package}.${target.destinationClassName} invar) {
          this.$table.getVariableName($relationshipRole) = invar;
     }
#else
     private ${target.package}.${target.destinationClassName} $table.getVariableName($relationshipRole);
    /**
     *
     * Standard getter
#set ($name = "${table.getVariableName($relationshipRole).substring(0,1).toUpperCase()}${table.getVariableName($relationshipRole).substring(1)}")       
     * @hibernate.one-to-one class="${table.package}.${name}" outer-join="auto"
#if ($bidirectional)
     *       not-null="true"
#end
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
     *      column name="$columnMap.foreignKey
#end
     *
     **/
     public ${target.package}.${target.destinationClassName} $table.getGetterName($relationshipRole)() {
          return this.$table.getVariableName($relationshipRole);
     }
    /**
     *
     * Standard setter
     *
     **/
     public void $table.getSetterName($relationshipRole)(${target.package}.${target.destinationClassName} invar) {
          this.$table.getVariableName($relationshipRole) = invar;
     }       
        #end
#else
#if( !$many2many && $bidirectional ) 
#foreach($relRole in $target.relationshipRoles)       
#if( $relRole.target.getSqlName() != $table.getSqlName() )
#set ($name = "${relRole.target.getSqlName().substring(0,1).toUpperCase()}${relRole.target.getSqlName().substring(1)}")
#set ($rl = $relRole.target.getSqlName())
#set ($col = $relRole.target.getPkColumn().getSqlName())     
#end     
#end
#foreach ($column in $target.basicColumns)
#if ($column.pk == false)
#set ($sortcol = $column.sqlName)
#end
#end
#end
     private java.util.List $rl;
    /**
      *
      * Standard getter
      *
      * @hibernate.list lazy="true" table="${target.getSqlName()}" role="${rl}s"
      * @hibernate.collection-many-to-many column="${col}" class="${pkg}.${name}"   
      * @hibernate.collection-key column="${table.getPkColumn().getSqlName()}"
      * @hibernate.collection-index column="$sortcol"
      *
      *
      **/
      public java.util.List get${name}s() {
           return this.${rl};
      }
     /**
      *
      * Standard setter
      * @param inval
      **/
     public void set${name}s( java.util.List inval ) {
           this.$rl = inval;
     }
     private java.util.Set $table.getVariableName($relationshipRole);
     /**
      *
      * Standard getter
      *
#if( !$many2many && $bidirectional )
#set ($name = "${table.getVariableName($relationshipRole).substring(0,1).toUpperCase()}${table.getVariableName($relationshipRole).substring(1)}")
      * @hibernate.set lazy="true" inverse="true" cascade="none" role="${table.getVariableName($relationshipRole)}" table="${target.getSqlName()}"
      * @hibernate.collection-key column="${table.getPkColumn().getSqlName()}"
      * @hibernate.collection-one-to-many class="${target.package}.${target.destinationClassName}" 
#end     
#if( $many2many )
      *       table="$relationshipRole.relation.joinTable.sqlName"
#end
      **/
     public java.util.Set $table.getGetterName($relationshipRole) () {
          return this.$table.getVariableName($relationshipRole);
     }
     /**
      *
      * Standard setter
      *
      **/
     public void $table.getSetterName($relationshipRole) (java.util.Set invar) {
          this.$table.getVariableName($relationshipRole) = invar;
     }     
#end
#end
#end
#foreach( $column in $table.basicColumns )
#if ($column.pk)
     /**
      *
      * Equals test
      * @returns true if the object passed in equals this object
      *
      **/       
     public boolean equals(Object other) {
          if (!(other instanceof ${table.destinationClassName}))
               return false;
          ${table.destinationClassName} castOther = (${table.destinationClassName}) other;
          return new EqualsBuilder().append(this.$column.variableName, castOther.$column.variableName).isEquals();
     }
     /**
      * Custom hashcode function uses the pk field.
      * @returns int hashcode
      *
      **/
     public int hashCode() {
          return new HashCodeBuilder().append($column.variableName).toHashCode();
     }
#end
#end

}

copy that template code to a file named hibernate_java.vm
then you have an ant task like this for middlegen
Code:
<target
      name="middlegen"
      description="Run Middlegen"
      unless="middlegen.skip"
      depends="check-driver-present,panic-if-driver-not-present,del"
   >
      <taskdef
         name="middlegen"
         classname="middlegen.MiddlegenTask"
         classpathref="lib.class.path"
      />
      <middlegen
         appname="${name}"
         prefsdir="${src.dir}"
         gui="${gui}"
         databaseurl="${database.url}"
         initialContextFactory="${java.naming.factory.initial}"
         providerURL="${java.naming.provider.url}"
         datasourceJNDIName="${name}.database"
         driver="${database.driver}"
         username="${database.userid}"
         password="${database.password}"
         schema=""
      >
       
       
         <hibernate
            destination="${build.gen-src.dir}"
            package="${project-package}.${name}">
            <fileproducer
                template="${basedir}/src/templates/hibernate_java.vm"
                destination="${build.gen-src.dir}"
                filename="{0}.java"/>
         </hibernate>
      </middlegen>
    </target>


You will have to play with middlegen api a bit, but it's working really nice for me right now. Generates all the java source files with xdoclet tags. To get your hibernate.hbm.xml files you need to run another ant target. The hibernatedoclet target looks like this (from hibernate docs and xdoclet)

Code:

<target name="hibernate" description="Generate mapping documents (run jar first)">
        <taskdef
            name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoc.lib"
            />
            <taskdef
            name="xdoclet"
            classname="xdoclet.DocletTask"
            classpathref="xdoc.lib"
            />
        <echo>+---------------------------------------------------+</echo>
        <echo>|                                                   |</echo>
        <echo>| R U N N I N G   H I B E R N A T E D O C L E T     |</echo>
        <echo>|                                                   |</echo>
        <echo>+---------------------------------------------------+</echo>

        <hibernatedoclet
            destdir="${build.gen-src.dir}"
            mergedir="${build.gen-src.dir}"
            excludedtags="@version,@author,@todo,@see"
            addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
            force="true"
            verbose="false">

            <fileset dir="${build.gen-src.dir}">
                <include name="**/*.java"/>
            </fileset>
            <hibernate version="2.0"/>
        </hibernatedoclet>
       
        <replace dir="${build.gen-src.dir}">
            <include name="**/*.hbm.xml"/>
            <replacefilter token="readonly" value="inverse"/>
            <replacefilter token="role" value="name"/>
            <replacefilter token="hibernate-mapping.dtd" value="hibernate-mapping-2.0.dtd"/>
        </replace>

    </target>


Hope that helps


Top
 Profile  
 
 Post subject: Thankx for your explanation!
PostPosted: Fri Sep 05, 2003 4:23 pm 
Newbie

Joined: Fri Sep 05, 2003 2:24 pm
Posts: 3
I will test it ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The R1 release is just about ready out of the box to run against the supplied hsql database. There is an ant Task to create the tables for you and the task to get the GUI up to play with table relationships and setting various properties and Meta Tags as required. All of the Jars etc are there ready to use. All you need to do is setup classpath and go. If you want to talk to another database then you are looking at changes to the JDBC driver and the username and password settings.

It is true that R1 only generates hbm files which can be used to generate the Java using hibernate's hbm2java tool. My internal verion also generates the java code with XDoclet tags. I have been ponding releasing this with R2. This provides the option to generate just hbm, or java or both. Allow users too choose the development approach that suites them the best. I have also added other enhancements (as well as further support for hbm2java).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 11:03 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
where is this r2 version? I'm interested, as you can tell since I started writing my own velocity template


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 9:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
In development, I will announce it when it is ready. I am trying to find a small chunk of time to finish it off.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 2:13 pm 
Beginner
Beginner

Joined: Fri Sep 05, 2003 7:21 am
Posts: 27
Location: Sweden
David, will you have the time to integrate my SLSB generation patch into the next release? Haven't worked on it much lately, but IIRC, it was pretty much finished - I think. (I believe the only issue was that Middlegen didn't output the files in the correct dir according to the package, but that should be fixable)

I think SLSB generation would be a killer feature anyhow, as I (personally) avoided writing 50 SLSBs from scratch by using it...

- Mikael


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Glad to see you are back.
As I have said previously, I cannot see why not. I have a preference for optional generation as this avoids code generation for those that don't require it and are not interested in that approach (not everyone is using SLSBs - I do :-) but through the command pattern).

I view the plugin as providing two classes of services.
1) Core services - such as the hbm (and possibly the POJO (optional core)).
2) Convenience services - such as your SLSB generation and the like.

If you want to send me your latest version I will add it to the list. Your commitment to help make it more mature (given you will initially be the primary user) will help the cause.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 5:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I'm getting more and more "nervous" here ;)

Are you going to reimplement hbm2java like features in middlegen ?
If yes - then we should at least try to utilize both their strenghts - as i don't see any reason for having 2 things that does the same ;)

Just my 2 cent ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 5:54 am 
Beginner
Beginner

Joined: Fri Sep 05, 2003 7:21 am
Posts: 27
Location: Sweden
David,

I'm actually considering moving the SLSB generation into a separate plugin, as that seems to be the simplest solution to the "one package only" problem.

That's probably the best thing from a "release point of view" as well - then you won't have to wait for me to finish testing it before you can release the new version.

Max: I thought hbm2java was to be integrated into the middlegen plugin, so I abandoned working on POJO generation. Am I correct on this matter..?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 6:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
carniz wrote:
Max: I thought hbm2java was to be integrated into the middlegen plugin, so I abandoned working on POJO generation. Am I correct on this matter..?


I thought so also ;), but SLSB is something that hbm2java would do just as fine - especially since it is aware of hbm.xml's etc.

Having hbm2java do it would make it possible to not be dependent on middlegen to do that......e.g. for those writing their own hbm.xml files.

It's just to have full power in any kind of "mapping" cycle you have ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 7:17 am 
Beginner
Beginner

Joined: Fri Sep 05, 2003 7:21 am
Posts: 27
Location: Sweden
Quote:
Having hbm2java do it would make it possible to not be dependent on middlegen to do that......e.g. for those writing their own hbm.xml files.


Sigh...theese kind of things is what takes my energy away, and makes me realise the benefits of being a Microsoft developer (everything is already there, everything is thought of from the beginning, and the tools are developed with a development model in mind)

I guess I'll just keep the Middlegen extension for myself then and wait until hbm2java provides the same functinality.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 7:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
carniz wrote:
Quote:
Having hbm2java do it would make it possible to not be dependent on middlegen to do that......e.g. for those writing their own hbm.xml files.


Sigh...theese kind of things is what takes my energy away, and makes me realise the benefits of being a Microsoft developer (everything is already there, everything is thought of from the beginning, and the tools are developed with a development model in mind)

I guess I'll just keep the Middlegen extension for myself then and wait until hbm2java provides the same functinality.


Easy now ;) If you don't provide the extension then we can't benefit from it! So, please provide it as inspiration and ground work for further extensions (to both hbm2java and middlegen)!

And your comment about "and the tools are developed with a development model in mind" i think is fairly misplaced in this concern ;)

Having SLSB generation support in hbm2java would be great enhancement since it would work no matter what development model you have..(or at least, more than the "i have database, give me a model and then java") other people got "I have a model, give me a database and java" and others got "I have java, give me a model and a database" .....middlegen (as i see it supports "i got a database..")

So, please provide the SLSB - it does no harm - i would just raise the flag most people would benefit it the SLSB generation was based on hbm.xml file instead of just the database and middlegen...

And may i ask - is the SLSB generation hibernate-bound or is it independent of it ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 23 posts ]  Go to page 1, 2  Next

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.