-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping an hbm.xml file with the generated classes properly
PostPosted: Wed Feb 16, 2005 5:31 am 
Newbie

Joined: Thu Jun 17, 2004 4:16 pm
Posts: 18
Hello all, How can I map the mapping hibernate files (*.hbm.xml) with the generated classes with my current project structure?

My project uses only ony *.hmb.xml file with many class elements.
The file paths of the files are explained below:
Mapping xml file(*.hbm.xml): [PROJECT_PATH]/src/META-INF
Generated classes: [PROJECT_PATH]/src/essex/cc403

Hibernate version:
hibernate-2.1.8
Mapping documents:
Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

    <class name="essex.cc403.hbeans.User" table="USER">
        <meta attribute="class-description">
        Represents a user in the database.
        @author Konstantinos Karadamoglou
        </meta>
        <id name="id" type="int" column="USER_ID">
            <meta attribute="scope-set">protected</meta>
            <generator class="native"/>
        </id>
       
        <property name="password" type="string" not-null="true"/>
        <property name="firstName" type="string" not-null="true"/>
        <property name="lastName" type="string"  not-null="true"/>
        <property name="addLine1" type="string"  not-null="true"/>
        <property name="addLine2" type="string"  not-null="false"/>
        <property name="city" type="string"  not-null="true"/>
        <property name="county" type="string"  not-null="true"/>
       
        <many-to-one name="rank" class="essex.cc403.hbeans.Rank" column="RANK_ID"/>
       
    </class>
   
    <class name="essex.cc403.hbeans.Rank" table="RANK">

        <meta attribute="class-description">
            Represents a rank in the database.
            @author Konstantinos Karadamoglou
        </meta>

        <id name="rankID" type="int" column="RANK_ID">
            <meta attribute="scope-set">protected</meta>
            <generator class="native"/>
        </id>

        <property name="name" type="string">
            <meta attribute="use-in-tostring">true</meta>
            <column name="NAME" not-null="true" unique="true" index="RANK_NAME"/>
        </property>

        <property name="canBook" type="boolean" not-null="true"/>
        <property name="canDeleteAccount" type="boolean" not-null="true"/>
        <property name="canViewBookings" type="boolean" not-null="true"/>
        <property name="canEditBookings" type="boolean" not-null="true"/>
        <property name="canCancelBookings" type="boolean" not-null="true"/>
       
        <set name="Users" cascade="all" inverse="true" lazy="true">
          <key column="RANK_ID"/>
          <one-to-many class="essex.cc403.hbeans.User"/>
        </set>

    </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
Session session = sessionFactory.openSession();
            Transaction tx = null;

            try {
                // Create some data and persist it
                tx = session.beginTransaction();

                Rank rank = new Rank(true, false, true, false, true, null);
                session.save(rank);

                User user = new User("123", "James", "Brown", "St. Paul 21",
                                     "Colchester", "United Kingdom");
                session.save(user);

                // We're done; make our changes permanent
                tx.commit();
            } catch (Exception e) {
                if (tx != null) {
                    // Something went wrong; discard all partial changes
                    tx.rollback();
                }

                throw e;
            } finally {
                // No matter what, close the session
                session.close();
            }

Full stack trace of any exception that occurs:
Code:
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
net.sf.hibernate.MappingException: Resource: essex/cc403/hbeans/User.hbm.xml not found
    at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:352)
    at essex.cc403.Main.main(Main.java:43)

Name and version of the database you are using:
hsqldb-1.7.3.3
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 5:51 am 
Newbie

Joined: Wed Sep 01, 2004 9:04 am
Posts: 18
Location: Enschede, Netherlands
You need to put the documents in the right directory.

net.sf.hibernate.MappingException: Resource: essex/cc403/hbeans/User.hbm.xml not found indicates you need to put the mapping file in that directory, not in /src/META-INF.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 6:02 am 
Newbie

Joined: Thu Jun 17, 2004 4:16 pm
Posts: 18
But can I use my file structure and then give directions to hibernate on how to find each file?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 9:37 am 
Newbie

Joined: Wed Sep 01, 2004 9:04 am
Posts: 18
Location: Enschede, Netherlands
In your hibernate.cfg.xml configuration file you can give directions to Hibernate on where to find the mapping files.

See section 3.8 of the reference:
http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html#configuration-xmlconfig


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