-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate Tools Reverse Engineering Usage
PostPosted: Thu Aug 31, 2006 5:02 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
Hi, all. I have been trying to dig through the documentation and these forums, and although they have been somewhat helpful, I have not been able to find my answers, so I will ask the questions straight out...

Hibernate Tools Version 3.2.0 beta7
MySQL Version 5.0.20

1) Can you point me somewhere how to create new exporters and use new templates? This is if I still need these after the questions below...

2) I finally got the hbm.xml files to pick up some "native" generators from reveng.xml (thanks to some posts on this forum). But I would like ALL tables to have "native", not "assigned". How do I do this?

3) I would like three tables (at this point) to have cascade="" attributes on some fields. I can add it manually to the hbm.xml files, but every time we regenerate for database changes it gets overwritten. Can I do this somehow using reveng.xml?

4) The current generated POJOs are inflexible (if I regenerate it will overwrite changes). I would like to generate classes that extend from the generated pojos that can be updated and changed and not be overwritten (a la Hibernate Synchronizer). It looks like I will need some new templates and maybe a new Exporter. How do I set these up? Or has someone done this?

5) I would like to auto-generate some Spring-based DAOs, with a beans.xml file in the directory to hook them into spring. Again, how would I set up the templates/exporter? Or has someone done this?

6) Lastly, we are hoping to be able to publish database changes up the build chain by using the hbm.xml files as a guide. Can this be done cleanly?

reveng.xml:
Code:
<hibernate-reverse-engineering>
<table name="bill">
   <primary-key>
      <generator class="native" />
      <key-column name="bill_id"></key-column>
   </primary-key>
</table>
<table name="bill_item">
   <primary-key>
      <generator class="native" />
      <key-column name="bill_item_id"></key-column>
   </primary-key>
</table>
<table name="bill_line_item">
   <primary-key>
      <generator class="native" />
      <key-column name="bill_line_item_id"></key-column>
   </primary-key>
</table>
</hibernate-reverse-engineering>



hibernate.cfg.xml:
Code:
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.default_schema">Dbname</property>
        <property name="hibernate.default_catalog">Dbname</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.connection.url">jdbc:mysql://db.server.net:3306/Dbname</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="com/blah/model/gen/Bill.hbm.xml" />
        <mapping resource="com/blah/model/gen/BillItem.hbm.xml" />
        <mapping resource="com/blah/model/gen/BillLineItem.hbm.xml" />
    </session-factory>
</hibernate-configuration>



Thank you all for your help.
Arnon[/code]


Top
 Profile  
 
 Post subject: Re: Hibernate Tools Reverse Engineering Usage
PostPosted: Fri Sep 01, 2006 3:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
arnon wrote:
1) Can you point me somewhere how to create new exporters and use new templates? This is if I still need these after the questions below...


docs and the sourcecode for the existing exporters/templates.

Contribution for more docs/tutorials on this i very welcome ;)

Quote:
2) I finally got the hbm.xml files to pick up some "native" generators from reveng.xml (thanks to some posts on this forum). But I would like ALL tables to have "native", not "assigned". How do I do this?


public class MyRevEngStrategy extends DefaultReverseEngineeringStrategy {

getTableIdentifierStrategyName(TableIdentifer t) {
return "native";
}
}

Quote:
3) I would like three tables (at this point) to have cascade="" attributes on some fields. I can add it manually to the hbm.xml files, but every time we regenerate for database changes it gets overwritten. Can I do this somehow using reveng.xml?


no, not at this time. Might be added...

Quote:
4) The current generated POJOs are inflexible (if I regenerate it will overwrite changes). I would like to generate classes that extend from the generated pojos that can be updated and changed and not be overwritten (a la Hibernate Synchronizer). It looks like I will need some new templates and maybe a new Exporter. How do I set these up? Or has someone done this?


i plan on implementing this soon.

can be done today by adding <meta attribute="generated-class">PersonBase</meta> to the hbm.xml.

This is just not supported for reveng.xml yet.

Quote:
5) I would like to auto-generate some Spring-based DAOs, with a beans.xml file in the directory to hook them into spring. Again, how would I set up the templates/exporter? Or has someone done this?


example ( assuming you write the spring specific beans.xml template )

<hbmtemplate template="springcfgxml.ftl" templatepath="pathtoyourtemplate" filepattern="beans.xml"/>

<hbmdao/>

Quote:
6) Lastly, we are hoping to be able to publish database changes up the build chain by using the hbm.xml files as a guide. Can this be done cleanly?


Define cleanly ?

<hbm2ddl update="true" output="updatesql.txt"/> ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Hibernate Tools Reverse Engineering Usage
PostPosted: Fri Sep 01, 2006 12:55 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
max wrote:
arnon wrote:
1) Can you point me somewhere how to create new exporters and use new templates? This is if I still need these after the questions below...


docs and the sourcecode for the existing exporters/templates.
Contribution for more docs/tutorials on this i very welcome ;)


I would love to help..... but I don't understand it well enough. Hence the asking of so many questions after throwing my hands up in defeat... We'll see if I can when I'm through all this.

Quote:
Quote:
2) I finally got the hbm.xml files to pick up some "native" generators from reveng.xml (thanks to some posts on this forum). But I would like ALL tables to have "native", not "assigned". How do I do this?


public class MyRevEngStrategy extends DefaultReverseEngineeringStrategy {

getTableIdentifierStrategyName(TableIdentifer t) {
return "native";
}
}


Awesome! So simple :D

Quote:
Quote:
3) I would like three tables (at this point) to have cascade="" attributes on some fields. I can add it manually to the hbm.xml files, but every time we regenerate for database changes it gets overwritten. Can I do this somehow using reveng.xml?


no, not at this time. Might be added...


Since I will now be extending ReverseEngineeringStrategy, is there a method that I can override where I can do some matching and insert the cascades there? For those three HBM files only?


Quote:
Quote:
4) The current generated POJOs are inflexible (if I regenerate it will overwrite changes). I would like to generate classes that extend from the generated pojos that can be updated and changed and not be overwritten (a la Hibernate Synchronizer). It looks like I will need some new templates and maybe a new Exporter. How do I set these up? Or has someone done this?


i plan on implementing this soon.

can be done today by adding <meta attribute="generated-class">PersonBase</meta> to the hbm.xml.

This is just not supported for reveng.xml yet.


So if I add this to the HBM template for all HBMs, then both the base class and the extended class will be generated automatically? Or will it only generate the base class, and I still need to generate the extended classes?

If I still need to generate the extended classes, how do I set it up so that it doesn't generate those classes if they exist? Is that ReverseEngineeringStrategy again? If so, can you guide me to the areas in the code to make those changes?


Quote:
Quote:
5) I would like to auto-generate some Spring-based DAOs, with a beans.xml file in the directory to hook them into spring. Again, how would I set up the templates/exporter? Or has someone done this?


example ( assuming you write the spring specific beans.xml template )

<hbmtemplate template="springcfgxml.ftl" templatepath="pathtoyourtemplate" filepattern="beans.xml"/>

<hbmdao/>


Great. I think I can do that. A few questions, though.
a) I'm still trying to figure out what is available in the context. I see that allEntity-list.ftl has a classList that it is running off of. Should I use that same list for springcfgxml.ftl?
b) In allEntity-list.ftl, what type of object is "class" and where is the API (so that I can get other values from it)?
c) What variable can I get the package these were generated in from?
d) Any chance I can run this from the UI? Everything else is running from there, and it would be a shame to go to Ant for this one thing.

Quote:
Quote:
6) Lastly, we are hoping to be able to publish database changes up the build chain by using the hbm.xml files as a guide. Can this be done cleanly?


Define cleanly ?

<hbm2ddl update="true" output="updatesql.txt"/> ?


OK, I'll read up on those options and see how they work. By cleanly, I mean that everything is intact (tables, PKs, FKs, indices) in the new database. I wouldn't expect triggers, stored procs, and the like to move through this (unless you can tell me a way). It will save alot on manual creation and alter scripts for publishes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 8:50 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
Hi, Max.

I have made progress today, thanks to your help. The questions from earlier today still stand, but I have a couple of additional problems.

7) By changing the templates, I was able to put the <meta attribute="generated-class">PersonBase</meta> in the hbm.xml files. But that didn't result in changing the class names. What did I do wrong? Do I have to do a second run using <configuration> rather than <jdbcconfiguration> to get the classes out properly? Would it be easier to change the class names using <hbmtemplate>?

Here is one of the generated hbm.xml files. So Item.hbm.xml results in the Item class being generated, not the ItemBase class.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 1, 2006 1:26:34 PM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping>
    <meta attribute="generated-class">com.inkubator.model.gen.ItemBase</meta>
    <class name="com.inkubator.model.gen.Item" table="item">
        <comment></comment>
        <id name="itemId" type="java.lang.Integer">
            <meta attribute="use-in-tostring">true</meta>
            <meta attribute="use-in-equals">true</meta>
            <column name="item_id" />
            <generator class="native" />
        </id>
        <property name="itemUserSku" type="string">
            <column name="item_user_sku" length="16" not-null="true">
                <comment></comment>
            </column>
        </property>
        <!-- more properties -->
    </class>
</hibernate-mapping>



8) The validation on my build.xml file is refusing to allow anything but destdir, templatepath, and templateprefix attributes on any of the exporters. I've got the latest hibernate-tools.jar on the defined classpath. What am I doing wrong?

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="../../../../../" default="reveng.jdbc" name="Reverse Engineer">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.5"/>
    <property name="source" value="1.5"/>

   <property name="src.dir" location="src"/>
   <property name="doc.dir" location="doc"/>
   <property name="webinf.dir" location="WebContent/WEB-INF"/>

   <property name="app.dir" location="${src.dir}/com/blah"/>
   <property name="dao.dir" location="${app.dir}/dao"/>
   <property name="model.dir" location="${app.dir}/model"/>
   <property name="template.dir" location="${app.dir}/util/reveng"/>

   <path id="library.classpath">
        <pathelement location="build/classes"/>
       <fileset dir="${webinf.dir}/lib">
          <include name="*.jar"/>
      </fileset>
    </path>

   <!-- =========================================== -->
   <!-- Reverse Engineer from database             -->
   <!-- =========================================== -->
   <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
      classpathref="library.classpath"/>
   <target name="reveng.jdbc" description="Reverse Engineer from database">
      <hibernatetool destdir="${model.dir}" templatepath="${template.dir}">
         <jdbcconfiguration configurationfile="${src.dir}/hibernate.cfg.xml"
            reversestrategy="com. blah.util.reveng.MyReverseEngineeringStrategy"
            packagename="com.inkubator.model.gen" />

         <!-- Output the hbm.xml files -->
         <hbm2hbmxml destdir="${model.dir}/hbm" templateprefix="/hbm" />

         <!-- Output the schema (for use in publishes) -->
         <hbm2ddl destdir="${schema.dir}" />

         <!-- Output the Spring based DAOs -->
         <!-- TODO Switch with hbmtemplate for to switch to *DAO suffix -->
         <hbm2dao destdir="${dao.dir}" templateprefix="/dao" />

         <!-- TODO Output the Spring beans.xml for the DAOs -->

         <!-- Output the generated model POJOs -->
         <!-- TODO Switch with hbmtemplate for to switch to *Base suffix -->
         <hbm2java destdir="${model.dir}/gen" templateprefix="/pojo"/>
      </hibernatetool>
      <!-- Edit the HBM files for cascade=""? -->
      <!-- Use replace? -->
   </target>
   
   <!-- Creates the extended model (this should only be done once or else it will overwrite changes)  -->
   <target name="extended.reveng.jdbc" depends="reveng.jdbc" description="Reverse Engineer from database">
      <hibernatetool destdir="${model.dir}" templatepath="${template.dir}">
         <configuration configurationfile="${src.dir}/hibernate.cfg.xml"
            reversestrategy="com. blah.util.reveng.MyReverseEngineeringStrategy"/>

         <!-- Output the extended model POJOs -->
      </hibernatetool>
   </target>
</project>


Thanks alot,
Arnon


Top
 Profile  
 
 Post subject: Re: Hibernate Tools Reverse Engineering Usage
PostPosted: Sat Sep 02, 2006 4:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Quote:
3) I would like three tables (at this point) to have cascade="" attributes on some fields. I can add it manually to the hbm.xml files, but every time we regenerate for database changes it gets overwritten. Can I do this somehow using reveng.xml?


no, not at this time. Might be added...
[/quote]

Since I will now be extending ReverseEngineeringStrategy, is there a method that I can override where I can do some matching and insert the cascades there? For those three HBM files only?
[/quote]

no, not at this time. Remember that reveng.xml is nothing else than a simplified "text" version of what can be done via RevEngStrategy..so to get it in reveng.xml the RevEngStrategy needs to get a method for it first.

Quote:
4) The current generated POJOs are inflexible (if I regenerate it will overwrite changes). I would like to generate classes that extend from the generated pojos that can be updated and changed and not be overwritten (a la Hibernate Synchronizer). It looks like I will need some new templates and maybe a new Exporter. How do I set these up? Or has someone done this?


i plan on implementing this soon.

can be done today by adding <meta attribute="generated-class">PersonBase</meta> to the hbm.xml.

This is just not supported for reveng.xml yet.
[/quote]

So if I add this to the HBM template for all HBMs, then both the base class and the extended class will be generated automatically? Or will it only generate the base class, and I still need to generate the extended classes?
[/quote]

it only generates the baseclass as the moment.

Quote:
If I still need to generate the extended classes, how do I set it up so that it doesn't generate those classes if they exist? Is that ReverseEngineeringStrategy again? If so, can you guide me to the areas in the code to make those changes?


there is no overwrite detection build in. only way at the moment is to generate to a seperate dir and copy-no-overwrite with e.g ant.

Quote:
5) I would like to auto-generate some Spring-based DAOs, with a beans.xml file in the directory to hook them into spring. Again, how would I set up the templates/exporter? Or has someone done this?


example ( assuming you write the spring specific beans.xml template )

<hbmtemplate template="springcfgxml.ftl" templatepath="pathtoyourtemplate" filepattern="beans.xml"/>

<hbmdao/>
[/quote]

Quote:
Great. I think I can do that. A few questions, though.
a) I'm still trying to figure out what is available in the context. I see that allEntity-list.ftl has a classList that it is running off of. Should I use that same list for springcfgxml.ftl?


allentity-list.ftl is something that the DocExporter uses. Check there to see what he puts in.

but generally for a "configuration wide" file you just use $cfg to get access to what you want. e.g. $cfg.getClassMappings()

Quote:
b) In allEntity-list.ftl, what type of object is "class" and where is the API (so that I can get other values from it)?


you mean $clazz ? it is a PersistentClass.

Quote:
c) What variable can I get the package these were generated in from?


i don't understand. the package name is part of the fully qualified classname.

Quote:
d) Any chance I can run this from the UI? Everything else is running from there, and it would be a shame to go to Ant for this one thing.


the ui doesn't support arbitrary custom exporters but it does support overriding existing templates.

we will eventually get "arbitrary custom exporters" in the ui.
Quote:
6) Lastly, we are hoping to be able to publish database changes up the build chain by using the hbm.xml files as a guide. Can this be done cleanly?


Define cleanly ?

<hbm2ddl update="true" output="updatesql.txt"/> ?


OK, I'll read up on those options and see how they work. By cleanly, I mean that everything is intact (tables, PKs, FKs, indices) in the new database. I wouldn't expect triggers, stored procs, and the like to move through this (unless you can tell me a way). It will save alot on manual creation and alter scripts for publishes.[/quote]

we don't have a perfect db schema delta tool.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 4:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
arnon wrote:
Hi, Max.
7) By changing the templates, I was able to put the <meta attribute="generated-class">PersonBase</meta> in the hbm.xml files. But that didn't result in changing the class names. What did I do wrong? Do I have to do a second run using <configuration> rather than <jdbcconfiguration> to get the classes out properly? Would it be easier to change the class names using <hbmtemplate>?


yes, you need to generate from the actual generated templates and thus need a <configuraiton> in additionto the <jdbcconfiguration>

Quote:
8) The validation on my build.xml file is refusing to allow anything but destdir, templatepath, and templateprefix attributes on any of the exporters. I've got the latest hibernate-tools.jar on the defined classpath. What am I doing wrong?


what additional attributes are you looking for ?

btw. templateprefix is ignored in svn and will eventually be removed completely.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 4:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
arnon wrote:
Hi, Max.
7) By changing the templates, I was able to put the <meta attribute="generated-class">PersonBase</meta> in the hbm.xml files. But that didn't result in changing the class names. What did I do wrong? Do I have to do a second run using <configuration> rather than <jdbcconfiguration> to get the classes out properly? Would it be easier to change the class names using <hbmtemplate>?


yes, you need to generate from the actual generated templates and thus need a <configuraiton> in additionto the <jdbcconfiguration>

Quote:
8) The validation on my build.xml file is refusing to allow anything but destdir, templatepath, and templateprefix attributes on any of the exporters. I've got the latest hibernate-tools.jar on the defined classpath. What am I doing wrong?


what additional attributes are you looking for ?

btw. templateprefix is ignored in svn and will eventually be removed completely.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 12:59 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
max wrote:
arnon wrote:
8) The validation on my build.xml file is refusing to allow anything but destdir, templatepath, and templateprefix attributes on any of the exporters. I've got the latest hibernate-tools.jar on the defined classpath. What am I doing wrong?


what additional attributes are you looking for ?

btw. templateprefix is ignored in svn and will eventually be removed completely.


I'm looking for all the attributes written in the documentation. For example, <hbmtemplate filepattern=""> can be used according to the documentation, but Eclipse 3.2 is complaining about it. It's not available. Only the three attributes shown above. Is destdir even a valid attribute on hbmtemplate?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 1:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eclipse 3.2 is complaining ? what does that mean ?

does it fail when you run ant ?

what are you running ? (just show the hibernatetool part not the full story)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 03, 2006 1:46 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
Hi, max. Forget about the eclipse problem. The main thing was that when I put in the tag names and typed ctrl-space it wouldn't come up with those attribute names, but when I put them in manually it worked.

I have made alot of progress with your help. Thank you. I am hitting some interesting behavior that I was hoping you could help me with. If I run the Ant build with no hbm.xml files, I get the error below. The the hbm.xml files ARE there. If I run it a second time (leaving the files generated the first time), it works fine. So it's not picking up the hbm.xml files for use immediately after they are created.

Note that ${src.dir}="src/", so is it not picking that up? Oh, just noticed another thing, #2 says "available via the classpath". It's available in the source path, not the classpath yet. Is that the problem?

Thanks,
Arnon

Error:
Code:
[hibernatetool] org.hibernate.MappingNotFoundException: resource: com/inkubator/model/gen/TaxRate.hbm.xml not found
[hibernatetool] A resource located at com/inkubator/model/gen/TaxRate.hbm.xml was not found.
[hibernatetool] Check the following:
[hibernatetool]
[hibernatetool] 1) Is the spelling/casing correct ?
[hibernatetool] 2)   Is com/inkubator/model/gen/TaxRate.hbm.xml available via the classpath ?
[hibernatetool] 3) Does it actually exist ?



build.xml
Code:
<target name="reveng.jdbc" description="Reverse Engineer from database">
      <!-- Output the hbm.xml and cfg.xml files -->
      <hibernatetool destdir="${src.dir}" templatepath="${template.dir}">
         <jdbcconfiguration configurationfile="${src.dir}/hibernate.cfg.xml"
            reversestrategy="com.inkubator.util.reveng.MyReverseEngineeringStrategy"
            packagename="com.inkubator.model.gen" />
         <hbm2hbmxml />
         <hbm2cfgxml />
      </hibernatetool>

      <!-- Edit the HBM files for cascade=""? -->

      <!-- Output the POJOs separately because they run off the HBMs -->
      <hibernatetool destdir="${src.dir}" templatepath="${template.dir}">
         <configuration configurationfile="${src.dir}/hibernate.cfg.xml"/>

         <!-- Output the generated model POJOs -->
         <hbm2java>
            <property key="jdk5" value="true"/>
         </hbm2java>

         <!-- Output the extended model POJOs -->
         <hbmtemplate template="extendedModel.ftl"
            filepattern="{package-name}/../ext/{class-name}.java" />

         <!-- Output the Spring based DAOs and beans.xml-->
         <hbmtemplate template="daohome.ftl"
            filepattern="{package-name}/../dao/{class-name}DAO.java" />
         <hbmtemplate template="springbeans.ftl"
            filepattern="{package-name}/../dao/beans.xml" />

         <!-- Output the documentation -->
         <hbm2doc destdir="${doc.dir}" />

         <!-- Output the schema (for use in publishes) -->
         <hbm2ddl destdir="${model.dir}" export="false" outputfilename="sql.ddl" />
      </hibernatetool>

      <!-- Move the extended model POJOs to model.dir without overwriting -->
   </target>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 03, 2006 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
arnon wrote:
Hi, max. Forget about the eclipse problem. The main thing was that when I put in the tag names and typed ctrl-space it wouldn't come up with those attribute names, but when I put them in manually it worked.


well, eclipse only supports the ant tasks it knows about - not every task in the world ;)

Quote:
Note that ${src.dir}="src/", so is it not picking that up? Oh, just noticed another thing, #2 says "available via the classpath". It's available in the source path, not the classpath yet. Is that the problem?


Yes, it needs to be via the classpath.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 03, 2006 7:02 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
max wrote:
well, eclipse only supports the ant tasks it knows about - not every task in the world ;)


I thought it might somehow read the XML schema from the embeded tasks. Don't know Ant that well. Oh well.

Nearly complete with everything. I even got the cascade attributes in the files by using some Ant file editing. Still working out some last issues, specifically casting issues when using the extended model and retrieving an object that is in the generated model.

When I am done with it all, I'll put up my results (I hate when people don't put the actual solutions on the forums) and I can give you any files you think might be helpful.

Thanks,
Arnon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 12:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
cool.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: THE SOLUTION - Spring DAOs and POJOs from Hibernate Tools
PostPosted: Sun Jun 10, 2007 11:11 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
Hi, all.

Well, it's been almost 9 months and my time has been swamped. I have finally guilted myself back into posting this solution on the site. Sorry that it has taken so long. But the nice part is that I just fixed a couple of bugs on this earlier today.

I am going to post the following files in the following few replies:

Code:
com.company.util.reveng.build.xml
    - my ant build file
com.company.util.reveng.modelBase.ftl
    - generates the base classes (overwritten every reveng)
com.company.util.reveng.modelExtended.ftl
    - generates classes that are not overwritten that extend the base classes
    - this allows addition and overriding of methods
com.company.util.reveng.springbeans.ftl
    - generates the Spring Beans file which lists all the DAOs
com.company.util.reveng.springdao.ftl
    - generates the Spring/Hibernate DAOs

These generate a code structure of

Code:
com.company.model
    - houses extended classes (not overwritten)
com.company.model.gen
    - houses base classes (overwritten every reveng)
com.company.model.dao
    - houses DAOs and beans.xml file
com.company.model.hbm
    - houses hbm files
hibernate.cfg.xml
    - this ends up at the base of your src/class tree


CONFIGURING THE WEBAPP TO USE THE GENERATED FILES

These assume that you have set up your web.xml to use multiple *beans.xml files, as follows:

Code:
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/**/*beans.xml</param-value>
   </context-param>
   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

I also use OpenSessionInViewFilter so just in case it has an effect on the generated code this should be in web.xml:

Code:
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Secondly, you need to make sure that your HibernateInterceptor and TransactionManager are set up appropriately. Here I have included only the necessary parts of com.company.beans.xml:

Code:
   <!-- AOP Setup -->
   <bean name="aapc" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
      <property name="usePrefix" value="true"/>
       <property name="proxyTargetClass" value="true"/>
   </bean>
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
       <property name="order" value="10"/>
       <property name="beanNames" value="*Manager,*Service"/>
       <property name="proxyTargetClass" value="true"/>
      <property name="interceptorNames">
            <list><idref bean="hibernateInterceptor"/></list>
       </property>
   </bean>

   <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

Thirdly, you need to make sure that you set up your data source and session factory. Here I have included only the necessary parts of com.company.data-beans.xml:

Code:
   <!-- Hibernate beans -->
   <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="${db.driver}"/>
      <property name="url" value="jdbc:${db.url}"/>
      <property name="username" value="${db.username}"/>
      <property name="password" value="${db.password}"/>
   </bean>
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="myDataSource"/>
      <property name="mappingDirectoryLocations">
      <list>
      <value>classpath:com/company/model/hbm</value>
      <value>classpath:com/company/store</value>
      </list>
      </property>
      <property name="hibernateProperties">
         <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
      </property>
   </bean>
   <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
         <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

Note that hibernateInterceptor and transactionManager are actually defined in data-beans.xml. That was because I split out all my beans files into different sections and, if I had put them into the first file, it would have had a circular bean definition causing an error on startup. This can also be done by combining beans.xml and data-beans.xml into one file.

Also note that I use MySQLDialect. This must be changed to the appropriate database, and the data source values must be input appropriately.

That's it for setting up your webapp, now you just need to run build.xml in the next message as an Ant build, and it will place all your files in the right directories to be used by this setup.


Top
 Profile  
 
 Post subject: The build.xml
PostPosted: Sun Jun 10, 2007 11:14 pm 
Newbie

Joined: Thu Aug 31, 2006 3:22 pm
Posts: 14
Location: Oregon
com.company.util.reveng.build.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="../../../../../" default="reveng.jdbc" name="Reverse Engineer">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.5"/>
    <property name="source" value="1.5"/>

   <property name="src.dir" location="src"/>
   <property name="doc.dir" location="doc/hibernate"/>
   <property name="webinf.dir" location="WebContent/WEB-INF"/>

   <property name="app.dir" location="${src.dir}/com/company"/>
   <property name="template.dir" location="${app.dir}/util/reveng"/>
   <property name="model.dir" location="${app.dir}/model"/>
   <property name="dao.dir" location="${model.dir}/dao"/>

   <path id="library.classpath">
        <pathelement location="${src.dir}"/>
        <pathelement location="build/classes"/>
       <fileset dir="${webinf.dir}/lib">
          <include name="*.jar"/>
      </fileset>
    </path>

   <!-- =========================================== -->
   <!-- Reverse Engineer from database             -->
   <!-- =========================================== -->
   <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
      classpathref="library.classpath"/>
   <target name="reveng.jdbc" description="Reverse Engineer from database">
      
      <!-- Output the hbm.xml files -->
      <hibernatetool destdir="${src.dir}" templatepath="${template.dir}">
         <jdbcconfiguration configurationfile="${src.dir}/hibernate.cfg.xml"
            reversestrategy="com.company.util.reveng.MyReverseEngineeringStrategy"
            packagename="com.company.model" />
         <hbm2hbmxml/>
         <hbm2cfgxml/>
      </hibernatetool>

      <!-- Move the hbm.xml files to their own directory and change cfg.xml to adjust -->
      <delete includeemptydirs="true">
         <fileset dir="${model.dir}/hbm" includes="**/*.*"/>
      </delete>
      <move todir="${model.dir}/hbm" overwrite="false">
         <fileset dir="${model.dir}">
           <include name="*.hbm.xml"/>
         </fileset>
      </move>
      <replaceregexp byline="true"
         match="model/" replace="model/hbm/"
         file="${src.dir}/hibernate.cfg.xml" />

      <!-- Edit the HBM files for cascade attributes -->
      <replaceregexp byline="true"
         match="(set name=.billItems. inverse=.true.)"
         replace="\1 cascade=&quot;all&quot;">
         <fileset dir="${model.dir}/hbm">
              <include name="Bill.hbm.xml"/>
            <include name="BillLineItem.hbm.xml"/>
         </fileset>
      </replaceregexp>
      <replaceregexp byline="true"
         match="(many-to-one .* fetch=.select.)"
         replace="\1 cascade=&quot;save-update,persist&quot;">
         <fileset dir="${model.dir}/hbm">
            <include name="BillItem.hbm.xml"/>
         </fileset>
      </replaceregexp>
      

      <!-- Output the POJOs separately because they run off the HBMs -->
      <delete includeemptydirs="true">
         <fileset dir="${model.dir}/gen" includes="**/*.*"/>
      </delete>
      <delete includeemptydirs="true">
         <fileset dir="${dao.dir}" includes="**/*.*"/>
      </delete>
      <delete includeemptydirs="true">
         <fileset dir="${doc.dir}" includes="**/*.*"/>
      </delete>
      <hibernatetool destdir="${src.dir}" templatepath="${template.dir}">
         <configuration configurationfile="${src.dir}/hibernate.cfg.xml">
               <fileset dir="${app.dir}">
                   <include name="**/*.hbm.xml"/>
                  <exclude name="model/hbm/*.hbm.xml"/>
               </fileset>
         </configuration>

         <!-- Output the generated model POJOs -->
         <hbmtemplate template="modelBase.ftl"
            filepattern="{package-name}/gen/{class-name}Base.java">
            <property key="jdk5" value="true" />
            <property key="ejb3" value="false" />
         </hbmtemplate>

         <!-- Output the extended model POJOs -->
         <hbmtemplate template="modelExtended.ftl"
            filepattern="{package-name}/ext/{class-name}.java">
            <property key="jdk5" value="true" />
            <property key="ejb3" value="false" />
         </hbmtemplate>
         
         <!-- Output the Spring based DAOs and beans.xml-->
         <hbmtemplate template="springdao.ftl"
            filepattern="{package-name}/dao/{class-name}DAO.java" />
         <hbmtemplate template="springbeans.ftl"
            destdir="${dao.dir}" filepattern="beans.xml" />

         <!-- Output the documentation -->
         <hbm2doc destdir="${doc.dir}" />
      </hibernatetool>

      <!-- Move the extended model POJOs to model.dir if they don't exist -->
      <touch>   <!-- Updates the modified date so that move doesn't overwrite existing files -->
         <fileset dir="${model.dir}"/>
      </touch>
      <move todir="${model.dir}" overwrite="false">
         <fileset dir="${model.dir}/ext"/>
      </move>
      <delete dir="${model.dir}/ext" />
   </target>
   
   <target name="generate.db.fresh" description="Use the HBMs to generate a fresh database schema">
      <hibernatetool destdir="${model.dir}" templatepath="${template.dir}">
         <configuration configurationfile="${src.dir}/hibernate.cfg.xml" />
         <hbm2ddl export="true" drop="true" />
      </hibernatetool>
   </target>
   
   <target name="generate.db.delta" description="Use the HBMs to update a database schema">
      <hibernatetool destdir="${model.dir}" templatepath="${template.dir}">
         <configuration configurationfile="${src.dir}/hibernate.cfg.xml" />
         <hbm2ddl export="true" update="true" />
      </hibernatetool>
   </target>
</project>


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