Hi all,
I was trying to Export Schema to my MySQL installation using an ant-task when the whole thing failed giving a NullPointerExcetion :-(
My apologies if you see any bad prctice/silly mistake. I am pretty new to Hibernate.
There are two classes Event and Location, for a simple calendar min-project. Event is supposed to contain instances of Location.
For sake of ease I have posted hbm.xml of both Location and Event.
I fail to understand why is the schema export is failing.
Thanks in advance.
Hibernate version:
3.2
Name and version of the database I am using:
MySQL 5.0
Mapping documents:
hibernate.cfg.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">
root
</property>
<property name="connection.password">
abc@123
</property>
<property name="connection.url">
jdbc:mysql://localhost/events_calendar
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/manning/hq/ch04/Event.hbm.xml"/>
<mapping resource="com/manning/hq/ch04/Location.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Location.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manning.hq.ch04">
<class name="Location" table="locations">
<id name="id" column="uid" type="long">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="address" type="string" />
</class>
</hibernate-mapping>
Event.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manning.hq.ch04">
<class name="Event" table="events">
<id name="id" column="uid" type="long">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="startDate" column="start_date" type="date"/>
<property name="duration" type="integer" />
<many-to-one name="location" column="location_id" class="Location"/>
</class>
</hibernate-mapping>
Ant FileCode:
<?xml version="1.0" ?>
<project name="build.xml" default="build" basedir=".">
<!-- properties -->
<property name="src.java.dir" value="src/java"/>
<property name="build.classes.dir" value="build/classes"/>
<property name="hibernate.version" value="3.2"/>
<property name="hibernate.lib.dir" location="H:\hibernate-${hibernate.version}"/>
<property name="mysql.jdbc.version" value="5.0.4"/>
<property name="jdbc.lib.dir" location="H:\apache\zips"/>
<property name="jdbc.driver.jar" value="${jdbc.lib.dir}/mysql-connector-java-${mysql.jdbc.version}-bin.jar"/>
<!-- Defines classpath with Hibernate and all its jars -->
<path id="hibernate.lib.path">
<fileset dir="${hibernate.lib.dir}/lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${hibernate.lib.dir}">
<include name="hibernate3.jar"/>
</fileset>
</path>
<!-- define classpath -->
<path id="project.classpath">
<pathelement location="${build.classes.dir}"/>
</path>
<path id="runtime.classpath">
<path refid="project.classpath"/>
<path refid="hibernate.lib.path"/>
<pathelement location="${jdbc.driver.jar}"/>
<pathelement location="${src.java.dir}"/>
</path>
<!-- folders creation -->
<target name="init">
<mkdir dir="${build.classes.dir}"/>
<echo message="Done"/>
</target>
<!-- compilation -->
<target name="compile" depends="init">
<javac srcdir="${src.java.dir}"
debug="on"
destdir="${build.classes.dir}" >
<classpath refid="hibernate.lib.path"/>
</javac>
</target>
<!-- execute class -->
<target name="build" depends="compile">
<java classname="com.manning.hq.ch04.EventLoader"
failonerror="true">
<classpath refid="runtime.classpath"/>
</java>
</target>
<!-- Clean Up -->
<target name="clean">
<delete dir="${build.classes.dir}"/>
</target>
<target name="schema-export" depends="compile">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask">
<classpath refid="runtime.classpath"/>
</taskdef>
<schemaexport config="${src.java.dir}/hibernate.cfg.xml"/>
</target>
</project>
Full stack trace of any exception that occurs:Code:
$ ant -verbose schema-export
Apache Ant version 1.6.5 compiled on June 2 2005
Buildfile: build.xml
Detected Java version: 1.5 in: c:\Progra~1\Java\jdk1.5.0_09\jre
Detected OS: Windows XP
parsing buildfile H:\WORKSPACE\calendar\build.xml with URI = file:///H:/WORKSPACE/calendar/build.xml
Project base dir set to: H:\WORKSPACE\calendar
Build sequence for target(s) `schema-export' is [init, compile, schema-export]
Complete build sequence is [init, compile, schema-export, clean, build, ]
init:
[echo] Done
compile:
[javac] com\manning\hq\ch02\EventCalendar.java omitted as com/manning/hq/ch02/EventCalendar.class is up to date.
[javac] com\manning\hq\ch04\Event.hbm.xml skipped - don't know how to handle it
[javac] com\manning\hq\ch04\Event.java omitted as com/manning/hq/ch04/Event.class is up to date.
[javac] com\manning\hq\ch04\EventLoader.java omitted as com/manning/hq/ch04/EventLoader.class is up to date.
[javac] com\manning\hq\ch04\Location.hbm.xml skipped - don't know how to handle it
[javac] com\manning\hq\ch04\Location.java omitted as com/manning/hq/ch04/Location.class is up to date.
[javac] hibernate.cfg.xml skipped - don't know how to handle it
schema-export:
[schemaexport] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[schemaexport] log4j:WARN Please initialize the log4j system properly.
BUILD FAILED
H:\WORKSPACE\calendar\build.xml:69: java.lang.NullPointerException
at org.hibernate.tool.hbm2ddl.SchemaExportTask.execute(SchemaExportTask.java:158)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: java.lang.NullPointerException
at org.hibernate.tool.hbm2ddl.SchemaExportTask.getSchemaExport(SchemaExportTask.java:218)
at org.hibernate.tool.hbm2ddl.SchemaExportTask.execute(SchemaExportTask.java:146)
... 12 more
--- Nested Exception ---
java.lang.NullPointerException
at org.hibernate.tool.hbm2ddl.SchemaExportTask.getSchemaExport(SchemaExportTask.java:218)
at org.hibernate.tool.hbm2ddl.SchemaExportTask.execute(SchemaExportTask.java:146)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Total time: 4 seconds
[/b]