-->
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.  [ 3 posts ] 
Author Message
 Post subject: Spring DAO generation with Hibernate Tools / JBossIDE
PostPosted: Wed Feb 07, 2007 11:39 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Hi All!

first of all i want to say that i searched this forum and googled around - but i found nothing really usefull.

I need to generate (reverse engineer the database) SpringDAOs along with my PoJo's. I know i can create a new ftl template - but i'm sure many have done this allready. is someone willing to share these templates?

the myeclipseide has this feature and there is a vm template.

thanx for your help!
treb


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 2:01 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Just as an example of generating a dao-specific applicationContext file:
Code:
<#--
   SPRING 2.0 CONFIGURATION FILE TEMPLATE FOR HIBERNATE TOOLS
   
   CREATED: 2006-08-14
   PURPOSE: To allow Hibernate Tools to generate a Spring Framework configuration file
   RESTRICTIONS:
      Spring 2.x declarative transaction style only
   ASSUMPTIONS:
      Mapping files (*.hbm.xml) files are in the same package as the POJO's
   
   PARAMETERS (name, required, default, valid values):
      default_autowire, required: no, default: none, valid: autodetect, byName, byType, constructor, no
         - sets the default-autowire attribute on the beans tag if a valid value
      default_lazy_init, required: no, default: none, valid: true, false
         - sets the default-lazy-init attribute on the beans tag if true
      default_merge, required: no, default: none, valid: true, false
         - sets the default-merge attribute on the beans tag if true
      default_init_method, required: no, default: none, valid: any string
         - sets the default-init-method attribute on the beans tag if specified
      default_destroy_method, required: no, default: none, valid: any string
         - sets the default-destroy-method attribute on the beans tag if specified
      sessionfactory_id, required: no, default: "sessionFactory", valid: any string
         - sets the Hibernate session factory bean identifier
      dao_beans, required: no, default: false, valid: true, false
         - if true, will generate bean definitions for DAO classes
      dao_id_pattern, required: no, default: empty string, valid: any string
         - the bean identifier for each bean, can use {class-name} and {class-name-lowercamel}, each
            of which will be replaced by the POJO class name (in lower camel case for the latter).
            An empty string will result in no identifier being configured (bean can be referenced by
            by FQDN)
      dao_class_pattern, required: yes, if dao_beans is true, default: nonsensical string, valid: any string
         - a pattern for the java class corresponding to the dao bean implementation, can use {class-name}
            and {class-name-lowercamel) as above
      additional_bean_data, required: no, default: none, valid: any string
         - any additional bean data to be added at the end of the file (note: must be escaped
            in the ant build file)
-->
<?xml version="1.0" encoding="utf-8"?>
<#-- ************************************************************** -->
<#-- ************** ROOT LEVEL CONFIGURATION ********************** -->
<#-- ************************************************************** -->
<beans    xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/aop/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
<#-- autowire the sessionFactory into the DAO beans -->
      default-autowire="byName"
<#-- Conditionally add default values to the beans node -->
<#if default_lazy_init?? && default_lazy_init?is_boolean>
      default-lazy-init="${default_lazy_init?string}"
</#if>
<#if default_merge?? && default_merge?is_boolean>
      default-merge="${default_merge?string}"
</#if>
<#if default_init_method??>
      default-init-method="${default_init_method}"
</#if>
<#if default_destroy_method??>
      default-destroy-method="${default_destroy_method}"
</#if>
      >

<#-- ************************************************************** -->
<#-- ********************** DAO BEANS ***************************** -->
<#-- ************************************************************** -->
<#assign sessionfactory_id = sessionfactory_id!"sessionFactory">
<#assign dao_beans = dao_beans!false>
<#if dao_beans>
   <#assign dao_id_pattern = dao_id_pattern!"">
   <#assign dao_class_pattern = dao_class_pattern!"org.replaceme.with.a.real.class.pattern.{class-name-lowercamel}Dao">
<!-- DAO Beans -->
<#foreach entity in c2j.getPOJOIterator(cfg.getClassMappings())>
   <#if dao_id_pattern == "">
      <#assign id_phrase = "">
   <#else>
      <#assign id_name = dao_id_pattern?replace("{class-name}", entity.getShortName())?replace("{class-name-lowercamel}", entity.getShortName()?uncap_first)>
      <#assign id_phrase = "id=\"${id_name}\" ">
   </#if>
   <bean ${id_phrase}class="org.package.test.framework.dao.hibernate.BaseHibernateDao" >
        <property name="persistentClass" value="${entity.qualifiedDeclarationName}"/>
   </bean>
</#foreach>
</#if>

<#-- ************************************************************** -->
<#-- ******************* ADDITIONAL BEANS ************************* -->
<#-- ************************************************************** -->
<#if additional_bean_data??>
<!-- Additional Beans -->
   ${additional_bean_data}
</#if>

</beans>


It generates this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/aop/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byName" default-lazy-init="false" default-merge="false">
<!-- DAO Beans -->
    <bean id="referralSourceDao" class="org.package.test.framework.dao.hibernate.BaseHibernateDao">
        <property name="persistentClass" value="org.package.test.framework.model.ReferralSource" />
    </bean>
    <bean id="participantRelationshipDao" class="org.package.test.framework.dao.hibernate.BaseHibernateDao">
        <property name="persistentClass" value="org.package.test.framework.model.ParticipantRelationship" />
    </bean>
...326 additional beans defined, I have 328 model classes in this project...
</beans>


Note that the generation of this Spring configuration file is highly specific to the application's architecture. I use a BaseHibernateDao which encompasses all basic DAO functionality given a specific model class, which is setter-injected by the above file. The Hibernate SessionFactory is injected byName from another Spring configuration file (applicationContext-hibernate.xml).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 06, 2007 8:49 am 
Beginner
Beginner

Joined: Tue Jan 30, 2007 1:35 pm
Posts: 27
Hi All,

I'm new in Hibernate reverse enginering. Now I'm doing the reverse enginering on Dao.

May I know, do I need to write a new ant task and exporter class in this case if I follow the example ?? Thanks for ur help.


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