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.  [ 8 posts ] 
Author Message
 Post subject: hbm2java problem help needed. Ple-e-ese
PostPosted: Wed Mar 26, 2008 10:26 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
I am just starting with Hibernate Tools and I will really appreciate some help.
I am trying to generate POJOs and DAO code from my hbm files using ant.
Under my project directory I have
ProjectDir
|
+/src - it is empty now
+/etc
| |-Hibernate.cfg.xml
| |-Account.hbm.xml
| |-User.hbm.xml
| |-Address.hbm.xml
| |-Invoice.hbm.xml
|
|-build.xml

I am trying to follow documentation and I have in my cfg.xml
Code:
        <mapping resource="etc/Account.hbm.xml"/>
        <mapping resource="etc/Address.hbm.xml"/>
        <mapping resource="etc/User.hbm.xml"/>
        <mapping resource="etc/Invoice.hbm.xml"/>

My ant target is defined
Code:
    <taskdef name="hibernatetool"
        classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib"/>
    <target name="-pre-compile" depends="pojo-gen,dao-gen"/>
    <target name="pojo-gen">
        <hibernatetool destdir="${src.dir}">           
            <configuration configurationfile="etc/Hibernate.cfg.xml"/>
            <hbm2java jdk5="true" ejb3="false"/>
        </hibernatetool>
    </target>
    <target name="dao-gen">
        <hibernatetool destdir="${src.dir}">           
            <configuration configurationfile="etc/Hibernate.cfg.xml"/>
            <hbm2dao jdk5="true" ejb3="false"/>
        </hibernatetool>
    </target>

When I am running ant, I am getting
Quote:
pojo-gen:
Executing Hibernate Tool with a Standard Configuration
1. task: hbm2java (Generates a set of .java files)
An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
To get the full stack trace run ant with -verbose
org.hibernate.MappingNotFoundException: resource: etc/Account.hbm.xml not found
A resource located at etc/Account.hbm.xml was not found.
Check the following:
1) Is the spelling/casing correct ?
2) Is etc/Account.hbm.xml available via the classpath ?
3) Does it actually exist ?
org.hibernate.MappingNotFoundException: resource: etc/Account.hbm.xml not found
...

It should be something very simple but I do not see it.
I tried to replace resource with the file, take out etc/, hibernate still cannot find the file.
I then decided to go without cfg.xml and changed my ant target to
Code:
    <target name="pojo-gen">
        <hibernatetool destdir="${src.dir}">           
            <configuration>
                <fileset dir="${src.etc.dir}">
                    <include name="**/*.hbm.xml"/>
                </fileset>
            </configuration>
            <hbm2java jdk5="true" ejb3="false"/>
        </hibernatetool>
    </target>

It is now reading the file but throws another exception:
Quote:
An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
To get the full stack trace run ant with -verbose
Failed in building configuration when adding C:\SDF\CodeBase\HBM\etc\Account.hbm.xml
org.hibernate.InvalidMappingException: Could not parse mapping document from file C:\SDF\CodeBase\HBM\etc\Account.hbm.xml
org.hibernate.MappingException: class Account not found while looking for property: accountId
java.lang.ClassNotFoundException: Account
A class were not found in the classpath of the Ant task.
Ensure that the classpath contains the classes needed for Hibernate and your code are in the classpath.
...

It is suppose to generate Account class or I am missing something?
Please give me a tip.
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 10:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you want to generate pojo's your hbm.xml files need to include type information for the properties.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 8:35 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
max wrote:
if you want to generate pojo's your hbm.xml files need to include type information for the properties.

Thank you, Max.
I did make some progress. I added types everywhere and looks like first couple files where processed but then I got an exception on the last one - User.hbm.xml and I cannot figure out what's wrong with it.
Here is the exception:
Quote:
1. task: hbm2java (Generates a set of .java files)
An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
To get the full stack trace run ant with -verbose
org.hibernate.MappingException: Association references unmapped class: User
org.hibernate.MappingException: Association references unmapped class: User
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2377)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2659)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1136)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:302)
at org.hibernate.tool.ant.HibernateToolTask.getProperties(HibernateToolTask.java:318)
at org.hibernate.tool.ant.ExporterTask.configureExporter(ExporterTask.java:94)
at org.hibernate.tool.ant.Hbm2JavaExporterTask.configureExporter(Hbm2JavaExporterTask.java:34)
at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.GeneratedMethodAccessor403.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:277)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:460)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
BUILD FAILED (total time: 0 seconds)

and my hbm file is here:
Code:
<hibernate-mapping package="com.ysi">
    <class name="User" table="YSI_USERS">
        <id name="userId" type="long" access="property">
            <column name="USER_ID" unique-key="PK_USER" sql-type="bigint"/>
            <generator class="native"/>
        </id>
        <property name="firstName" type="String" column="FIRST_NAME" length="32"
                  insert="true" update="true" lazy="false"/>
        <property name="lastName" type="String" column="LAST_NAME" length="64"
                  insert="true" update="true" lazy="false"/>
        <property name="gender" type="char" column="GENDER" length="1"
                  insert="true" update="true" lazy="false"/>
        <property name="age" column="AGE" type="integer"
                  insert="true" update="true" lazy="false"/>
        <property name="created" column="CREATED" type="java.util.Date"
                  insert="false" update="false" lazy="false"/>
        <property name="modified" column="MODIFIED" type="java.util.Date"
                  insert="false" update="false" lazy="false"/>
        <property name="lastLogin" column="LAST_LOGIN" type="java.util.Date"
                  insert="false" update="true" lazy="false"/>
        <many-to-one name="account" access="property" class="com.ysi.Account" outer-join="false"
                 cascade="none" not-found="ignore"
                 update="true" insert="true">
                     <column name="ACCOUNT" sql-type="bigint" not-null="true"/>
                 </many-to-one>
        <many-to-one name="address" access="property" class="com.ysi.Address" outer-join="true"
                cascade="none" update="true" insert="true">
                    <column name="ADDRESS" sql-type="bigint" not-null="false"/>
                </many-to-one>
        <bag name="invoice" outer-join="false" lazy="true">
            <key on-delete="noaction" unique="false"/>
            <one-to-many class="com.ysi.Invoice"/>
        </bag>
        <sql-query name="getUserByName">
            select * from YSI_USERS where FIRST_NAME=:name and LAST_NAME=?
        </sql-query>
        <sql-query name="getYounger">
            select * from YSI_USERS where AGE &lt; :age
        </sql-query>
    </class>
</hibernate-mapping>

If you can point what's wrong here, I would appreciate it a lot.
Thanks, Gary


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 2:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well there are no association inside User to User so this must be some other association in another hbm.xml that is not setup correctly.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 28, 2008 1:59 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
max wrote:
well there are no association inside User to User so this must be some other association in another hbm.xml that is not setup correctly.

Yeah, you are right. Looks like hbm2java wants fully qualified name for the class, although they are in the same package. I put the package in and it went through - java code was generated.
Now can you tell me why it did not work from the cfg.xml file?
I.e., why
<mapping resource="etc/Account.hbm.xml"/>

produces
org.hibernate.MappingNotFoundException: resource: etc/Account.hbm.xml not found

Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 29, 2008 11:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you should use package="x.y.z" in the root element to set which package it is from.

About mapping resource then I assume it errors because etc/Account.hbm.xml is not on your classpath.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 12:41 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
max wrote:
you should use package="x.y.z" in the root element to set which package it is from.

About mapping resource then I assume it errors because etc/Account.hbm.xml is not on your classpath.

I have package specified in the root element. However I still had to give fully qualified name. My Account and Invoice classes are in the same package and I do have in hbm.xml file the following line
Code:
<hibernate-mapping auto-import="true" package="com.ysi">

I still had to specify com.ysi.Account in the Invoice class for it to work.
Regarding the classpath, etc/ is in my classpath. hibernate.cfg.xml file is also in this directory. Do I need to list all hbm.xml files in the classpath? That does not sounds right.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 7:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
greenbee wrote:
I still had to specify com.ysi.Account in the Invoice class for it to work.
Regarding the classpath, etc/ is in my classpath. hibernate.cfg.xml file is also in this directory.


If etc/ is in your classpath then the resource is named hibernate.cfg.xml and not etc/hibernate.cfg.xml

Quote:
Do I need to list all hbm.xml files in the classpath? That does not sounds right.


That is how it works with core hibernate. If you want autodiscovery of these use Hibernate EntityManager which can autoscan for classes and/or hbm.xml files.

_________________
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.  [ 8 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.