-->
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.  [ 10 posts ] 
Author Message
 Post subject: How generate code similar to those Hibernate Synchronizer
PostPosted: Thu Aug 25, 2005 10:47 am 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
Basically from the hbm files I want the following code generated

1) a pojo java class that extends a base pojo java class. That way if I have custome attributes I can put those in the user overridable pojo java class.

i.e:

Code:
package com.xxx.olbmaint.business.domain;

import com.xxx.olbmaint.business.domain.base.BaseCoveragePackageCode;

/**
* This is the object class that relates to the COVERAGE_PACKAGE_CODE table.
* Any customizations belong here.
*/
public class CoveragePackageCode extends BaseCoveragePackageCode {

/*[CONSTRUCTOR MARKER BEGIN]*/
   public CoveragePackageCode () {
      super();
   }

   /**
    * Constructor for primary key
    */
   public CoveragePackageCode (java.lang.String _coveragePackageCode) {
      super(_coveragePackageCode);
   }

   /**
    * Constructor for required fields
    */
   public CoveragePackageCode (
      java.lang.String _coveragePackageCode,
      java.lang.String _benLimitUr) {

      super (
         _coveragePackageCode,
         _benLimitUr);
   }

/*[CONSTRUCTOR MARKER END]*/
}

Code:

package com.xxx.olbmaint.business.domain.base;

import java.io.Serializable;
/**
* This class has been automatically generated by Hibernate Synchronizer.
* For more information or documentation, visit The Hibernate Synchronizer page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at joe@binamics.com.
*
* This is an object that contains data related to the COVERAGE_PACKAGE_CODE table.
* Do not modify this class because it will be overwritten if the configuration file
* related to this class is modified.
*
* @hibernate.class
*  table="COVERAGE_PACKAGE_CODE"
*/
public abstract class BaseCoveragePackageCode  implements Serializable {

   public static String PROP_COVERAGE_PACKAGE_CODE = "coveragePackageCode";
   public static String PROP_END_DATE = "endDate";
   public static String PROP_CONTAINED_IN_OLB = "containedInOlb";
   public static String PROP_BEN_LIMIT_UR = "benLimitUr";
   public static String PROP_EFFECTIVE_DATE = "effectiveDate";


   private int hashCode = Integer.MIN_VALUE;

   // primary key
   private java.lang.String _coveragePackageCode;

   // fields
   private java.util.Date _effectiveDate;
   private java.lang.String _benLimitUr;
   private java.lang.String _containedInOlb;
   private java.util.Date _endDate;

   // collections
   private java.util.Set _planPackageOdometers;


   // constructors
   public BaseCoveragePackageCode () {
      initialize();
   }

   /**
    * Constructor for primary key
    */
   public BaseCoveragePackageCode (java.lang.String _coveragePackageCode) {
      this.setCoveragePackageCode(_coveragePackageCode);
      initialize();
   }

   /**
    * Constructor for required fields
    */
   public BaseCoveragePackageCode (
      java.lang.String _coveragePackageCode,
      java.lang.String _benLimitUr) {

      this.setCoveragePackageCode(_coveragePackageCode);
      this.setBenLimitUr(_benLimitUr);
      initialize();
   }

   protected void initialize () {}



   /**
    * Return the unique identifier of this class
     * @hibernate.id
     *  generator-class="uuid.hex"
     *  column="COVERAGE_PACKAGE_CODE"
     */
   public java.lang.String getCoveragePackageCode () {
      return _coveragePackageCode;
   }

   /**
    * Set the unique identifier of this class
    * @param _coveragePackageCode the new ID
    */
   public void setCoveragePackageCode (java.lang.String _coveragePackageCode) {
      this._coveragePackageCode = _coveragePackageCode;
      this.hashCode = Integer.MIN_VALUE;
   }


   /**
    * Return the value associated with the column: EFFECTIVE_DATE
    */
   public java.util.Date getEffectiveDate () {
      return _effectiveDate;
   }

   /**
    * Set the value related to the column: EFFECTIVE_DATE
    * @param _effectiveDate the EFFECTIVE_DATE value
    */
   public void setEffectiveDate (java.util.Date _effectiveDate) {
      this._effectiveDate = _effectiveDate;
   }


   /**
    * Return the value associated with the column: BEN_LIMIT_UR
    */
   public java.lang.String getBenLimitUr () {
      return _benLimitUr;
   }

   /**
    * Set the value related to the column: BEN_LIMIT_UR
    * @param _benLimitUr the BEN_LIMIT_UR value
    */
   public void setBenLimitUr (java.lang.String _benLimitUr) {
      this._benLimitUr = _benLimitUr;
   }


   /**
    * Return the value associated with the column: CONTAINED_IN_OLB
    */
   public java.lang.String getContainedInOlb () {
      return _containedInOlb;
   }

   /**
    * Set the value related to the column: CONTAINED_IN_OLB
    * @param _containedInOlb the CONTAINED_IN_OLB value
    */
   public void setContainedInOlb (java.lang.String _containedInOlb) {
      this._containedInOlb = _containedInOlb;
   }


   /**
    * Return the value associated with the column: END_DATE
    */
   public java.util.Date getEndDate () {
      return _endDate;
   }

   /**
    * Set the value related to the column: END_DATE
    * @param _endDate the END_DATE value
    */
   public void setEndDate (java.util.Date _endDate) {
      this._endDate = _endDate;
   }


   /**
    * Return the value associated with the column: planPackageOdometers
    */
   public java.util.Set getPlanPackageOdometers () {
      return this._planPackageOdometers;
   }

   /**
    * Set the value related to the column: planPackageOdometers
    * @param _planPackageOdometers the planPackageOdometers value
    */
   public void setPlanPackageOdometers (java.util.Set _planPackageOdometers) {
      this._planPackageOdometers = _planPackageOdometers;
   }
   
   public void addToPlanPackageOdometers (Object obj) {
      if (null == this._planPackageOdometers) this._planPackageOdometers = new java.util.HashSet();
      this._planPackageOdometers.add(obj);
   }



   public boolean equals (Object obj) {
      if (null == obj) return false;
      if (!(obj instanceof com.bcbsma.olbmaint.business.domain.base.BaseCoveragePackageCode)) return false;
      else {
         com.bcbsma.olbmaint.business.domain.base.BaseCoveragePackageCode mObj = (com.bcbsma.olbmaint.business.domain.base.BaseCoveragePackageCode) obj;
         if (null == this.getCoveragePackageCode() || null == mObj.getCoveragePackageCode()) return false;
         else return (this.getCoveragePackageCode().equals(mObj.getCoveragePackageCode()));
      }
   }


   public int hashCode () {
      if (Integer.MIN_VALUE == this.hashCode) {
         if (null == this.getCoveragePackageCode()) return super.hashCode();
         else {
            String hashStr = this.getClass().getName() + ":" + this.getCoveragePackageCode().hashCode();
            this.hashCode = hashStr.hashCode();
         }
      }
      return this.hashCode;
   }


   public String toString () {
      return super.toString();
   }

}


2) I would like to generate a spring/hibernate/dao structure. This was accomplishable using templates in synchronizer.


i.e:

Code:
:Modified=true
:ResourceType=C
:Description=Dao Impl
-
#set ($package = $class.ValueObjectPackage.replaceAll(".domain",".service.dao"))
package ${package}.hibernate;

import  ${package}.${class.DAOClassName};
import  ${package}.hibernate.base.Base${class.DAOClassName}Impl;

/*
* ${class.DAOClassName}Impl.java
*
* Copyright 2004 by Electronic Data Systems
* Corporation. All rights reserved.
*
* An unpublished work created Aug 23, 2004, 2004. This work is a
* trade secret of EDS and unauthorized use or copying
* is prohibited.
*
*/
public class ${class.DAOClassName}Impl extends Base${class.DAOClassName}Impl implements ${class.DAOClassName} {
// Add methods overriding base class here

}

:Modified=true
:ResourceType=C
:Description=Dao Impl Base
-
#set ($package = $class.ValueObjectPackage.replaceAll(".domain",".service.dao"))
package ${package}.hibernate.base;

import ${class.AbsoluteValueObjectClassName};
import  ${package}.base.Base${class.DAOClassName};
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;



/*
* ${class.DAOClassName}Impl.java
*
* Copyright 2004 by Electronic Data Systems
* Corporation. All rights reserved.
*
* An unpublished work created Aug 23, 2004, 2004. This work is a
* trade secret of EDS and unauthorized use or copying
* is prohibited.
*
*/
public class Base${class.DAOClassName}Impl extends HibernateDaoSupport implements Base${class.DAOClassName} {

#if ($class.ParentRoot.Id)
   public final ${class.ValueObjectClassName} load(${class.ParentRoot.Id.Property.AbsoluteSignatureClassName} key)
    {
       ${class.ValueObjectClassName} obj = (${class.ValueObjectClassName}) getHibernateTemplate().load(${class.ValueObjectClassName}.class, key);
       getHibernateTemplate().initialize(obj);
       return obj;
   }

   /**
    * Persist the given transient instance, first assigning a generated identifier. (Or using the current value
    * of the identifier property if the assigned generator is used.)
    * @param ${class.VarName} a transient instance of a persistent class
    * @return the class identifier
    */
   public final ${class.ParentRoot.Id.Property.ObjectClass} save(${class.ValueObjectClassName} ${class.VarName})
    {
      return (${class.ParentRoot.Id.Property.ObjectClass}) getHibernateTemplate().save(${class.VarName});
     }

#end

   public final void refresh(${class.ValueObjectClassName} ${class.VarName})
    {
       getHibernateTemplate().refresh(${class.VarName});
    }

   public final ${class.ValueObjectClassName} saveOrUpdateCopy(${class.ValueObjectClassName} ${class.VarName})
    {
       return (${class.ValueObjectClassName}) getHibernateTemplate().merge(${class.VarName});
    }

   /**
    * Either save() or update() the given instance, depending upon the value of its identifier property. By default
    * the instance is always saved. This behaviour may be adjusted by specifying an unsaved-value attribute of the
    * identifier property mapping.
    * @param ${class.VarName} a transient instance containing new or updated state
    */
   public final void saveOrUpdate(${class.ValueObjectClassName} ${class.VarName})
    {
       getHibernateTemplate().saveOrUpdate(${class.VarName});
    }


   /**
    * Update the persistent state associated with the given identifier. An exception is thrown if there is a persistent
    * instance with the same identifier in the current session.
    * @param ${class.VarName} a transient instance containing updated state
    */
   public final void update(${class.ValueObjectClassName} ${class.VarName})
    {
       getHibernateTemplate().update(${class.VarName});
    }

   /**
    * Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving
    * Session or a transient instance with an identifier associated with existing persistent state.
    * @param ${class.VarName} the instance to be removed
    */
   public final void delete(${class.ValueObjectClassName} ${class.VarName})
    {
       getHibernateTemplate().delete(${class.VarName});
    }

   /**
    * Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving
    * Session or a transient instance with an identifier associated with existing persistent state.
    * @param ${class.VarName} the instance to be removed
    */
   public final List loadAll()
    {
        String orderBy = "";
        if (null != getDefaultOrderProperty())
       {
         orderBy = "order by lower(theRow." + getDefaultOrderProperty() + ") asc";
        }
      return (List) getHibernateTemplate().find(
                " select theRow from ${class.ValueObjectClassName} as theRow " +
                orderBy);
    }

   /**
    * Return the property of the class you would like to use for default ordering
    * @return the property name
    */
   public String getDefaultOrderProperty () {
      return null;
   }


}


:Modified=true
:ResourceType=C
:Description=Dao Interface
-
#set ($package = $class.ValueObjectPackage.replaceAll(".domain",".service.dao"))
package $package;

import ${package}.base.Base${class.DAOClassName};


/*
* ${class.DAOClassName}.java
*
* Copyright 2004 by Electronic Data Systems
* Corporation. All rights reserved.
*
* An unpublished work created Aug 23, 2004, 2004. This work is a
* trade secret of EDS and unauthorized use or copying
* is prohibited.
*
*/
public interface ${class.DAOClassName} extends Base${class.DAOClassName}{
// Add new methods here


}

:Modified=true
:ResourceType=C
:Description=Dao Interface Base
-
#set ($package = $class.ValueObjectPackage.replaceAll(".domain",".service.dao"))
package ${package}.base;

import ${class.AbsoluteValueObjectClassName};
import java.util.List;

/*
* ${class.DAOClassName}.java
*
* Copyright 2004 by Electronic Data Systems
* Corporation. All rights reserved.
*
* An unpublished work created Aug 23, 2004, 2004. This work is a
* trade secret of EDS and unauthorized use or copying
* is prohibited.
*
*/
public interface Base${class.DAOClassName} {
   

#if ($class.ParentRoot.Id)
   ${class.ValueObjectClassName} load(${class.ParentRoot.Id.Property.AbsoluteSignatureClassName} key);


   /**
    * Persist the given transient instance, first assigning a generated identifier. (Or using the current value
    * of the identifier property if the assigned generator is used.)
    * @param ${class.VarName} a transient instance of a persistent class
    * @return the class identifier
    */
   ${class.ParentRoot.Id.Property.ObjectClass} save(${class.ValueObjectClassName} ${class.VarName});


#end

   void refresh(${class.ValueObjectClassName} ${class.VarName});

  ${class.ValueObjectClassName} saveOrUpdateCopy(${class.ValueObjectClassName} ${class.VarName});

   /**
    * Either save() or update() the given instance, depending upon the value of its identifier property. By default
    * the instance is always saved. This behaviour may be adjusted by specifying an unsaved-value attribute of the
    * identifier property mapping.
    * @param ${class.VarName} a transient instance containing new or updated state
    */
   void saveOrUpdate(${class.ValueObjectClassName} ${class.VarName});


   /**
    * Update the persistent state associated with the given identifier. An exception is thrown if there is a persistent
    * instance with the same identifier in the current session.
    * @param ${class.VarName} a transient instance containing updated state
    */
   void update(${class.ValueObjectClassName} ${class.VarName}) ;



   /**
    * Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving
    * Session or a transient instance with an identifier associated with existing persistent state.
    * @param ${class.VarName} the instance to be removed
    */
   void delete(${class.ValueObjectClassName} ${class.VarName});

   /**
    * Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving
    * Session or a transient instance with an identifier associated with existing persistent state.
    * @param ${class.VarName} the instance to be removed
    */
   List loadAll();



}




How would I go about accomplishing things like this with hibernate tools?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 10:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Have a look at the hibernate tools pojo.vm velocity script and modify it as you see fit. If the result is good then submit the changes to JIRA for consideration.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 10:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Also see this forum thread for more infor and other options.

http://forum.hibernate.org/viewtopic.php?t=946751


Top
 Profile  
 
 Post subject: Re: How generate code similar to those Hibernate Synchronize
PostPosted: Fri Aug 26, 2005 4:16 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
garpinc wrote:
Basically from the hbm files I want the following code generated

1) a pojo java class that extends a base pojo java class. That way if I have custome attributes I can put those in the user overridable pojo java class.
...

How would I go about accomplishing things like this with hibernate tools?


I need something very similar. It's easy to adapt the template.
However you gonna run into trouble with the file-name: if you want a class generated "BasePerson" you can easily put "Base" in front of the class-name or something like that. but the file-name will still be Person.java!

To fix this I have following strategy:
- generating in two steps with two cfg.xml-files: Pojo and BasePojo
- in the cfg2jbasepojo.cfg.xml add property "createBasePojo"
- in PojoExporter.start() I have added code that listens this prop and modifies the classname from xxx to Basexxx

unfortunately this is not yet running, I'm about to find the reason.
perhaps somebody has some input on this solution.


Top
 Profile  
 
 Post subject: Re: How generate code similar to those Hibernate Synchronize
PostPosted: Fri Aug 26, 2005 4:58 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
axismundi wrote:
you gonna run into trouble with the file-name: if you want a class generated "BasePerson" you can easily put "Base" in front of the class-name or something like that. but the file-name will still be Person.java!


checkout the tools
extend org.hibernate.tool.hbm2x.POJOExporter
overide runVelocity
change the following line
File file = getFileForClassName(getOutputDirectory(), qualifiedDeclarationName, ".java");


Top
 Profile  
 
 Post subject: Re: How generate code similar to those Hibernate Synchronize
PostPosted: Fri Aug 26, 2005 9:21 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
er - to what factory would you pass your extended class? I don't like the idea to change al references to POJOExporter

actually I followed the plan that I described above and it appears to work.
It's really just patch, not smoothly integrating into the infrastructure, but it does the job.




dennisbyrne wrote:
axismundi wrote:
you gonna run into trouble with the file-name: if you want a class generated "BasePerson" you can easily put "Base" in front of the class-name or something like that. but the file-name will still be Person.java!


checkout the tools
extend org.hibernate.tool.hbm2x.POJOExporter
overide runVelocity
change the following line
File file = getFileForClassName(getOutputDirectory(), qualifiedDeclarationName, ".java");


Top
 Profile  
 
 Post subject: generate base classes for pojos
PostPosted: Sun Nov 26, 2006 1:04 am 
Newbie

Joined: Wed Jan 19, 2005 2:39 pm
Posts: 16
I'm still trying to figure out a good way to do this with hibernate tools 3.2b8. I'd like base pojo classes prefixed by 'Base' or somesuch. I've been using middlegen to do this, but assumed I could make the jump to hibernate tools now. Is it possible... Or is this scheme a bad idea for some reason?

I've toyed with altering the vm templates, but also ran into the file naming problem...

Any help? :)

Thanks much.
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 6:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the current way is to use <meta attribute="generated-class">BasePerson</meta> for those classes you really use the subclass overriding for.

Then the tooling will generate the base structure and you just need to do the extend for those you want to do it for.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 8:40 am 
Newbie

Joined: Wed Jan 19, 2005 2:39 pm
Posts: 16
I've been using the hibernate tools from within eclipse to generate the 'Domain Code' directly (without generating mapping files). Is there a way to effectively do what you're saying in this environment? Or, do I have to use to generate hbm.xml files and generate the pojos from there?

And, if I do have to generate the mapping files; it seems I could use a custom template to insert the generated-class tag... Hm.

Anyone working it this way?

Thanks for the response.
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 9:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
reveng.xml supports adding <meta> tags to table-filter and table which could be used for this. (not sure if it is just in svn or did make it to b8)

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