-->
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.  [ 15 posts ] 
Author Message
 Post subject: Custom exporter and custom templates - how can I do that?
PostPosted: Mon Mar 27, 2006 11:00 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
Hi,

as there was the need that my generated, annotated classes have to be refreshed, I looked for a way to keep my manually written methods. By default the files are all written from scratch.

Therefor I wrote my custom exporter which extends the POJOExporter class. In my case the method exportPOJO(...) first checks if the file already exists and saves the custom content in this case. After generating the template the custom content should be added again. The Java code seems to work - but my custom velocity templates are ignored when using the custom exporter.

My hbmtemplate config look like this

Code:
<hbmtemplate
   filepattern="{package-name}/{class-name}.java"
   template="Pojo.vm"
   exporterclass="de.innosystec.meta.hibernate.exporter.IstExporter"
>
   <property key="jdk5" value="${hibernatetool.use.jdk5}" />
   <property key="ejb3" value="${hibernatetool.use.ejb3}" />
   <property key="basepackage" value="${hibernatetool.reveng.package}.base" />
</hbmtemplate>


Is there a way to have a custom exporter with custom templates?

Rgds.,
Axel

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 5:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Template and exporterClass is mutually exclusive since you could have an exporterclass which does not use the template.

in any case, if you extend PojoExporter it will simply just start by using pojo/Pojo.vm (or pojo/Pojo.ftl if you use the latest svn) which will be looked up via the templatepath which you can configure so it will find your template.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 10:00 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
max wrote:
in any case, if you extend PojoExporter it will simply just start by using pojo/Pojo.vm (or pojo/Pojo.ftl if you use the latest svn) which will be looked up via the templatepath which you can configure so it will find your template.


Sorry for digging out such an old thread.

My custom exporter extends PojoExporter and my custom template is located at the specified template path in a subdirectory pojo and its name is Pojo.ftl
Nevertheless the custom template is not used. It seems that the templates are taken from the jar file even if my custom exporter returns the correct template path for the method getTemplatePaths(). Is there still a known bug.

I think I still didn't check the Exporter/Template stuff - even if I read the documentation several times now. :-(

:edit I am currently using Hibernate Tools 3.2.0beta7

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 4:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
is it in <templatepath>/Pojo.ftl or <templatepath>/pojo/Pojo.ftl ?

could you show the way you call it ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 2:01 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
max wrote:
is it in <templatepath>/Pojo.ftl or templatepath>/pojo/Pojo.ftl ?

It's in templatepath>/pojo/Pojo.ftl. <templatepath> is set in my Ant xml file. I tested it with calling getTemplatePaths() which returned <tempatepath>/pojo. This seems to be correct for me.

max wrote:
could you show the way you call it ?


In my custom exporter I override the method
Code:
exportPOJO(Map, POJOClass)
which does some checks if the file to be created already exists and so on. The
Code:
super.exportPOJO(Map, POJOClass)
is called.

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 2:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not what your custom exporter does, but how you "run" it. e.g. via ant or eclipse

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 3:04 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
max wrote:
not what your custom exporter does, but how you "run" it. e.g. via ant or eclipse


I run it via Ant.

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 3:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, but *how*, e.g. how do your <hibernatetool> "call" looks like.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 3:52 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
max wrote:
yes, but *how*, e.g. how do your <hibernatetool> "call" looks like.


Ok, hope this helps. :-)

My Ant xml look like this:

Code:
    <target name="hibernatetool.properties" depends="">
        <property name="hibernatetool.destdir" value="${basedir}/demo" />
        <property name="hibernatetool.template.path" value="${basedir}/freemarker" />
        <property name="hibernatetool.configuration" value="${basedir}/hibernate.cfg.xml" />
        <property name="hibernatetool.reveng.strategy" value="package.to.my.CustomReverseEngineeringStrategy" />
        <property name="hibernatetool.reveng.package" value="package.to.my.persistence.classes" />
        <property name="hibernatetool.reveng.revengfile" value="${basedir}/hibernate.reveng.xml" />
        <property name="hibernatetool.use.jdk5" value="true" />
        <property name="hibernatetool.use.ejb3" value="true" />
    </target>

    <target name="hibernatetool" depends="task.define,hibernatetool.properties">
        <hibernatetool
            destdir="${hibernatetool.destdir}"
            templatepath="${hibernatetool.template.path}/pojo">

            <classpath>
                <pathelement path="${developer.working.directory.classes}" />
                <path refid="lib.external.class.path" />
                <path refid="ant.class.path" />
            </classpath>

            <jdbcconfiguration
                configurationfile="${hibernatetool.configuration}"
                reversestrategy="${hibernatetool.reveng.strategy}"
                packagename="${hibernatetool.reveng.package}.base"
                revengfile="${hibernatetool.reveng.revengfile}"
            />

            <hbmtemplate
                exporterclass="package.to.my.CustomExporter"
                >
                <property key="filepattern" value="{package-name}/{class-name}.java" />
                <property key="template" value="Pojo.ftl" />
                <property key="jdk5" value="${hibernatetool.use.jdk5}" />
                <property key="ejb3" value="${hibernatetool.use.ejb3}" />
            </hbmtemplate>
        </hibernatetool>
    </target>


If I set fiepattern as an attribute for hbmtemplate like this
Code:
<hbmtemplate
     exporterclass="package.to.my.CustomExporter"
     filepattern="{package-name}/{class-name}.java"
                >
...


I receive an exception
Code:
org.hibernate.tool.hbm2x.ExporterException: File pattern not set on GenericExporter
. If I set it as a property this exception does not occur. Simmilar for template.

In my custom exporter I override doStart() and call

Code:
super.setFilePattern(super.getProperties().getProperty("filepattern"));
super.setTemplateName(super.getProperties().getProperty("template"));

super.doStart();

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 4:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
try and do templatepath="${hibernatetool.template.path}" instead of
templatepath="${hibernatetool.template.path}/pojo"

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 4:36 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
Ok, now it works - and again I am the stupid guy. ;-) I looked at the xml for several days now and NEVER recoginzed the templatepath attribute in the hibernatetool tag. Sorry for that.

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 7:19 am 
Newbie

Joined: Tue Sep 26, 2006 6:42 am
Posts: 1
I'd just posted a problem in this regards which I could immediately resolve it ;-) Sorry!


Top
 Profile  
 
 Post subject: Need more help
PostPosted: Sat Dec 09, 2006 12:07 pm 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Hey,
i'm totally new in things like hibernate and i think i need help. I wanna generate my DAO-objets with hbmtemplate, cause the hbm2dao does generate a dao i dont need...

i have eclipse with hibernatetools and i use ANT. I have a file called dao.ftl, which starts with

Code:
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}


And if i wanna generate it with my build.xml

Code:
      <hibernatetool destdir="${web.dir}/src">
         <configuration>
            <fileset dir="${model.dir}">
               <include name="**/*.hbm.xml"/>
            </fileset>
            </configuration>
         <hbm2java jdk5="true"/>
         <!--<hbm2dao destdir="${web.dir}/src"/>-->
         <hbmtemplate
             templateprefix="pojo"
            filepattern="model/*.java"
            templatepath="${web.dir}/tmpl"
            template="pojo.ftl"  >
            <property key="jdk5" value="true" />
            <property key="ejb3" value="false" />         
         </hbmtemplate>
      </hibernatetool>


then i get an exception that is called :
Expression pojo is undefined on line 1, column 3 in pojo.ftl..

actually this sounds like an easy error, but like i said, i'm new...
Hope for help

Dosi


Last edited by Dosihris on Sat Dec 09, 2006 1:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Need more help
PostPosted: Sat Dec 09, 2006 12:07 pm 
Beginner
Beginner

Joined: Sat Dec 09, 2006 12:02 pm
Posts: 26
Hey,
i'm totally new in things like hibernate and i think i need help. I wanna generate my DAO-objets with hbmtemplate, cause the hbm2dao does generate a dao i dont need...

i have eclipse with hibernatetools and i use ANT. I have a file called dao.ftl, which starts with

Code:
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}


And if i wanna generate it with my build.xml

Code:
      <hibernatetool destdir="${web.dir}/src">
         <configuration>
            <fileset dir="${model.dir}">
               <include name="**/*.hbm.xml"/>
            </fileset>
            </configuration>
         <hbm2java jdk5="true"/>
         <!--<hbm2dao destdir="${web.dir}/src"/>-->
         <hbmtemplate
             templateprefix="pojo"
            filepattern="model/*.java"
            templatepath="${web.dir}/tmpl"
            template="pojo.ftl"  >
            <property key="jdk5" value="true" />
            <property key="ejb3" value="false" />         
         </hbmtemplate>
      </hibernatetool>


then i get an exception that is called :
Expression pojo is undefined on line 1, column 3 in pojo.ftl..

actually this sounds like an easy error, but like i said, i'm new...
Hope for help

Dosi


Top
 Profile  
 
 Post subject: Re: Need more help
PostPosted: Sat Dec 09, 2006 9:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Dosihris wrote:
Code:
      <hibernatetool destdir="${web.dir}/src">
         <configuration>
            <fileset dir="${model.dir}">
               <include name="**/*.hbm.xml"/>
            </fileset>
            </configuration>
         <hbm2java jdk5="true"/>
         <!--<hbm2dao destdir="${web.dir}/src"/>-->
         <hbmtemplate
             templateprefix="pojo"
            filepattern="model/*.java"
            templatepath="${web.dir}/tmpl"
            template="pojo.ftl"  >
            <property key="jdk5" value="true" />
            <property key="ejb3" value="false" />         
         </hbmtemplate>
      </hibernatetool>


then i get an exception that is called :
Expression pojo is undefined on line 1, column 3 in pojo.ftl..


Multiple problems ;)

you say your template is named dao.ftl but uses template pojo.ftl in build.xml

you use a filepattern that does not mean anything "model/*.java" just result in you being given a $cfg object and only generates one file.

you probably want to use something like "{package-name}/{class-name}.java"

...

_________________
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.  [ 15 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.