Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: re-create all tables based on hibernate.cfg.xml
PostPosted: Tue Nov 20, 2007 7:19 am 
Newbie

Joined: Tue Nov 20, 2007 5:56 am
Posts: 2
Hibernate version: 3

Dear all,

I'm a hibernate newbie. I need an ant target to dump any previous tables and recreate new ones, based on hibernate.cfg.xml . Suppose I added new entries in this file like
<mapping resource="com/companyMyOtherClass.hbm.xml"/>
, hence I'm expecting new table creations for my fresh database.

I've seen the usual
<property name="hbm2ddl.auto">create</property>
which does just this, but would it does it every time I restart my application. I only want to re-create my tables when I say I need to (calling my ant target).

Has anybody come across such a need, or knows a way to do it with
hbm2ddl tools ?

Cheers

Igor


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 8:28 am 
Newbie

Joined: Wed Jul 11, 2007 6:06 am
Posts: 15
Why not create to separate ant targets? One with
Code:
<property name="hbm2ddl.auto">create</property>

and one with
Code:
<property name="hbm2ddl.auto">update</property>
.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 12:01 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Refer to this URL to know how to use hibernate ANT tasks to create, validate and update your schema using hibernate mappings.

http://www.hibernate.org/hib_docs/refer ... guide-s1-5

Here is the build.xml with hibernate tasks. You can execute any of these tasks. But first time execute export, then validate and update.


<!-- exports all the hbm mappings to the specified database -->
<target name="schemaexport" depends="" description="Exports all hbm.xml files in {res.dir}/hbm">
<echo message="Run the schema export for all hbm.xml files in ${res.dir}/hbm"/>
<taskdef
name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.class.path"
>
</taskdef>
<schemaexport
properties="${res.dir}/hibernate.properties"
quiet="no"
text="no"
drop="no"
output="${gen.files.dir}/schema-export.sql">
<fileset dir="${gen.files.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>

<!-- exports all the hbm mappings to update the specified database -->
<target name="schemaupdate" depends="" description="Exports all hbm.xml files in {res.dir}/hbm">
<taskdef name="schemaupdate"
classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
classpathref="project.class.path"/>

<schemaupdate
properties="${res.dir}/hibernate.properties"
quiet="no"
text="no">
<fileset dir="${gen.files.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaupdate>
</target>
<target name="schemavalidate">
<taskdef name="schemavalidator"
classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask"
classpathref="project.class.path"/>

<schemavalidator
properties="${res.dir}/hibernate.properties">
<fileset dir="${gen.files.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</schemavalidator>
</target>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 5:56 am 
Newbie

Joined: Tue Nov 20, 2007 5:56 am
Posts: 2
Thanks sjhyam , it made the trick. As I wanted to drop and re-create the tables (I don't have many), here's what I used:

<!-- exports all the hbm mappings to the specified database -->
<target name="cleanDB" depends="" description="Clean up the database - needs startDB running in // ">
<taskdef
name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="classpath" />
<schemaexport
config="src/hibernate.cfg.xml"
output="${persistence.home}/schema-export-drop.sql"
quiet="yes" drop="yes" />
<schemaexport
config="src/hibernate.cfg.xml"
output="${persistence.home}/schema-export-create.sql"
quiet="yes" create="yes" />
</target>

<target name="startDB" description="Start the database used to store the user information in case of portal restart">
<!-- The name of the DB "my_db" is hard coded in
property "connection.url" of file src/hibernate.cfg.xml -->
<echo level="warning">Please check the file src/hibernate.cfg.xml fits your needs, specially connection.url</echo>
<java classname="org.hsqldb.Server" fork="true">
<classpath path="lib/hsqldb.jar" />
<arg value="-port"/>
<arg value="10000"/>
<arg value="-database.0"/>
<arg value="${persistence.home}/my_db"/>
<arg value="-dbname.0"/>
<arg value="my_db"/>
<arg value="-silent"/>
<arg value="false"/>
</java>
</target>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.