-->
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: Dynamically Adding Hibernate Mapping Files
PostPosted: Thu Feb 17, 2005 8:11 pm 
Newbie

Joined: Thu Feb 17, 2005 7:32 pm
Posts: 3
I have the following bit of code:

Code:
package models.dao;

import general.Functions;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import net.sf.hibernate.cfg.Configuration;

import java.io.File;

/**
* This class has been automatically generated by Hibernate Synchronizer.
* For more information or documentation, visit The Hibernate Synchronizer page
* at http://www.binamics.com/hibernatesync or contact Joe Hudson at joe@binamics.com.
*/
public class _RootDAO extends models.base._BaseRootDAO {
   public static String configFile;   
   
   public _RootDAO() {   }
   
   public Class getReferenceClass() { return java.lang.Object.class; }
   
   public void startup(String configFileName) throws HibernateException {
      setConfigurationFileName(configFileName);
      
      if (null == configFileName && sessionFactoryMap.size() > 0) return;
      else if (null != sessionFactoryMap.get(configFileName)) return;
      else {
         Configuration cfg = new Configuration();
         if (null == configFileName)   {
            cfg.configure();
         } else {
            File file = new File(configFileName);
            cfg.addDirectory(new File(file.getParent() + File.separator + "somesubdir" + File.separator + "models"));
            cfg.configure(file);  //Fails Here!
         }
                           
         setSessionFactory(configFileName, cfg.buildSessionFactory());
      }
   }
   
   public void setConfigurationFileName(String configFileName) { configFile = configFileName; }
   public String getConfigurationFileName() { return configFile; }
}


I call startup("C:\Work\somedir\hibernate.cfg.xml"). It sucessfully adds all of the classes in the models directory (C:\work\somedir\somesubdir\models). However, when it tries to parse the hibernate.cfg.xml file it throw an error. I'm 90% sure this error is coming from the fact that I do not have any mappings in the hibernate.cfg.xml file itself - they should have been done when I called configuration.addDirectory(dir). Is there a way around this? It seems to me that you should not have to require any mappings to occur in the hibernate.cfg.xml file as long as they are added elsewhere. Thanks in advance for any help.

Below is some debug information that might be useful:

Hibernate version:
2.1.8

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

<hibernate-configuration>
   <session-factory>
      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:mysql://111.111.111.111:3306/somedb</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username">hibernate</property>
      <property name="hibernate.connection.password">hibernate</property>
      <!-- DBCP connection pooling options -->
      <property name="hibernate.dbcp.maxWait">3000</property>
      <property name="hibernate.dbcp.maxIdle">100</property>
      <property name="hibernate.dbcp.maxActive">10</property>
      <property name="hibernate.dbcp.autoReconnectForPool">true</property>
      <property name="hibernate.dbcp.autoReconnect">true</property>
      <!-- dialect for MySQL -->
      <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
      
      <property name="hibernate.use_outer_join">true</property>
      <property name="hibernate.show_sql">true</property>
           
   </session-factory>
</hibernate-configuration>



Full stack trace of any exception that occurs:
Code:
net.sf.hibernate.HibernateException: problem parsing configurationC:\Work\somedir\hibernate.cfg.xml
   at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:972)
   at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:945)
   at models.dao._RootDAO.startup(_RootDAO.java:36)
   at ObjectInterface.initialize(ObjectInterface.java:28)
   at tests.Test6.main(Test6.java:25)
Caused by: net.sf.hibernate.MappingException: invalid configuration
   at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:968)
   ... 4 more
Caused by: org.xml.sax.SAXParseException: Element "session-factory" requires additional elements.
   at org.apache.crimson.parser.Parser2.error(Unknown Source)
   at org.apache.crimson.parser.ValidatingParser$ChildrenValidator.done(Unknown Source)
   at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
   at org.apache.crimson.parser.Parser2.content(Unknown Source)
   at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
   at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
   at org.apache.crimson.parser.Parser2.parse(Unknown Source)
   at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
   at org.dom4j.io.SAXReader.read(SAXReader.java:339)
   at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:967)
   ... 4 more
Exception in thread "main"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 11:52 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
AFIAK you have to patch the DTD to have no mapping information in the cfg.xml.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 1:37 pm 
Newbie

Joined: Thu Feb 17, 2005 7:32 pm
Posts: 3
I'm not quite sure what you're saying. How do I do that? Do I just modify the DTD information at the top of the cfg.xml file? If so, what DTD information would I supply it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 6:02 pm 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
what ernst_pluess means is that you have to patch the hibernate-configuration.dtd ...

that's what you'll find:
Code:
<!ELEMENT session-factory (property*, mapping+, (class-cache|collection-cache|jcs-class-cache|jcs-collection-cache)*)>
<!ATTLIST session-factory name CDATA #IMPLIED> <!-- the JNDI name -->


the "mapping+" means, that you need at least one mapping-declaration ...

I don't know what will happen if you download that dtd, change "mapping+" in "mapping*", change the dtd-reference in your mapping-file to your own, changed one and start your app ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 8:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It will work

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2005 5:13 pm 
Newbie

Joined: Thu Feb 17, 2005 7:32 pm
Posts: 3
I just tried it and it works fine, thanks guys!


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.