-->
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.  [ 15 posts ] 
Author Message
 Post subject: hibernate.cfg.xml / hibernate.properties best practices
PostPosted: Wed Sep 21, 2005 8:36 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
One of my projects has several environments, local devs, a test server, a QA server, staging, and production. This means there are 5 hibernate.cfg.xml files in our CVS repository, each with an identical list of mappings. If the mappings get updated, they need to be updated in five places, and then other developers need to figure out that they need to update there local machine config file, and in general it's a bit of pain.

My solution was to break out the database connection and other configs into various hibernate.properties files and to have ONLY mappings in a single hibernate.cfg.xml...That way, the mappings get updated with a regular CVS refresh, and I don't have to maintain them in five places.

Someone told me that Hibernate was "moving away" from using properties files...Can someone offer a better solution than mine? I'd be surprised if I'm the only one out there with this issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 8:44 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
you can have more *cfg.xml files, but You have to change HibernateUtil (or your class for session building)

see this thread http://forum.hibernate.org/viewtopic.php?t=947911 (for performance, but use 2 xml files)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 9:08 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
That's pretty cool, I may try to implement that.

Can anyone tell me why properties files are being moved away from?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 9:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what people should avoid is having too much in a /hibernate.properties file if they want multiple configurations since /hibernate.properties is *always* read if it exists.

Using arbitrary mix of property and cfg.xml files is all ok.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 1:19 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
But why is Hibernate "moving away" from properties files?

Since the mappings will ALWAYS be the same for the same application on different environments, it seems wise to keep them separate from database connection properties (etc) that will often be different for each environment.

ie, the mappings on my dev environment will be the same on the test and production server, but all the other stuff will change.

I'm surprised people aren't bothered by keeping so much identical stuff in more than one place.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 1:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh ? as someone already wrote - you can call configure more than once so just keep the parts separate that you want separate.

And I expliciltly wrote that both properties and cfg.xml are all ok so why do keep saying we are moving away from it ? ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 1:45 pm 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Sounds like my solution is fine...Probably better to just use two Configurations...Cool, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 10:48 am 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
I'm having some trouble getting this working...If the mappings aren't in hibernate.cfg.xml, it doesn't seem to find them even after I load the separate mapping file. I don't know if I'm doing this the right way, but I want to get this figured out. Can anyone tell me why the mappings in mappings.cfg.xml aren't really being included in the session factory? When I try to run a simple query I get unexpected token errors that aren't there when I put the mappings in hibernate.cfg.xml.


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

<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory name="java:comp/env/hibernate/SessionFactory">

        <!-- properties -->
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.61:1521:DB</property>
        <property name="hibernate.connection.username">login</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">false</property>
        <property name="use_outer_join">true</property>
      
        <mapping resource="_Dummy.hbm.xml"/>

        </session-factory>

</hibernate-configuration>


It balks if there's no dummy mapping...Any way to avoid that?


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

<hibernate-configuration>

   <!-- a SessionFactory instance listed as /jndi/name -->
   <session-factory name="java:comp/env/hibernate/SessionFactory">

      <!-- mapping files -->
      <mapping resource="Content.hbm.xml"/>
      ...
      <mapping resource="User.hbm.xml"/>

   </session-factory>

</hibernate-configuration>



Here's the session initialization from the servlet listener class:
Code:
    public SessionFactory createSessionFactory() throws Exception {
        SessionFactory sessions = null;
        Configuration cfg = null;
       
        try {
            log.info("Loading hibernate.cfg.xml from classpath.");
            cfg = new Configuration().configure();
         
            log.info( "Adding mappings to Hibernate configuration..." );
            mappings = getClass().getClassLoader().getResourceAsStream( "mappings.cfg.xml" );
            cfg.addInputStream( mappings );

            sessions = cfg.buildSessionFactory();
        } catch (Exception e) {
            log.error("Could not initialize webapp:", e);
        }

        return sessions;
    }



Do I need to do my own parsing like in the thread posting above? I'm kinda boggled by all this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:05 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Code:
cfg = new Configuration().configure();
         
            log.info( "Adding mappings to Hibernate configuration..." );
            mappings = getClass().getClassLoader().getResourceAsStream( "mappings.cfg.xml" );
            cfg.addInputStream( mappings );

            sessions = cfg.buildSessionFactory();


change to

Code:
cfg = new Configuration();
         
            log.info( "Adding mappings to Hibernate configuration..." );
            mappings = getClass().getClassLoader().getResourceAsStream( "mappings.cfg.xml" );
            cfg.addInputStream( mappings );
             cfg.configure();
            sessions = cfg.buildSessionFactory();


cfg = new Configuration();

will load standard *cfg.xml or properties if you have one


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:13 am 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Is this true in 2.x? After adding cfg.configure() after adding the input stream, I'm still getting the same issue (bad token in "from User as user", etc)...I'm gonna see if the docs can give me a better understanding of what question to ask...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
could it be your underlying db does not like User as a unquoted tablename/keyword ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:20 am 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
Heh, well, that was just a dummy query. The query that's bonking works when I put the mappings in hibernate.cfg.xml, but not when I put them in mappings.cfg.xml and try to load both files...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
would be good if you started by showing a stacktrace and more info rather than guessing it has something to do with the order of cfg.xml adding ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:48 am 
Beginner
Beginner

Joined: Fri Jul 15, 2005 12:26 pm
Posts: 37
This is what I'm seeing from the first query that's run:

net.sf.hibernate.QueryException: unexpected token: as [from SystemInfo as systeminfo]
at net.sf.hibernate.hql.FromParser.token(FromParser.java:94)
at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:120)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:146)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:133)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:352)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:330)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1368)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1332)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1314)


I just don't think it's finding the SystemInfo mapping...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no i would expect "SystemInfo is not mapped" if that was the case...but that is H3.

you can see in the logs what classes it has mapped.

you can also call cfg.getClassMapping(fqcn) to see if it knows about it.

/max

_________________
Max
Don't forget to rate


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