-->
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: Override hibernate.connection.url
PostPosted: Mon Sep 28, 2009 2:43 am 
Newbie

Joined: Mon Sep 21, 2009 2:57 am
Posts: 2
Hi community,
I have a question that concerns the url to the database. Usually it is configured in hibernate.cfg.xml and I need it there because it is used by the hibernate tools building the database schema.

When my application creates a hibernate session I want to get the connection url from another source. I tried to set the property directly after the Configuration object is created:

Configuration config = new AnnotationConfiguration();
config.setProperty("hibernate.connection.url", myUrl);

But this works only if the url in hibernate.cfg.xml is not present. If the url is already there the Url cannot be overwrtten with myUrl.

Has anybody an idea?

Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Override hibernate.connection.url
PostPosted: Mon Sep 28, 2009 3:42 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Hi

Try something like this

Code:
Configuration cfg = new Configuration();
cfg.configure();
System.setProperty("hibernate.connection.password",pass);
System.setProperty("hibernate.connection.username",usr);
System.setProperty("hibernate.connection.driver_class", driver_class);
System.setProperty("hibernate.connection.url", driver_url);
System.setProperty("hibernate.dialect", dialect);
// etc, etc, for all properties
cfg.setProperties(System.getProperties());
sessionFactory = cfg.buildSessionFactory();


Top
 Profile  
 
 Post subject: Re: Override hibernate.connection.url
PostPosted: Tue Sep 29, 2009 2:45 am 
Newbie

Joined: Mon Sep 21, 2009 2:57 am
Posts: 2
Thank you for your help. This works fine.


Top
 Profile  
 
 Post subject: Re: Override hibernate.connection.url
PostPosted: Tue Sep 29, 2009 5:20 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
Hello!

I have a simliar problem!
But I can not change a connection to a different
db schema over the configuration.

Code:
protected static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           SessionFactory sf = null;
           System.out.println("DBHandler.url " + DBHandler.url);
           
           Configuration cfg = new Configuration();
           
           
              
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.116/fischer_test")){
              
              System.setProperty("hibernate.connection.password", "sadrach");
              System.setProperty("hibernate.connection.username", "sadrach");
              System.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
              System.setProperty("hibernate.connection.url", "jdbc:mysql://10.100.1.116/fischer_test");
              System.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
              System.setProperty("hibernate.current_session_context_class", "thread");
              System.setProperty("hibernate.bytecode.use_reflection_optimizer", "false");
              cfg.setProperties(System.getProperties());            
              System.out.println("conf");
              cfg.configure("/fischer_test.hibernate.cfg.xml");
              cfg.buildSettings();
           }
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }


Top
 Profile  
 
 Post subject: Re: Override hibernate.connection.url
PostPosted: Tue Sep 29, 2009 10:15 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
Hi donbar

i guess calling

Code:
cfg.configure("/fischer_test.hibernate.cfg.xml");

will override the configuration which you set in your code earlier. So call this method before setting properties check if this works.

try something like this

Code:
protected static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           SessionFactory sf = null;
           System.out.println("DBHandler.url " + DBHandler.url);
           
           Configuration cfg = new Configuration();
           
           
             
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.116/fischer_test")){
                cfg.configure("/fischer_test.hibernate.cfg.xml");
              System.setProperty("hibernate.connection.password", "sadrach");
              System.setProperty("hibernate.connection.username", "sadrach");
              System.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
              System.setProperty("hibernate.connection.url", "jdbc:mysql://10.100.1.116/fischer_test");
              System.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
              System.setProperty("hibernate.current_session_context_class", "thread");
              System.setProperty("hibernate.bytecode.use_reflection_optimizer", "false");
              cfg.setProperties(System.getProperties());           
              System.out.println("conf");
           
              cfg.buildSettings();
           }
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }



Top
 Profile  
 
 Post subject: Re: Override hibernate.connection.url
PostPosted: Tue Sep 29, 2009 12:28 pm 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
Thank you! But I tried 2 ways to change the properties.
The content in /fischer_test.hibernate.cfg.xml is the same!


1st WAY:
Code:
protected static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           SessionFactory sf = null;
           System.out.println("DBHandler.url " + DBHandler.url);
           
           Configuration cfg = new Configuration();
           
           
             
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.116/fischer_test")){
              cfg.configure("/fischer_test.hibernate.cfg.xml");
                 
              System.out.println("conf");
           
              cfg.buildSettings();
           }
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }



2nd WAY:
Code:
protected static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           SessionFactory sf = null;
           System.out.println("DBHandler.url " + DBHandler.url);
           
           Configuration cfg = new Configuration();
           
           
             
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.116/fischer_test")){
              System.setProperty("hibernate.connection.password", "sadrach");
              System.setProperty("hibernate.connection.username", "sadrach");
              System.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
              System.setProperty("hibernate.connection.url", "jdbc:mysql://10.100.1.116/fischer_test");
              System.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
              System.setProperty("hibernate.current_session_context_class", "thread");
              System.setProperty("hibernate.bytecode.use_reflection_optimizer", "false");
              cfg.setProperties(System.getProperties());           
              System.out.println("conf");
           
              cfg.buildSettings();
           }
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }



BUT NOTHING WORKS! ANY IDEA?


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.