-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can anyone explain why I can't generate Java classes?
PostPosted: Wed Nov 22, 2006 4:22 pm 
Newbie

Joined: Wed Nov 22, 2006 4:08 pm
Posts: 4
Hi

I'm trying to teach myself Hibernate and have been following a couple of online tutorials. However, I am getting an error which I can't fix....can anybody shed any light ?!?

I have the following hibernate.cfg.xml file....



Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">dbc:hsqldb:hsql://localhost</property>
        <property name="connection.username"></property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>




        <mapping resource="uk/co/yahoo/wjday/bean/MatchBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/NationalityBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/PlayerBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/PlayerMatchBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/ClubBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/CompetitionBean.hbm.xml"/>
        <mapping resource="uk/co/yahoo/wjday/bean/LoginBean.hbm.xml"/>

    </session-factory>

</hibernate-configuration>



and the following MatchBean.hbm.xml file.....

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">

<hibernate-mapping>
     <class name="uk.co.yahoo.wjday.bean.MatchBean" table="MATCH">
         <id name="id" type="long" column="MATCH_ID">
             <generator class="native"/>
         </id>

          <property name="date"         type="date"    not-null="true"/>
          <property name="venue"        type="string"  not-null="true"/>
          <property name="goalsFor"     type="integer" not-null="true"/>
          <property name="goalsAgainst" type="integer" not-null="true"/>
          <property name="matchReport"  type="text"    not-null="false"/>
          <property name="homeKit"      type="blob"    not-null="false"/>
          <property name="awayKit"      type="blob"    not-null="false"/>
          <many-to-one name="opposition"column="opposition_id"
                      class="ClubBean"/>
          <many-to-one name="competition"column="competition_id"
                      class="CompetitionBean"/>


    </class>
</hibernate-mapping>


I am trying to get Hibernate to create the Java classes for me from these mapping files using the following build script....

Code:
<?xml version="1.0"?>
<project name="maggies" default="dist" basedir=".">

    <!-- Tomcat home directory, the path to where your tomcat is installed -->


    <property name="projname"            value="mags"/>
    <property name="tomcathome"          value="c:/tomcat/apache-tomcat-5.5.16"/>     
    <property name="root"                value="c:/development/mags" />
   
    <property name="src"                 value="${root}/src" />
    <property name="classes"             value="${root}/classes" />   
    <property name="lib"                 value="${root}/lib" />
    <property name="jar"                 value="${root}/jar" />   
    <property name="deploy"              value="${tomcathome}/webapps/${projname}/WEB-INF" />
    <property name="deployClasses"       value="${deploy}/classes" />
    <property name="deployLib"           value="${deploy}/lib" />
    <property name="generateLib"         value="C:/hibernateTools/plugins/org.hibernate.eclipse_3.2.0.beta8/lib" />
    <property name="generateAnnotations" value="${generateLib}/annotations" />
    <property name="generateHibernate"   value="${generateLib}/hibernate" />
    <property name="generateTools"       value="${generateLib}/tools" />
    <property name="velocity"            value="C:/hibernateVelocity/jar" />

    <!-- ***************************************** CLEAN ***************************************** -->

    <target name="clean">
        <delete dir="${classes}" />
        <echo message="Classes deleted"/>
        <delete dir="${tomcathome}/webapps/${projname}" />       
        <echo message="Web application deleted"/>
    </target>
   

    <!-- ***************************************** GENERATE CODE ************************************ -->

     <path id="classpath">
        <fileset dir="${generateAnnotations}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${generateHibernate}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${generateTools}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${velocity}">
            <include name="*.jar" />
        </fileset>
     </path>
     
      <taskdef name="hibernatetool"
         classname="org.hibernate.tool.ant.HibernateToolTask"
         classpathref="classpath" />
     

       <target name="codegen">
          <hibernatetool destdir="${src}/output">
              <configuration configurationfile="${src}/hibernate.cfg.xml">
              </configuration>
              <hbm2java/>
          </hibernatetool>
       </target>

   
    <!-- ***************************************** COMPILE ***************************************** -->
   
    <target name="compile">
        <mkdir dir="${classes}"/>
        <javac destdir="${classes}">
           <src path="${src}" />
           <classpath>
               <fileset dir="${lib}">
              <include name="**/*.jar" />
              <include name="**/*.zip" />
               </fileset>
           </classpath>
        </javac>
        <echo message="Compilation of code completed"/>   
    </target> 
   
    <!-- ***************************************** DEPLOY ***************************************** -->
   
    <target name="dist" depends="compile" >
        <copy todir="${deploy}">
       <fileset dir="${src}/WEB-INF"/>
        </copy>
        <copy todir="${deployClasses}">
       <fileset dir="${classes}"/>
        </copy>
        <copy todir="${deployLib}">
       <fileset dir="${lib}"/>
        </copy>
        <copy todir="${deploy}">
       <fileset dir="${classes}">
               <include name="**/*.jsp" />
               <include name="**/*.html" />
            </fileset>
        </copy>
        <echo message="Application deployed"/>
    </target>


</project>


However, when I invoke the 'codegen' target, I get the following error...

Code:
C:\development\mags>ant codegen
Buildfile: build.xml

codegen:
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[hibernatetool] log4j:WARN No appenders could be found for logger (org.hibernate
.cfg.Environment).
[hibernatetool] log4j:WARN Please initialize the log4j system properly.
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Genera
tes a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.MappingNotFoundException: resource: uk/co/yahoo/wj
day/bean/MatchBean.hbm.xml not found
[hibernatetool] A resource located at uk/co/yahoo/wjday/bean/MatchBean.hbm.xml w
as not found.
[hibernatetool] Check the following:
[hibernatetool]
[hibernatetool] 1) Is the spelling/casing correct ?
[hibernatetool] 2)      Is uk/co/yahoo/wjday/bean/MatchBean.hbm.xml available vi
a the classpath ?
[hibernatetool] 3) Does it actually exist ?

BUILD FAILED
C:\development\mags\build.xml:57: org.hibernate.MappingNotFoundException: resour
ce: uk/co/yahoo/wjday/bean/MatchBean.hbm.xml not found

Total time: 5 seconds
C:\development\mags>


The files definately exist and the casing is correct. Do the *.hbm.xml files have to be on the classpath? The *.hbm.xml and hiberanate.cfg.xml are all under the same project root (I have hibernate.cfg.xml under WEB-INF and the individual *.hbm.xml files in a src sub-directory).

I know that the hibernate.cfg.xml is being found, since if I swap the order of the *.hbm.xml files, the exception complains about a different mapping files not being found (i.e. the first one in the file).

Can anybody spot anything obviously wrong with the above? Your help is greatly appreciated !

Bill


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 11:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
If the hbm.xml should not be on the classpath I would not write it as a check item in the error message.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 4:49 pm 
Newbie

Joined: Wed Nov 22, 2006 4:08 pm
Posts: 4
Hi,

thanks for having a look into this....I have tried adding the mapping files to the classpath as follows:-

Code:
       <target name="codegen">
          <hibernatetool destdir="${src}/output">
              <configuration configurationfile="${src}/hibernate.cfg.xml">
           <fileset dir="${src}/uk/co/yahoo/wjday/bean" >
         <include name="**/*.hbm.xml" />
                  </fileset>
               </configuration>
              <hbm2java/>
          </hibernatetool>
       </target>


But this doesn't seem to make any difference - I am using this site as a reference....

http://www.onjava.com/pub/a/onjava/2005/12/14/hibernate-class-generation-with-hbm2java.html?page=5

I tried adding the following to the <path id....> element instead....

Code:
           <fileset dir="${src}/uk/co/yahoo/wjday/bean" >
         <include name="**/*.hbm.xml" />
                  </fileset>


but this produces a load of

Code:
java.util.zip.ZipException: error in opening zip file


exceptions....

does anyone have any further ideas? I am using hibernate3 btw...

thanks again....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 6:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you are not adding them to the classpath ...you are selecting them to be loaded by the tool manually.

Add it to the classpath task as stated in the tool docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 6:28 pm 
Newbie

Joined: Wed Nov 22, 2006 4:08 pm
Posts: 4
hi,

thanks for helping me on this, but i'm still not having any luck...the build script i have now looks like this (i'm not sure where i am supposed to set the classpath, since i have seen examples with it set in both the places i am setting it below....

Code:
     <path id="project.classpath">
        <fileset dir="${generateAnnotations}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${generateHibernate}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${generateTools}">
            <include name="*.jar" />
        </fileset>
        <fileset dir="${velocity}">
            <include name="*.jar" />
        </fileset>
     </path>
     <path id="schema.export.classpath">
        <path refid="project.classpath" />
        <pathelement location="${src}/uk/co/yahoo/wjday/bean"/>
     </path>

     <taskdef name="hibernatetool"
        classname="org.hibernate.tool.ant.HibernateToolTask"
        classpathref="schema.export.classpath" />

     <target name="codegen">     
         <hibernatetool destdir="${src}/output">
             <classpath>
                 <pathelement path="${src}/uk/co/yahoo/wjday/bean" />
             </classpath>
             <configuration configurationfile="${src}/hibernate.cfg.xml" />             
             <hbm2java />
         </hibernatetool>
     </target>


if anyone coulg help me then I would be really grateful, the error i am getting is...

Code:
[hibernatetool] org.hibernate.MappingNotFoundException: resource: uk/co/yahoo/wj
day/bean/MatchBean.hbm.xml not found
[hibernatetool] A resource located at uk/co/yahoo/wjday/bean/MatchBean.hbm.xml w
as not found.
[hibernatetool] Check the following:
[hibernatetool]
[hibernatetool] 1) Is the spelling/casing correct ?
[hibernatetool] 2)      Is uk/co/yahoo/wjday/bean/MatchBean.hbm.xml available vi
a the classpath ?
[hibernatetool] 3) Does it actually exist ?

BUILD FAILED
C:\development\mags\build.xml:60: org.hibernate.MappingNotFoundException: resour
ce: uk/co/yahoo/wjday/bean/MatchBean.hbm.xml not found


where MatchBean.hbm.xml is the first file in my hibernate.cfg.xml...

Is the classpath being set in the correct place? do i need to jar the *.hbm.xml files ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 4:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
This is most likely wrong:

<pathelement location="${src}/uk/co/yahoo/wjday/bean"/>

The root of your elements should match what you are actually using in the resource mappings, thus the correct one is more likely:

<pathelement location="${src}"/>

And with respect to where classpaths should be then it is quite simple:

<taskdef> has the classpath that is needed to start and use the tools - nothing specific from your project (except maybe the jdbc driver)

<hibernatetool> is the task that runs against your project and here the classpath is for the classes/resources that is needed to access your project and thus it should not contain any duplicates of what is in your taskdef.

Alternatively you can put everything in the taskdefs classpath, but that is definitly not recommended.

_________________
Max
Don't forget to rate


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