-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hebernate SessionFactory cannot find hbm.xml file
PostPosted: Thu Apr 09, 2009 10:44 am 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
Throws a MappingNotFoundException in HibernateUtil even though the file is present. The exact error is:
Code:
org.hibernate.MappingNotFoundException: resource: net/ce/common/audit/hibernate/Audit.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)


Using Hibernate 3.3.1, MsSQLServer 2000 with 2005 jdbc drivers, Eclipse Ganymede, jBoss.

hibernate.cfg.xml is in WEB-INF/classes. hbm files are in src/net/ce/common/audit/hibernate and were generated from the database using HibernateTools.

HibernateUtil is the standard one from the reference manual:
Code:
public class HibernateUtil {
  private static final Logger log = Logger.getLogger(HibernateUtil.class);
  private static final SessionFactory sessionFactory;
 
  static {
    try {
      sessionFactory = new Configuration().configure().buildSessionFactory();
    }catch(Throwable e){
      log.error("Failed to create a SessionFactory from hibernate.cfg.xml", e);
      throw new ExceptionInInitializerError(e);
    }
  }
 
  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}


Hibernate.cfg.xml is nothing special. Audit contains Sets of the other two classes, but to keep things simple for the first test I pass empty sets for the associated tables.
Code:
<hibernate-configuration>
    <session-factory name="hibernateFactory">
        <!-- Database connection -->
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="hibernate.connection.password">myPWD</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://myHost:1433;DatabaseName=MyDB</property>
        <property name="hibernate.connection.username">myUsername</property>
       
        <!-- Other properties -->
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
       
        <!-- Mappings used -->
        <mapping resource="net/ce/common/audit/hibernate/Audit.hbm.xml" />
        <mapping resource="net/ce/common/audit/hibernate/AuditDataIds.hbm.xml" />
        <mapping resource="net/ce/common/audit/hibernate/AuditFieldsModified.hbm.xml" />
    </session-factory>
</hibernate-configuration>


Generated Audit.hbm.xml:
Code:
<hibernate-mapping>
    <class name="net.ce.common.audit.hibernate.Audit" table="audit" schema="dbo" catalog="CE_CUSTOM">
        <id name="auditId" type="long">
            <column name="audit_id" />
            <generator class="assigned" />
        </id>
        <timestamp name="timestamp" column="timestamp" />
        <property name="applicationId" type="string">
            <column name="application_id" length="10" not-null="true" />
        </property>
        <property name="actionId" type="string">
            <column name="action_id" length="20" not-null="true" />
        </property>
        <property name="userId" type="string">
            <column name="user_id" length="40" not-null="true" />
        </property>
        <property name="affectedDataType" type="string">
            <column name="affected_data_type" length="20" />
        </property>
        <set name="auditFieldsModifieds" inverse="true">
            <key>
                <column name="audit_id" not-null="true" />
            </key>
            <one-to-many class="net.ce.common.audit.hibernate.AuditFieldsModified" />
        </set>
        <set name="auditDataIdses" inverse="true">
            <key>
                <column name="audit_id" not-null="true" />
            </key>
            <one-to-many class="net.ce.common.audit.hibernate.AuditDataIds" />
        </set>
    </class>
</hibernate-mapping>


Calling code (skipping the generated DAO to troubleshoot):
Code:
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    session.save(audit);
    session.getTransaction().commit();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 11:06 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
set class path to "net" directory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 11:10 am 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
parmendratyagi wrote:
set class path to "net" directory.

It is a web application, so anything under src gets compiled to WEB-INF/classes during build/deploy and is therefore in the classpath.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 11:28 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Please make sure, you are coping the .hbm file to WEB-INF/classes/net/... respective folder in build.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 11:30 am 
Beginner
Beginner

Joined: Sun Dec 28, 2008 6:01 pm
Posts: 24
I hope you are coping you .hbm.xml files as well into your classes directory while creating .war or .ear file.

____________________________________________
Please do RATE if this post was helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 1:20 pm 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
You know... you are quite right.

I got suckered by Eclipse - it only deploys the class files to WEB-INF/classes, not the other files! I sooo miss being able to use Netbeans - it is far more predictable. Thanks for your reply, I hadn't thought to double-check that Eclipse was really copying the files.

Is there a best practice for where to put the hbm.xml files when using Eclipse? Putting them directly in WEB-INF/classes 'feels wrong'.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 2:14 pm 
Beginner
Beginner

Joined: Sun Dec 28, 2008 6:01 pm
Posts: 24
Quote:
Is there a best practice for where to put the hbm.xml files when using Eclipse? Putting them directly in WEB-INF/classes 'feels wrong'.


I use ant build.xml to build .war files and then deploy it in the server. Surely, using build script is one way.

______________________________________________
Please do RATE if this post was helpful


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