Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: How to select from a list of db's at run-time with MySQL?
PostPosted: Fri Nov 04, 2005 12:37 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
Hibernate version:
3.0.5

Hi,

I’m working on a swing client application that must use give the user the option to create a new or use an existing database each time it runs. So I must be able to dynamically (at run-time) get a list of all of the existing (previously created by the user) databases or create a new one, with a name given by the user.

I’m using MySQL 4.1 and I have my db connection info, etc in hibernate.config.xml.

How can I do the following things:

1. Get a list of all the existing databases. Is there an HQL similar to SHOW DATABASES that will return a list of databases?

2. programmatically override or add to my hibernate.config.xml that is in my applications .jar file to tell hibernate to use a particular dbase. I suppose I should use something like:

Configuration().setProperty("hibernate.connection.datasource", "dbase name")

Is is possible to override just one property in the hibernate.config.xml and keep the rest?

Thanks for your help,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 1:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
1. No - that is some database specific operation

2. Yes

3. Yes

...and why didn't you just try this before asking ? :)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: My attempts at this are not working
PostPosted: Mon Nov 07, 2005 4:45 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
Thanks for your help, but my attempts at this are not working.

I have a helper class to instantiate a global SessionFactory and return Session instances . The SessionFactory is created by the JVM when the class is loaded so I'm having a hard time understanding how to configure the database to use at run time. What code do I need, and where? My helper class code is below:

thanks,

Steve

public class HibernateHelper
{

// calling this doesn't work to change the Connection URL
public static void setConnectionURL(String dbName)
{
connectURL = dbName;
}

private HibernateHelper()
{

}

public static Session getSession()
{

Session session = (Session) HibernateHelper.session.get();
if (session == null)
{
session = sessionFactory.openSession();
HibernateHelper.session.set(session);
}
return session;
}

private static String connectURL = "jdbc:mysql://localhost/etsiu_data";
private static final ThreadLocal session = new ThreadLocal();
private static final ThreadLocal transaction = new ThreadLocal();

private static final SessionFactory sessionFactory = new Configuration()
.setProperty("hibernate.connection.url", connectURL)
.configure()
.buildSessionFactory();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 4:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
if you want it to be configured later, then you need to of course need to create the sessionfactory at the time when you got all information to get it.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 5:32 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
OK. My understanding of this is that my helper class is no use as it is currently written since it is static and I need to create my SessionFactory only after I have some configuration data from the user.

I'll try re-write my helper as a non-static class. Hopefully I won't be bugging you anymore about this one :)

thanks again for your help,

Steve


Top
 Profile  
 
 Post subject: Re: How to select from a list of db's at run-time with MySQL?
PostPosted: Fri Dec 02, 2011 3:30 am 
Newbie

Joined: Wed Jun 18, 2008 7:30 am
Posts: 3
Location: Delhi
Hello,

I have worked in hibernate for one database only.
I am working on a project, where the database name will be taken as input from user. The user will then be allowed to work with that database.

I learnt from various sites, how to create configuration at runtime...
Changing the dbsource property of the configuration...

One thing that is bothering me....changing the hibernate configuration will impact all the users. While my requirement is
user - database one to one mapping...

Please advise on this...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.