-->
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.  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: hbm2java and POJO base class / interface
PostPosted: Sat Aug 05, 2006 2:09 am 
Newbie

Joined: Mon Nov 07, 2005 6:50 am
Posts: 2
Hi All,

I am trying to use hbm2java Ant task include in Hibernate Tools 3.2 Beta 6 and I would like to generate POJOs that hava a base class and a custom interface. E.g. for Customer entity I would like to generate the following:

- POJO class Customer that extends BaseCustomer and implements ICustomer

- BaseCustomer

- ICustomer interface

I know that there is interface meta tag in .hbm file that would cause the tool to generate an interface instead of a POJO class. I would like to generate both.
Is it possible to achive the above without creating custom templates?

Thanks

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 05, 2006 3:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no not at the moment.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 2:35 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Of course, you only have to override one template file, PojoTypeDeclaration.ftl.

e.g.
Code:
/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
*/
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} extends Base${pojo.getDeclarationName()} implements I${pojo.getDeclarationName()}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 2:50 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
As far as generating the base class and interface, you will still need to do an hbmtemplate Ant task (I only use Ant, so do not know about the plugin). So far, I've been able to generate the mapping, configuration, pojo, dao interfaces, dao implementations, Spring configuration file, Struts config, dao unit tests and skeletal CRUD jsp's without too much trouble.

The biggest PITA so far has been the Spring configuration file, for which I needed to create a custom Exporter (based on the HibernateConfigurationExporter class).

Dauntingly enough, this code generation is for a 428 table application. It ended up generating over 3000 files in just a few minutes.

Learn Freemarker, it's worth the effort! I swear!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why did you have to write your custom exporter for the spring cfg file exporter ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:09 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Actually, I was just doing it that way to familiarize myself with the various classes/tools/interfaces in the hibernate-tools package. At the moment, I am converting it to a standalone Freemarker template, perhaps you would like the results after it is complete? Right now, it only supports the Spring 2.0RC2 nomenclature and schemas, but it could always be backported to Spring 1.x.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, would be interesting to see.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:24 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I'll let you know when it is complete (couple of days probably, pretty busy at work ATM).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 11:18 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Max:

I have a first go round for the Spring2 configuration file template if you would like to see it. Just let me know the manner in which I can send it. There are several tricks in there which are pretty nifty for the future development.

Kind of ugly at the moment (17 parameters, eek!), but still a work in progress. I should probably code a custom exporter to verify some of the parameter requirements, along with an ExporterTask defining the aforementioned properties.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 1:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
send it to me (email available on the left side).

I like to see it to get input on what could go into a externally created exporter and hopefully make it so its easy to hook it in and e.g. don't add too many dependencies in the tools core... i also hope to get the seam related generation seperated out to make it easier to maintain.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Me too
PostPosted: Thu Aug 31, 2006 5:16 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
Ananasi wrote:
So far, I've been able to generate the mapping, configuration, pojo, dao interfaces, dao implementations, Spring configuration file, Struts config, dao unit tests and skeletal CRUD jsp's without too much trouble.


These would solve several of the problems I am dealing with. Would you be willing to send me over the files and setup for these?

Thanks,
Arnon


Top
 Profile  
 
 Post subject: Re: Me too
PostPosted: Wed Feb 07, 2007 11:29 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Ananasi's files would also solve my problems! Would you be willing to send me over the files and setup for these too?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 1:45 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Well, it's a work in progress, but here are some examples:

Strategy file (snippets):
Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {

   // Exclude these columns from being mapped in the pojos
   private static String[] excludePojoColumnNames = {
         "CREATED_BY", // superclassed auditing information
         "CREATED_DATE", // superclassed auditing information
         "MODIFIED_BY", // superclassed auditing information
         "MODIFIED_DATE" // superclassed auditing information
   };

   static {
      Arrays.sort(excludePojoColumnNames);
   }

   @Override
   public Map tableToMetaAttributes(TableIdentifier tableIdentifier) {
      Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();

      // Generate the class-code meta attribute to add a default
      // serialVersionUID
      MetaAttribute classCode = new MetaAttribute("class-code");
      classCode.addValue("private static final long serialVersionUID = 1L;");
      metaAttributes.put("class-code", classCode);

      // Generate the extends meta attribute to extend all domain classes from
      // BaseModel
      MetaAttribute extendsCode = new MetaAttribute("extends");
      extendsCode.addValue("BaseModel");
      metaAttributes.put("extends", extendsCode);
      return metaAttributes;
   }

   @Override
   public Map columnToMetaAttributes(TableIdentifier identifier, String column) {
      Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();
      // Suppress pojo mapping of specified column names
      if (Arrays.binarySearch(excludePojoColumnNames, column) >= 0) {
         MetaAttribute genProperty = new MetaAttribute("gen-property");
         genProperty.addValue("false");
         metaAttributes.put("gen-property", genProperty);
      }

      return metaAttributes;
   }
}
As you can see, I'm generating meta attributes to extend the classes from BaseModel, generate class code for the serialVersionUID and supressing the mappings for column names designated in the excludePojoColumnNames array.

dao_intf.ftl:
Code:
package org.package.${project_name}.framework.dao;
// Generated ${date}
<#assign modelName = pojo.importType(pojo.getQualifiedDeclarationName())>
<#assign thisClassName = "${modelName}Dao">
<#assign classbody>import org.package.${project_name}.framework.model.*;

/**
* Dao interface for domain model class ${modelName}
*/
public interface ${thisClassName} extends BaseDao<${modelName},${pojo.importType(c2j.getJavaTypeName(clazz.identifierProperty, false))}> {
}
</#assign>

${pojo.generateImports()}
${classbody}
This template could be used to generate the dao interfaces.

dao_hibernate.ftl:
Code:
package org.package.${project_name}.framework.dao.hibernate;
// Generated ${date}
<#assign modelName = pojo.importType(pojo.getQualifiedDeclarationName())>
<#assign thisClassName = "${modelName}DaoHibernate">
<#assign pojoInstanceName = "${modelName?uncap_first}">
<#assign daoInterfaceName = pojo.importType("org.package.${project_name}.framework.dao.${modelName}Dao")>
<#assign criterionClass = pojo.importType("org.hibernate.criterion.Criterion")>
<#assign classbody>import org.package.${project_name}.framework.model.*;

/**
* Dao implementation for domain model class ${modelName}.
*/
public class ${thisClassName} extends ${pojo.importType("org.package.${project_name}.framework.dao.hibernate.BaseHibernateDao")}<${modelName},${pojo.importType(c2j.getJavaTypeName(clazz.identifierProperty, false))}> implements ${daoInterfaceName} {
   public static final long serialVersionUID = 1L;
   
   /* ************************************** */
   /* Criterion building methods             */
   /* ************************************** */
   <#foreach property in pojo.getAllPropertiesIterator()>
      <#if (!c2h.isCollection(property)) && (property.getName() != "id")><#assign propertyType = pojo.importType(c2j.getJavaTypeName(property, true))>
   protected final ${criterionClass} build${pojo.getPropertyName(property)}ValueRestriction(${propertyType} ${property.getName()}) {
      return buildPropertyValueRestriction("${property.getName()}", ${property.getName()});
   }

   protected final ${criterionClass} build${pojo.getPropertyName(property)}RangeRestriction(${propertyType} startValue, ${propertyType} endValue) {
      return buildPropertyRangeRestriction("${property.getName()}", startValue, endValue);
   }
   
      </#if>
   </#foreach>
   
}
</#assign>

${pojo.generateImports()}
${classbody}
This template could be used to generate the Hibernate specific DAO implementations. I no longer use this, but when I did, it generated Criterion builder methods.

The Spring configuration templates are way too large to post here.

I use the Ant tasks to build the project, but the build.xml and buld.properties are very large. Here are some snippets from the build.xml (note: most variables are set in the build.properties file):
Code:
   <!-- ================================================================
       target: mapping
         The target to generate mapping (hbm.xml) files and the
         Hibernate configuration file (used for configuration of
         other hbm2x tasks)
         ================================================================ -->
   <target name="mapping" depends="init" description="Generate Hibernate mapping files, Hibernate configuration file and the Spring configuration file">
      <echo message="***** Building the mapping (*.hbm.xml) and configuration (hibernate.cfg.xml, applicationContext.xml) files... *****"/>
      <hibernatetool destdir="${dir.javasource}">
            <jdbcconfiguration packagename="org.package.${project.name}.framework.model" propertyfile="build.properties" reversestrategy="org.package.${project.name}.reveng.Strategy" revengfile="hibernate.reveng.xml"/>
            <hbm2hbmxml templatepath="${dir.template}"/>
         <hbm2cfgxml destdir="${dir.dest.cfgxml}"/>
        </hibernatetool>
      <sequential>
         <echo message="***** ...mapping and configuration files sucessfully generated, refreshing... *****"/>
         <antcall target="refresh"/>
         <echo message="***** ...complete! *****"/>
      </sequential>
   </target>
   
   <!-- ================================================================
       target: pojo
         The target to generate the POJOs for the Hibernate mapping
         files. Note that the Hibernate configuration file must have
         already been generated/supplied
       ================================================================ -->
   <target name="pojo" depends="init" description="Generate the Hibernate domain classes (POJO's)">
      <echo message="***** Building the POJO files... *****"/>
      <hibernatetool destdir="${dir.javasource}">
            <configuration configurationfile="${dir.dest.cfgxml}/hibernate.cfg.xml"/>
         <hbm2java templatepath="${dir.template}" jdk5="true"/>
        </hibernatetool>
      <sequential>
         <echo message="***** ...POJO files sucessfully generated, refreshing... *****"/>
         <antcall target="refresh"/>
         <echo message="***** ...POJO file generation complete! *****"/>
      </sequential>
   </target>
   
   <!-- ================================================================
       target: dao
         The target to generate the DAO interface and implementation
         classes. note that the hibernate configuration gfile must
         have already been generated/supplied
       ================================================================ -->
   <target name="dao" depends="init" description="Generate the DAO interface and implementation classes">
      <echo message="***** Building the DAO interface and implementation files... *****"/>
      <hibernatetool>
            <configuration configurationfile="${dir.javasource}/hibernate.cfg.xml"/>
         <hbmtemplate
            templatepath="${dir.template.dao}"
            template="${dao.intf.templatename}"
            destdir="${dir.dest.dao.interface}"
            filepattern="${dao.intf.filepattern}">
            <propertyset refid="dao.properties"/>
         </hbmtemplate>
         <hbmtemplate
            templatepath="${dir.template.dao}"
            template="${dao.hibernate.templatename}"
            destdir="${dir.dest.dao.hibernate}"
            filepattern="${dao.hibernate.filepattern}">
            <propertyset refid="dao.properties"/>
         </hbmtemplate>
      </hibernatetool>
      <sequential>
         <echo message="***** ...DAO interface and implementation files sucessfully generated, refreshing... *****"/>
         <antcall target="refresh"/>
         <echo message="***** ...DAO interface and implementation file generation complete! *****"/>
      </sequential>
    </target>
   
   <!-- ================================================================
       target: spring
         The target to generate the Spring configuration file. Note
         that the Hibernate configuration file must have already been
         generated/supplied
        ================================================================ -->
   <target name="spring" depends="init" description="Generate the Spring configuration file">
      <echo message="***** Building the Spring configuration file ... *****"/>
      <echo message="Building templates for the following: ${spring.templates}"/>
      <for list="${spring.templates}" param="templatename">
         <sequential>
            <hibernatetool>
                  <configuration configurationfile="${dir.javasource}/hibernate.cfg.xml"/>
                     <hbmtemplate
                        templatepath="${dir.template.spring}"
                        template="spring-@{templatename}.ftl"
                        destdir="${dir.dest.spring}"
                        filepattern="applicationContext-@{templatename}.xml">
                        <propertyset refid="spring.properties"/>
                     </hbmtemplate>
            </hibernatetool>
         </sequential>
      </for>
      <sequential>
         <echo message="***** ...Spring configuration file sucessfully generated, refreshing *****"/>
         <antcall target="refresh"/>
         <echo message="***** ...Spring configuration file generation complete! *****"/>
      </sequential>
   </target>


This is just a taste of my reverse engineering configurations, let me know how much more information you need.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 9:41 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
@Ananasi

Hi, I followed the thread and would appreciate if you sent me the Spring configuration file templates (*.ftl).

I am about to create my own ant script to generate those things.

Thanks in advance!
rapthor(at)lycos.de


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 27, 2007 6:04 am 
Regular
Regular

Joined: Wed Nov 01, 2006 2:17 pm
Posts: 78
Unfortunately, I did not receive any templates yet.

I wonder how to achieve the creation of only 1 file, that is extended each time a DAO-bean is inserted. How does a Freemarker ftl template in this case look like?

Basically what I want to achieve is one Spring configuration xml file, that holds all DAO-beans and not one xml file per bean.

I'd appreciate if someone helped me.


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