-->
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.  [ 12 posts ] 
Author Message
 Post subject: Use of hbmtemplate exporterclass; ImportContext
PostPosted: Thu Aug 24, 2006 5:44 am 
Newbie

Joined: Thu Aug 24, 2006 4:42 am
Posts: 15
Hi all,

I'd like to build new freemarker templates using generic DAO model as described in the demo CaveatEmptor-HIA-SE and the article of Christian Bauer at http://www.hibernate.org/328.html.

* The first problem I have is to put the new class in a different package than the POJO. Looking at the tools source ( svn checked out on August 20th) I discovered that the imports where always relative to the POJO package, through the basePackage value.

Instead of trying to modify the hibernate-tools source code, I decided to use the exporterclass feature of hbmtemplate tool, and experiment with my custom exporter. My exporter extends POJOExporter (like DAOExporter), and is using its own ImportContextImpl and supplies exporter.ImportType() method for use in template.

* Now I can generate a DAOFactory.java with my exporter, either:
  • from junit test: using it directly
    Code:
      MicExporter exporter = new MicExporter(cfg, outputdir);
    ...
      exporter.start();

  • from junit test: using it through GenericExporter's exporterclass
    Code:
      GenericExporter exporter = new GenericExporter(cfg,outputdir);
      exporter.setExporterClassName("rockyroad.hbm2x.MicExporter");
    ...
      exporter.start();

  • from junit test: using it through GenericExporterTask
    Code:
      public void testAntMic() {
        org.apache.tools.ant.Project prj = new Project();
        HibernateToolTask task = new HibernateToolTask();
        task.setDestDir(new File("../generated"));
        task.setClasspath(new Path(prj, "../generated"));
        ConfigurationTask tcfg = task.createConfiguration();
        tcfg.setConfigurationFile(
            new File("../generated/hibernate.cfg.xml"));
        task.getConfiguration();
        final Variable pn = new Variable();
        pn.setKey("packagename");
        pn.setValue("rockyroad.Bank");
        task.addConfiguredProperty(pn);
       
        GenericExporterTask tool = new GenericExporterTask(task);
             tool.setFilePattern("rockyroad/Bank/dao/hibernate/DAOFactory.java");
            tool.setTemplatePath(new Path(prj,"../templates"));
            tool.setTemplate("dao/DAOFactory.java.ftl");
            tool.setExporterClass("rockyroad.hbm2x.MicExporter");
          tool.execute();
      }


  • but when I try to run Ant itself, instead of DAOFactory I get the POJOS
    generated, here's the trace:
    Code:
    daoMaster:
    [hibernatetool] Executing Hibernate Tool with a Standard Configuration
    [hibernatetool] 1. task: generic exporterrockyroad.hbm2x.MicExporter
    [hibernatetool] 09:51:22,328  INFO Environment:500 - Hibernate 3.2 cr3
    [hibernatetool] 09:51:22,328  INFO Environment:518 - loaded properties from resource hibernate.properties: {hibernate.connection.username=tester, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql:///test, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver}
    [hibernatetool] 09:51:22,328  INFO Environment:667 - Bytecode provider name : cglib
    [hibernatetool] 09:51:22,359  INFO Environment:584 - using JDK 1.4 java.sql.Timestamp handling
    [hibernatetool] 09:51:22,531  INFO Configuration:1384 - configuring from file: hibernate.cfg.xml
    [hibernatetool] 09:51:23,093  INFO Configuration:507 - Reading mappings from resource: rockyroad/Bank/Statement.hbm.xml
    [hibernatetool] 09:51:23,296  INFO HbmBinder:300 - Mapping class: rockyroad.Bank.Statement -> statement
    ...
    [hibernatetool] 09:51:23,718  INFO Configuration:1465 - Configured SessionFactory: null
    [hibernatetool] 09:51:23,718  INFO HbmBinder:2375 - Mapping collection: rockyroad.Bank.Balance.reportsForEndsOn -> report
    ...
    [hibernatetool] 09:51:23,765 DEBUG MicExporter:132 - rockyroad.hbm2x.MicExporter outputdir:E:\Devlp\Eclipse\workspace\MyBcq\generated templatePrefix: pojo/ path: [E:\Devlp\Eclipse\workspace\MyBcq\Templates]
    [hibernatetool] 09:51:23,953  INFO Version:15 - Hibernate Tools 3.2.0.beta6a
    [hibernatetool] 09:51:24,765  INFO TemplateProducer:40 - Writing Pojo to E:\Devlp\Eclipse\workspace\MyBcq\generated\rockyroad\Bank\Balance.java
    [hibernatetool] 09:51:24,781  INFO TemplateProducer:40 - Writing Pojo to E:\Devlp\Eclipse\workspace\MyBcq\generated\rockyroad\Bank\Statement.java
    [hibernatetool] 09:51:24,796  INFO TemplateProducer:40 - Writing Pojo to E:\Devlp\Eclipse\workspace\MyBcq\generated\rockyroad\Bank\Report.java
    [hibernatetool] 09:51:24,828  INFO TemplateProducer:40 - Writing Pojo to E:\Devlp\Eclipse\workspace\MyBcq\generated\rockyroad\Bank\Account.java
    BUILD SUCCESSFUL
    Total time: 3 seconds

the build target is:
Code:
  <target name="daoMaster">
    <hibernatetool destdir="${generated}" classpath="${my.classpath}">
      <configuration configurationfile="${generated}/hibernate.cfg.xml" />
      <property key="packagename" value="rockyroad.Bank" />
      <hbmtemplate filepattern="rockyroad/Bank/dao/hibernate/DAOFactory.java"
                   templatepath="${templates}"
                   template="dao/DAOFactory.java.ftl"
              templateprefix=""
                   exporterclass="rockyroad.hbm2x.MicExporter">
        <property key="jdk5" value="true" />
        <property key="ejb3" value="false" />
      </hbmtemplate>
    </hibernatetool>
  </target>


BTW, I don't clearly understand the role of templateprefix.

Could you help me ?

TIA
Michelle


Top
 Profile  
 
 Post subject: Re: Use of hbmtemplate exporterclass; ImportContext
PostPosted: Thu Aug 24, 2006 6:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Rocky Road wrote:
I'd like to build new freemarker templates using generic DAO model as described in the demo CaveatEmptor-HIA-SE and the article of Christian Bauer at http://www.hibernate.org/328.html.


something you want to contribute ? :)

Quote:
* The first problem I have is to put the new class in a different package than the POJO. Looking at the tools source ( svn checked out on August 20th) I discovered that the imports where always relative to the POJO package, through the basePackage value.


which basePackage ? (just to be sure i know what you refer to)

Quote:
Instead of trying to modify the hibernate-tools source code, I decided to use the exporterclass feature of hbmtemplate tool, and experiment with my custom exporter. My exporter extends POJOExporter (like DAOExporter), and is using its own ImportContextImpl and supplies exporter.ImportType() method for use in template.


ok - don't really understand why you need that for ?

Quote:
[*] but when I try to run Ant itself, instead of DAOFactory I get the POJOS
generated, here's the trace:


it should work. have you tried to just set exporterclass and not the template ?

Quote:
BTW, I don't clearly understand the role of templateprefix.


it's just to allow modularizationof the templates.

so if you look in the jar you will see we don't put all templates in one directory but in dao/, pojo/ etc. and those prefixes are used when the processing looksup a template. so if you refer to "daohome.ftl" it will first look in dao/ and then afterwards in the root.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Use of hbmtemplate exporterclass; ImportContext
PostPosted: Thu Aug 24, 2006 8:11 am 
Newbie

Joined: Thu Aug 24, 2006 4:42 am
Posts: 15
Hi Max,

First, thanks for your work on hibernate and user support.

max wrote:
something you want to contribute ? :)

Why not ? with the LGPL conventions of course :-)
But let's write something working first ;)


Quote:
which basePackage ? (just to be sure i know what you refer to)


I mean org.hibernate.tool.hbm2x.pojo.ImportContextImpl.basePackage, initialized by BasicPOJOClass to the POJO package. That's fine for generating POJOs, but not necessarily the expected thing when generating dao.


pojo.ImportType() is a feature of the Interface ...hbm2x.pojo.ImportContext implemented in *.hbm2x.pojo.ImportContextImpl . An instance of it is associated with the BasicPOJOClass, and the method is relayed there.
My exporter could associate and relay another instance of ImportContextImpl, initialized with the correct destination package.

Quote:
ok - don't really understand why you need that for ?


First, to generate dao classes in a different package than the POJO, but relative to it, ant get correct package declaration and imports.
Then, whatever extra-need for my new templates.
If they prove useful, I would propose to merge my work with main trunk.

Quote:
it should work. have you tried to just set exporterclass and not the template ?

Same result. The weird thing is that the trace instructions I put in both constructors don't show up in ant log, whilst they do in Junit testAntMic().

Code:
114:05:25,015  INFO HbmBinder:2375 - Mapping collection: michelp.Bank.Report.statements -> statement
14:05:25,015  INFO HbmBinder:2375 - Mapping collection: michelp.Bank.Account.reports -> report
14:05:25,046  INFO MicExporter:30 - MicExporter instance created default constructor, template is
14:05:25,046  INFO MicExporter:114 - TemplateName set to DAOFactory.java.ftl
14:05:25,046  INFO MicExporter:134 - OutputDirectory set to ..\generated
14:05:25,046  INFO MicExporter:120 - TemplatePath set to [E:\Devlp\Eclipse\workspace\MyBcq\bin\..\templates]
14:05:25,046 DEBUG MicExporter:93 - start() called
14:05:25,046  INFO MicExporter:114 - TemplateName set to dao/DAOFactory.java.ftl
14:05:25,046  INFO MicExporter:108 - FilePattern set to michelp/Bank/dao/hibernate/DAOFactory.java
14:05:25,046 DEBUG MicExporter:100 - calling super.start()
14:05:25,046 DEBUG MicExporter:132 - michelp.hbm2x.MicExporter outputdir:..\generated templatePrefix: null path: [E:\Devlp\Eclipse\workspace\MyBcq\bin\..\templates]
14:05:25,125  INFO Version:15 - Hibernate Tools 3.2.0.beta7
14:05:25,359  INFO TemplateProducer:40 - Writing dao/DAOFactory.java.ftl to E:\Devlp\Eclipse\workspace\MyBcq\bin\..\generated\michelp\Bank\dao\hibernate\DAOFactory.java



I forgot to tell about an important fix: I had to override (in MicExporter) the start() method as follows:
Code:
   @Override
   public void start() {
      // Copy properties missing after GenericExporter.start()
      log.debug("start() called");
      Properties p = getProperties();
      setTemplateName(p.getProperty(ExporterSettings.PREFIX_KEY
            + "template_name"));
      setFilePattern(p.getProperty(ExporterSettings.PREFIX_KEY
            + "file_pattern"));
      // setTemplatePrefix(???)); // Dunno how to retrieve it from here
      log.debug("calling super.start()");
      super.start();
   }


Also I wonder why the new exporter class is created so late in process;
wouldn't be possible to avoid those properties copiing ...
did I miss something ?

--
Michelle


Top
 Profile  
 
 Post subject: Re: Use of hbmtemplate exporterclass; ImportContext
PostPosted: Thu Aug 24, 2006 8:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Rocky Road wrote:
Quote:
which basePackage ? (just to be sure i know what you refer to)


I mean org.hibernate.tool.hbm2x.pojo.ImportContextImpl.basePackage, initialized by BasicPOJOClass to the POJO package. That's fine for generating POJOs, but not necessarily the expected thing when generating dao.


why are you using the *pojo*'s import context and not your *own* importcontext ?

Quote:
Quote:
it should work. have you tried to just set exporterclass and not the template ?

Same result. The weird thing is that the trace instructions I put in both constructors don't show up in ant log, whilst they do in Junit testAntMic().


try and see if you could reduce it to a minimal testcase and submit it to jira.


Quote:
I forgot to tell about an important fix: I had to override (in MicExporter) the start() method as follows:
Code:
   @Override
   public void start() {
      // Copy properties missing after GenericExporter.start()
      log.debug("start() called");
      Properties p = getProperties();
      setTemplateName(p.getProperty(ExporterSettings.PREFIX_KEY
            + "template_name"));
      setFilePattern(p.getProperty(ExporterSettings.PREFIX_KEY
            + "file_pattern"));
      // setTemplatePrefix(???)); // Dunno how to retrieve it from here
      log.debug("calling super.start()");
      super.start();
   }


Also I wonder why the new exporter class is created so late in process;
wouldn't be possible to avoid those properties copiing ...
did I miss something ?


what does it fix?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 12:51 pm 
Newbie

Joined: Thu Aug 24, 2006 4:42 am
Posts: 15
max wrote:
why are you using the *pojo*'s import context and not your *own* importcontext ?

Are you kidding or do I write chinese ?
I'm writing my *own* Exporter, using *your* importcontext, because *pojo*'s doesn't work !!!

Quote:
try and see if you could reduce it to a minimal testcase and submit it to jira.

I'm not used to write Ant tasks test cases, I'm working at it ...

Quote:
what does it fix?

Sorry, I thought it'd be obvious: the custom exporter couldn't work because, when called from ant task, it is instanciated in GenericExporter.start() -- please have a look at it -- and *there*, vital properties like TemplateName and FilePattern are *not* copied from GenericExporter instance to new Exporter instance.

IMHO, I'd prefer if the custom exporter class was created directly from AntTask, through a well defined constructor.
It should be easy (no need to use all kinds of java.util.Properties objects, of ReflectHelper use) if the custom explorer class what bound to implement a rewritten Exporter interface, with all essential properties made available though getters and setters, and suitable constructors and methods to configure and launch it.
--
Michelle


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 1:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Maybe not chinese, but non-precise ;)

anyhow - if something is not working/could be better then provide a patch.
code is precise ;)

and i guess the problem you have is that getProperties() instead of p is used. that looks like a bug yes; but a copy is indeed needed since the "owning" exporter should not be affected by the "child".

but i want to clean some of it up.

and the reason why anttasks is not doing what GenericExporter do directly is ecause it is intended to be used from within eclipse too.l

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 1:49 pm 
Newbie

Joined: Thu Aug 24, 2006 4:42 am
Posts: 15
max wrote:
and i guess the problem you have is that getProperties() instead of p is used. that looks like a bug yes; but a copy is indeed needed since the "owning" exporter should not be affected by the "child".

???

Quote:

and the reason why anttasks is not doing what GenericExporter do directly is ecause it is intended to be used from within eclipse too.l

That's not at all what I meant...

Quote:
but i want to clean some of it up.

ok, I'll try to work on my side.
Let me know...

Thanks again.
---
Best regards
Michelle


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
then express what you mean in a diff/patch; i'm apparently not reading your words correctly.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Solved!
PostPosted: Fri Aug 25, 2006 1:30 pm 
Newbie

Joined: Thu Aug 24, 2006 4:42 am
Posts: 15
Hi max,

I finally found out why the ant build didn't behave the same way as junit tests: different classpaths!

About the GenericExporter problems, I wrote a mini project and patch
and submitted to JIRA

http://opensource.atlassian.com/project ... se/HBX-737

Only GenericExporterTask is changed, so there should be no footprint on eclipse plugins.

what do you think ?

--
Best regards

Michelle


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 05, 2008 6:26 pm 
Regular
Regular

Joined: Tue Jul 29, 2008 4:15 pm
Posts: 62
Location: Dallas, TX US
This post has really help guide me in the right direction as I am trying to use the GenericExporter of the Hibernate Tools to generated Custom DAOs. I realize this post is about 2 years old and it seems there have been major improvement to enable customization from inside the Hibernate Tools plugin. So I am wonder if there is a way to make the DAOs implement a DAO interface using the Generic Exporter?

So far I have been able to accomplish the following:
    Renaming the DAOs
    Creating a different package structure for the DAOs
    Added Named Queries to the DAOs


The above list was accomplished by setting up the following properties in the Generic Exporter of the Hibernate Tools:

File Pattern: {package-name}/../dao/{class-name}DAO.java
Output directory: generated
Template name: dao/daohome.ftl
jdk5: true
edj3: false
sessionFactoryName: SessionFactory

Thanks!

_________________
pouncilt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 2:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
pouncilt - please don't double post or revive threads for something that is already being discussed elsewhere.

Thanks

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 3:36 pm 
Regular
Regular

Joined: Tue Jul 29, 2008 4:15 pm
Posts: 62
Location: Dallas, TX US
Ok sorry!

_________________
pouncilt


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.