Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

GeneratingClasses and mappingFiles from database schema/ sql with ant/maven/hibe

hi,
I have an application using the the following ant script (it creates mapping files - hbms and sql schema for my database, basing on xdoclet for hibernate), (application also use spring beans in configuration file):

<?xml version="1.0" encoding="UTF-8"?>
<project name="demo" default="generate">

<property file="build.properties" />

<target name="init">

<echo message="Clening..."/>
<delete dir="target" failonerror="false"/>

<echo message="Creating folders..."/>
<mkdir dir="target"/>
<mkdir dir="target/classes"/>
<mkdir dir="target/jar"/>

<echo message="Defining xdoclet2 ant task..."/>
<path id="xdoclet2.task.classpath">
<fileset dir="lib/xdoclet">
<include name="*.jar"/>
</fileset>
</path>

<path id="project.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>

<taskdef
name="xdoclet2"
classname="org.xdoclet.ant.XDocletTask"
classpathref="xdoclet2.task.classpath"
/>
</target>
<target name="generate" depends="init" description="Cleans and builds everything.">
<echo message="Generating hbms..."/>
<xdoclet2>
<fileset dir="source/src">
<include name="**/*Model.java"/>
</fileset>

<component
classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
destdir="target/hbm"
version="2.0"
/>
</xdoclet2>

<echo message="Compiling..."/>
<javac classpathref="project.classpath" destdir="target/classes"
srcdir="source/src">
</javac>

<echo message="Creating jar..."/>
<jar destfile="target/jar/demo.jar">
<fileset dir="target/classes"/>
<fileset dir="resources"/>
<fileset dir="target/hbm"/>
</jar>

<path id="hibernate.task.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<fileset dir="target/jar">
<include name="*.jar"/>
</fileset>
</path>

<echo message="Creating database schema..."/>
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="hibernate.task.classpath"/>

<schemaexport
properties="resources/hibernate.properties"
quiet="no"
text="no"
drop="no"
delimiter=";"
output="target/schema-export.sql">
<fileset dir="target/hbm">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>

</target>

</project>

EXAMPLE MAPPING FILE:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class table="DEMO_ADDRESS" name="amg.demo.model.AddressModel">
<id unsaved-value="null" name="id">
<generator class="native">
<param name="sequence">id_seq</param>
</generator>
</id>
<version unsaved-value="undefined" name="version" column="version" type="java.lang.Long"/>
<property name="firstName"/>
<property name="lastName"/>
<property name="type" type="amg.demo.model.AddressTypeEnum"/>
<property name="zipCode"/>
</class>
</hibernate-mapping>

I wish I would know how to make an inverse operation - I mean having the sql database schema I should generate classes and propably the mapping xml-es.

Could u please give me some example applications/ant or maven scripts with some database sql schemat or some not very complicated links to the tutorials concerning this problem?
[4701 byte] By [haiaw] at [2007-11-11 7:19:44]