My build.xml file creates a table, however it would only create the table that the hibernate-2.1 folder provides.
Here is my build.xml file:
<?xml version="1.0"?>
<project name="test" default="db" basedir=".">
<!-- Set up properties containing important project directories -->
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="lib.dir" value="lib"/>
<property name="data.dir" value="data"/>
<!-- Set up the class path for compilation and execution -->
<path id="project.class.path">
<!-- Include our own classes, of course -->
<pathelement location="${class.root}" />
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="db" description="Runs HSQLDB database management UI
against the database file--use when application is not running">
<java classname="org.hsqldb.util.DatabaseManager"
fork="yes">
<classpath refid="project.class.path"/>
<arg value="-driver"/>
<arg value="org.gjt.mm.mysql.Driver"/>
<arg value="-url"/>
<arg value="jdbc:mysql:${data.dir}/music"/>
<arg value="-user"/>
<arg value="sa"/>
</java>
</target>
<target name="codegen"
description="Generate Java source from the O/R mapping files">
<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="project.class.path"/>
<hbm2java output="${source.root}">
<fileset dir="${source.root}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
<!-- Create our runtime subdirectories and copy resources into them -->
<target name="prepare" description="Sets up build structures">
<mkdir dir="${class.root}"/>
<!-- Copy our property files and O/R mappings for use at runtime -->
<copy todir="${class.root}" >
<fileset dir="${source.root}" >
<include name="**/*.properties"/>
<include name="**/*.hbm.xml"/>
</fileset>
</copy>
</target>
<!-- Compile the java source of the project -->
<target name="compile" depends="prepare"
description="Compiles all Java classes">
<javac srcdir="${source.root}"
destdir="${class.root}"
debug="on"
optimize="off"
deprecation="on">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="schema" depends="compile"
description="Generate DB schema from the O/R mapping files">
<!-- Teach Ant how to use Hibernate's schema generation tool -->
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.class.path"/>
<schemaexport properties="${class.root}/hibernate.properties"
quiet="no" text="no" drop="no" delimiter=";">
<fileset dir="${class.root}">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>
</project>
In the src folder that is provided to us in the hibernate-2.1 folder, in net.sf.hibernate.odmg there is a Name.java class and a Name.hbm.xml file . Here are those 2 files:
//$Id: Name.java,v 1.3.2.1 2003/08/09 11:24:47 turin42 Exp $
package net.sf.hibernate.odmg;
import java.io.Serializable;
/**
* An ODMG name that may be bound to a persistent object.
*/
public class Name {
private String name;
private Class persistentClass;
private Serializable id;
public Name(String name, Class persistentClass, Serializable id) {
this.name = name;
this.persistentClass = persistentClass;
this.id = id;
}
public Name() {}
/**
* Returns the name. JavaDoc requires a second sentence.
* @return String
*/
public String getName() {
return name;
}
/**
* Returns the persistentClass. JavaDoc requires a second sentence.
* @return Class
*/
public Class getPersistentClass() {
return persistentClass;
}
/**
* Sets the name. JavaDoc requires a second sentence.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the persistentClass. JavaDoc requires a second sentence.
* @param persistentClass The persistentClass to set
*/
public void setPersistentClass(Class persistentClass) {
this.persistentClass = persistentClass;
}
/**
* Returns the id. JavaDoc requires a second sentence.
* @return Serializable
*/
public Serializable getId() {
return id;
}
/**
* Sets the id. JavaDoc requires a second sentence.
* @param id The id to set
*/
public void setId(Serializable id) {
this.id = id;
}
}
Here's Name.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!-- A default mapping for the ODMG Name class. Applications
may specify their own mappings instead. -->
<class name="net.sf.hibernate.odmg.Name">
<id name="name" type="string">
<generator class="assigned"/>
</id>
<property name="persistentClass" type="class" not-null="true"/>
<property name="id" type="serializable" not-null="true"/>
</class>
<query name="byName">from n in class net.sf.hibernate.odmg.Name where n.name = ?</query>
</hibernate-mapping>
Now in the hibernate-2.1 folder I created my own foler called practice9. I copied and pasted the src folder given to us.
Now it creates the Name table in my database which is good.
However when I create my own .java and hbm.xml files it won't work. Why is that? When I type ant schema in the command line I get the error that the persistent class is not found. So if I had a COllection.hbm.xml file and a Collection.java file it would say 'persistent class Collection not found'.
Also, about my build files, my database is MySQL 4.0. So I was wondering if this part of my build.xml file was right:
<target name="db" description="Runs HSQLDB database management UI
against the database file--use when application is not running">
<java classname="org.hsqldb.util.DatabaseManager"
fork="yes">
<classpath refid="project.class.path"/>
<arg value="-driver"/>
<arg value="org.gjt.mm.mysql.Driver"/>
<arg value="-url"/>
<arg value="jdbc:mysql:${data.dir}/music"/>
<arg value="-user"/>
<arg value="sa"/>
</java>
</target>
Thx for all the help!
Calc
|