-->
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: Using Hibernate model POJO classes from a jar file
PostPosted: Tue May 02, 2006 2:53 pm 
Newbie

Joined: Thu Mar 30, 2006 3:48 pm
Posts: 7
I have a jar file that contains all of my Hibernate model and mapping files. This jar file is included by a plugin that is used by an Eclipse RCP application. My problem is that Hibernate cannot find the hbm files for the model classes. What do I put in my hibernate.cfg.xml file so Hibernate can find my mapping files? I have tried using the
Code:
<mapping resource="..." />
entries, but they cannot be resolved since the hbm files are inside the jar.

I am using the following code to initialize the SessionFactory:
Code:
Configuration config = new Configuration();
config.configure();
sessionFactory = config.buildSessionFactory();


Is there something I can call on the config object? I have tried calling addClass, but that doesn't work either.

Thanks,

Brock Parker


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 4:32 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:57 am
Posts: 33
Location: Alpharetta, Georgia
have you tried addJar?
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/cfg/Configuration.html#addJar(java.io.File)


Top
 Profile  
 
 Post subject: Re: Using Hibernate model POJO classes from a jar file
PostPosted: Tue May 02, 2006 6:53 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
rollingbrock wrote:
I have a jar file that contains all of my Hibernate model and mapping files. This jar file is included by a plugin that is used by an Eclipse RCP application. My problem is that Hibernate cannot find the hbm files for the model classes. What do I put in my hibernate.cfg.xml file so Hibernate can find my mapping files? I have tried using the
Code:
<mapping resource="..." />
entries, but they cannot be resolved since the hbm files are inside the jar.

I am using the following code to initialize the SessionFactory:
Code:
Configuration config = new Configuration();
config.configure();
sessionFactory = config.buildSessionFactory();


Is there something I can call on the config object? I have tried calling addClass, but that doesn't work either.

Thanks,

Brock Parker


You might need to create your own EntityResolver and tell Hibernate to use
it when attempting to configure. Have a look at the code below:
Code:
    public static Configuration createConfiguration(String configName, String schemaName)
    {
        Configuration cfg = new Configuration();
        cfg.setEntityResolver(new SPCTEntityResolver());

        return cfg.configure(configName);
    }


...

    private static class SPCTEntityResolver extends DTDEntityResolver
    {
        public InputSource resolveEntity(String publicId, String systemId)
        {
            if (systemId != null && inclPattern.matcher(systemId).matches())
            {
                Class resCls = NetworkObject.class;
                String rsrcName = systemId.substring(systemId.lastIndexOf('/') + 1);

//                log.debug("trying to locate " + rsrcName + " in classpath");

                // Search for XML Include
                InputStream inclStream = resCls.getResourceAsStream(rsrcName);

                if (inclStream == null)
                    inclStream = resCls.getResourceAsStream("resources/" + rsrcName);

                if (inclStream == null)
                {
                    log.debug(systemId + " not found in classpath");
                    return null;
                }
                else
                {
//                    log.debug("found " + rsrcName + " in classpath");
                    InputSource source = new InputSource(inclStream);
                    source.setPublicId(publicId);
                    source.setSystemId(systemId);
                    return source;
                }
            }

            else return super.resolveEntity(publicId, systemId);
        }
    }



In my case, the hbm files are in a resources subdirectory under the beans
package, and although the path specified in the HIbernate config file
specifies the complete class path, Hibernate could not find the config
files for the beans.

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 8:19 am 
Newbie

Joined: Thu Mar 30, 2006 3:48 pm
Posts: 7
I have tried addJar and that didn't work either. I don't get any errors, but my queries don't return any results.


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.