-->
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.  [ 10 posts ] 
Author Message
 Post subject: .configure(mycfg)....not found but its there.
PostPosted: Mon Jul 11, 2005 11:16 am 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
I am trying to debug the following code in Eclipse 3.0.

The configuration xml exists at "C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml"

but it throws a not found exception. The TestEvent.hbm.xml also exists at the path specified and is the same hbm as found in the Event tutorial.


Hibernate version:Hibernate3.jar (3.0)

Code:
//constructor
public HibernateUtil(String cfgPath) {

   try {
      configuration = new Configuration().configure(cfgPath);
   } catch (HibernateException e) {
      e.printStackTrace();
   }

}


Hibernate.cfg.xml at that location
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>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:data/test</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="transaction.factory_class">
             org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <property name="hibernate.cache.provider_class">
             org.hibernate.cache.HashtableCacheProvider
        </property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/TestEvent.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Full stack trace of exceptions:

org.hibernate.HibernateException: C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml not found
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1087)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1111)
at com.ajcs.hibernatestores.HibernateUtil.<init>(HibernateUtil.java:83)
at com.ajcs.hibernatestores.test.HibernateStoresTests.testSaveBean(HibernateStoresTests.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Initial SessionFactory creation failed.java.lang.NullPointerException

_________________
Mike "Ollie" Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 1:15 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 7:12 pm
Posts: 25
I assume that you tried changing the '.'s to '/'s in this line?


<mapping
resource="C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/TestEvent.hbm.xml"/>


Top
 Profile  
 
 Post subject: No you assume incorrectly
PostPosted: Mon Jul 11, 2005 1:47 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
The actual path to the file not found is:

Code:
C:\Java\eclipse\workspace\HibernateStores\resources\Test\com.ajcs.hibernatestores.test.TestEvent\Hibernate.cfg.xml


The same test suite has the following test case and does NOT fail:

Code:
public void testReadCfg() {
   File cfgFile = new File("/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml");
   FileInputStream fis = null;
   try {
      if (cfgFile != null && cfgFile.exists()){
         fis = new FileInputStream( cfgFile );
         Assert.assertNotNull("Cannot open input stream",fis);
      }
   } catch (FileNotFoundException e) {
      Assert.fail(e.getLocalizedMessage());
   }
}


So does that mean that Hibernate treats the path to the file differently?
[/quote]

_________________
Mike "Ollie" Oliver


Top
 Profile  
 
 Post subject: DEBUG LOG
PostPosted: Mon Jul 11, 2005 1:54 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
Code:
[2005-07-11 10:51:46,546] INFO  Environment                    Hibernate 3.0.5
[2005-07-11 10:51:46,562] INFO  Environment                    hibernate.properties not found
[2005-07-11 10:51:46,578] INFO  Environment                    using CGLIB reflection optimizer
[2005-07-11 10:51:46,578] INFO  Environment                    using JDK 1.4 java.sql.Timestamp handling
[2005-07-11 10:51:47,187] INFO  Configuration                  configuring from resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml
[2005-07-11 10:51:47,187] INFO  Configuration                  Configuration resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml
[2005-07-11 10:51:47,203] WARN  Configuration                  C:/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/Hibernate.cfg.xml not found

_________________
Mike "Ollie" Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 1:54 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 7:12 pm
Posts: 25
I think Hibernate uses ClassLoader.getResource()/getResourceAsStream():


/**
* Finds the resource with the given name. A resource is some data
* (images, audio, text, etc) that can be accessed by class code in a way
* that is independent of the location of the code.
*
* <p> The name of a resource is a '<tt>/</tt>'-separated path name that
* identifies the resource.
*
* <p> This method will first search the parent class loader for the
* resource; if the parent is <tt>null</tt> the path of the class loader
* built-in to the virtual machine is searched. That failing, this method
* will invoke {@link #findResource(String)} to find the resource. </p>
*
* @param name
* The resource name
*
* @return A <tt>URL</tt> object for reading the resource, or
* <tt>null</tt> if the resource could not be found or the invoker
* doesn't have adequate privileges to get the resource.
*
* @since 1.1
*/
public URL getResource(String name) {


Top
 Profile  
 
 Post subject: DOTS NOT IT
PostPosted: Mon Jul 11, 2005 2:01 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
Just to be sure that the dots in the file spec directory are NOT the problem, I changed the code and put copies of the Hibernate.cfg.xml up one directory and ran the test again.

sonsole
Code:
[2005-07-11 10:58:26,984] DEBUG HibernateStoresTests          : <?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_06" class="java.beans.XMLDecoder">
<object class="com.ajcs.hibernatestores.test.TestEvent">
  <void property="date">
   <object class="java.util.Date">
    <long>1121104706515</long>
   </object>
  </void>
  <void property="title">
   <string>My First Hibernate Test Event</string>
  </void>
</object>
</java>

[2005-07-11 10:58:26,984] DEBUG HibernateStoresTests          : <?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_06" class="java.beans.XMLDecoder">
<object class="com.ajcs.hibernatestores.test.TestEvent">
  <void property="date">
   <object class="java.util.Date">
    <long>1121104706515</long>
   </object>
  </void>
  <void property="title">
   <string>My First Hibernate Test Event</string>
  </void>
</object>
</java>

[2005-07-11 10:58:36,703] INFO  Environment                   : Hibernate 3.0.5
[2005-07-11 10:58:36,703] INFO  Environment                   : Hibernate 3.0.5
[2005-07-11 10:58:36,718] INFO  Environment                   : hibernate.properties not found
[2005-07-11 10:58:36,718] INFO  Environment                   : hibernate.properties not found
[2005-07-11 10:58:36,734] INFO  Environment                   : using CGLIB reflection optimizer
[2005-07-11 10:58:36,734] INFO  Environment                   : using CGLIB reflection optimizer
[2005-07-11 10:58:36,734] INFO  Environment                   : using JDK 1.4 java.sql.Timestamp handling
[2005-07-11 10:58:36,734] INFO  Environment                   : using JDK 1.4 java.sql.Timestamp handling
[2005-07-11 10:58:37,250] INFO  Configuration                 : configuring from resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml
[2005-07-11 10:58:37,250] INFO  Configuration                 : configuring from resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml
[2005-07-11 10:58:38,796] INFO  Configuration                 : Configuration resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml
[2005-07-11 10:58:38,796] INFO  Configuration                 : Configuration resource: C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml
[2005-07-11 10:58:38,812] WARN  Configuration                 : C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml not found
[2005-07-11 10:58:38,812] WARN  Configuration                 : C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml not found
org.hibernate.HibernateException: C:/Java/eclipse/workspace/HibernateStores/resources/Test/Hibernate.cfg.xml not found
   at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1087)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1111)
   at com.ajcs.hibernatestores.HibernateUtil.<init>(HibernateUtil.java:83)
   at com.ajcs.hibernatestores.test.HibernateStoresTests.testSaveBean(HibernateStoresTests.java:161)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

_________________
Mike "Ollie" Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 2:09 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 7:12 pm
Posts: 25
So let me get this straight - in the source dir of your project, you now have a directory named resources with a subdir named Test and in that Test directory, your Hibernate.cfg.xml exists?


Top
 Profile  
 
 Post subject: RTFJD
PostPosted: Mon Jul 11, 2005 2:37 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
Ok a closer review of the JavaDocs and I see:

Code:
configure(String resource)

Use the mappings and properties specified in the given application resource. The format of the resource is defined in hibernate-configuration-3.0.dtd.

The resource is found via getConfigurationInputStream(resource).


and

Code:
configure(File configFile)

    Use the mappings and properties specified in the given application file. The format of the file is defined in hibernate-configuration-3.0.dtd.

    Parameters:
        configFile - File from which you wish to load the configuration


So I changed my code to:

Code:
public HibernateUtil(String cfgPath) {

   try {
      File cfgFile = new File(cfgPath);
      configuration = new Configuration().configure(cfgFile);
   } catch (HibernateException e) {
      e.printStackTrace();
   }
}


SUCCESS!

But Now I am getting a similar mapping exception upon reading the configuration file.

Code:
[2005-07-11 11:24:29,765] INFO  Environment                   : Hibernate 3.0.5
[2005-07-11 11:24:29,781] INFO  Environment                   : hibernate.properties not found
[2005-07-11 11:24:29,781] INFO  Environment                   : using CGLIB reflection optimizer
[2005-07-11 11:24:29,781] INFO  Environment                   : using JDK 1.4 java.sql.Timestamp handling
[2005-07-11 11:24:30,171] INFO  Configuration                 : configuring from file: Hibernate.cfg.xml
[2005-07-11 11:24:30,531] DEBUG DTDEntityResolver             : trying to locate http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd in classpath under org/hibernate/
[2005-07-11 11:24:30,531] DEBUG DTDEntityResolver             : found http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd in classpath
[2005-07-11 11:24:30,671] DEBUG Configuration                 : hibernate.connection.driver_class=org.hsqldb.jdbcDriver
[2005-07-11 11:24:30,687] DEBUG Configuration                 : hibernate.connection.url=jdbc:hsqldb:data/test
[2005-07-11 11:24:30,687] DEBUG Configuration                 : hibernate.connection.username=sa
[2005-07-11 11:24:30,687] DEBUG Configuration                 : hibernate.connection.password=
[2005-07-11 11:24:30,687] DEBUG Configuration                 : dialect=org.hibernate.dialect.HSQLDialect
[2005-07-11 11:24:30,687] DEBUG Configuration                 : show_sql=true
[2005-07-11 11:24:30,687] DEBUG Configuration                 : transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
[2005-07-11 11:24:30,687] DEBUG Configuration                 : hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
[2005-07-11 11:24:30,687] DEBUG Configuration                 : hibernate.hbm2ddl.auto=update
[2005-07-11 11:24:30,687] DEBUG Configuration                 : null<-org.dom4j.tree.DefaultAttribute@faa550 [Attribute: name resource value "com.ajcs.hibernatestores.test.TestEvent"]
[2005-07-11 11:24:30,687] INFO  Configuration                 : Mapping resource: com.ajcs.hibernatestores.test.TestEvent
org.hibernate.MappingException: Resource: com.ajcs.hibernatestores.test.TestEvent not found
   at org.hibernate.cfg.Configuration.addResource(Configuration.java:447)
   at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
   at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
   at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
   at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1146)
   at com.ajcs.hibernatestores.HibernateUtil.<init>(HibernateUtil.java:86)
   at com.ajcs.hibernatestores.test.HibernateStoresTests.testSaveBean(HibernateStoresTests.java:161)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Initial SessionFactory creation failed.java.lang.NullPointerException


So I looked in the dtd and found the change in the hibernate.cfg.xml to use a mapping file vs. a mapping resource so my Hibernate.cfg.xml looks like:

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>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:data/test</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="transaction.factory_class">
             org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <property name="hibernate.cache.provider_class">
             org.hibernate.cache.HashtableCacheProvider
        </property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping file="/Java/eclipse/workspace/HibernateStores/resources/Test/com.ajcs.hibernatestores.test.TestEvent/TestEvent.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


And now it all WORKS!

Thanks and I hope this helps someone else.

_________________
Mike "Ollie" Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 2:45 pm 
Beginner
Beginner

Joined: Thu Jul 07, 2005 7:12 pm
Posts: 25
Ok, but just bear in mind that won't work if you package things up in a JAR/WAR/EAR.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Jul 11, 2005 2:57 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 6:51 pm
Posts: 27
Location: Philippines
Right, this is a test harness, not to be deployed but to be used in development unit testing prior to deployment.

_________________
Mike "Ollie" Oliver


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