-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to invoke hbm2java from a java file.
PostPosted: Fri Apr 18, 2008 1:34 am 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
Is it possible to invoke hbm2java from a java file to generate hibernate mapping files and POJOs. I haven't seen any examples on how to do this way.

Appreciate your help.

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 12:23 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Yes it is possible. If you are using an ant build for hbm2java, you can write a simple java code to invoke ant (I have tried it personally). And I guess there is a pure java way to invoke hbm2java without using ant. You have to choose which way to go about.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 2:25 am 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
Hi Sukirtha,

I appreciate your response. It would be helpful if you can give me a sample example.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 3:27 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Build.xml
Code:
      <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
      
      <target name = "hibernatetool" >
         <echo message="Starting hbm generation ..."></echo>
         <hibernatetool>
            <jdbcconfiguration propertyfile="hibernate.properties" revengfile="revenge.xml" packagename="sample">
               
            </jdbcconfiguration>
            <classpath refid="toolslib"/>
            <hbm2hbmxml destdir="new" templatepath="src">
            </hbm2hbmxml>
         </hibernatetool>
         <echo message="Successfully generated hbm files"></echo>
         
         <echo message="Java class generation starting..."></echo>
         <hibernatetool>
            <configuration propertyfile="hibernate.properties">
               <fileset dir="sample" id="id">
                   <include name="*.hbm.xml"/>
               </fileset>
            </configuration>
            <hbm2java destdir="new/java"/>
         </hibernatetool>
         <echo message="Successfully generated java files"></echo>
      </target>


Java code

Code:
File buildFile = new File(file);
      Project p = new Project();
      String path = buildFile.getAbsolutePath();
      path = path.replaceAll("%20", " ");
      p.setUserProperty("ant.file", path);
      DefaultLogger consoleLogger = new DefaultLogger();
      consoleLogger.setErrorPrintStream(System.err);
      consoleLogger.setOutputPrintStream(System.out);
      consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
      p.addBuildListener(consoleLogger);
      try {
         p.fireBuildStarted();
         p.init();
         ProjectHelper helper = ProjectHelper.getProjectHelper();
         p.addReference("ant.projectHelper", helper);
         helper.parse(p, buildFile);
         p.executeTarget(p.getDefaultTarget());
         p.fireBuildFinished(null);
      } catch (BuildException e) {
         p.fireBuildFinished(e);
      }


Don't forget to add all the dependency library files onto your classpath. Hope it helps!

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 12:54 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
this can be done much simpler; If it can be done by ant there must be
a direct way.
to run the schema export for example:

Code:
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
se.create(false, true);


All the credit goes to the book "Java Persistence with Hibernate",
I really recommend it.

regards

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 11:22 pm 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
But I think schemaExport creates and drops tables in a schema based on the mapping changes. The actual question is to generate java files and hbm files from the schema. Do you have any such sample for hbm and java class generation using hbm2java and hbm2hbmxml? If so please share it. I would be interested. Thanks!

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 1:37 am 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
You are right Sukirtha, I am interested in knowing about generating the java and mapping files based on a configuration file.

Also I really want to know about invoking hbm2java without using ant.

Any pointers are greatly appreciated...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 12:26 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Exactly. And Gowri, I suggest you post your doubt in the tools forum as well. That's the forum where all discussions on tools happen.

_________________
Sukirtha


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.