-->
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.  [ 1 post ] 
Author Message
 Post subject: change database at runtime use hibernate and spring
PostPosted: Sat Apr 04, 2009 12:09 am 
Newbie

Joined: Tue Jun 10, 2008 4:40 am
Posts: 13
how to change database at runtime, i use hibernate and spring.
i have make like this

hibernate.cfg.xml
Code:
Help with Code Tags
(Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <mapping class="com.model.Person" />
        <mapping class="com.model.Group" />
       
    </session-factory>
</hibernate-configuration>


spring.ctx.xml
Code:
Help with Code Tags
(Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
    <context:property-placeholder location="classpath:jdbc.properties">
    <context:component-scan base-package="com.hbtemplate" />
    <tx:annotation-driven transaction-manager="txManager" />
   
   <bean id="dataSource"
   class="org.springframework.jdbc.datasource.DriverManagerDataSource"
   p:driverClassName="${hibernate.connection.driver_class}"
   p:url="${hibernate.connection.url}"
   p:username="${hibernate.connection.username}"
   p:password="${hibernate.connection.password}" />

   <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
   p:dataSource-ref="dataSource">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.generate_statistics">
                    ${hibernate.show_statistics}
                </prop>
               
                <prop key="hibernate.hbm2ddl.auto">
               ${hibernate.hbm2ddl_auto}
            </prop>
            </props>
        </property>
    </bean>
   
       
    <bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" />
             
            </beans>

file hibernate.cfg.xml and file spring.ctx.xml i make in same folder

MainFrame.java
Code:
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
               
            String PROP_FILE = "jdbc.properties";
            Properties p = new Properties();
         
            p.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
            p.setProperty("hibernate.connection.url","jdbc:mysql://localhost/changeAtRuntime");
            p.setProperty("hibernate.connection.username","root");
            p.setProperty("hibernate.connection.password","");
            p.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLInnoDBDialect");
            p.setProperty("hibernate.hbm2ddl_auto","create");
            p.setProperty("hibernate.show_sql","true");
            p.setProperty("hibernate.show_statistics","true");

            //save properties
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(PROP_FILE);
                   
        } catch (FileNotFoundException ex) {
                  ex.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
               
            }
        }

        //load file properties
        p = new Properties();
            try {
            FileInputStream in = new FileInputStream(PROP_FILE);
            p.load(in);
            in.close();

        } catch (IOException i) {
                i.printStackTrace();
        }


        applicationContext = new ClassPathXmlApplicationContext("spring.ctx.xml");

        }
        });
    }



got error
Code:
Help with Code Tags
(Toggle Plain Text)
INFO: Loading properties file from class path resource [jdbc.properties]
Mar 30, 2009 1:28:29 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e0cc23: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,groupDaoHibernate,personDaoHibernate,userService,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,dataSource,sessionFactory,txManager]; root of factory hierarchy
Exception in thread "AWT-EventQueue-0" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist

at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:541)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:515)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)

Thanks for your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.