Results 1 to 9 of 9

Thread: changing hibernate schemas at runtime

  1. #1
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default changing hibernate schemas at runtime

    In our environment, we have three logical schemas in one database. Since all of these schemas are accessed from the same session factory and data source, we need to include the schema attribute in our hibernate mapping files. We would like to be able to create new sandbox environments by just adding new schemas in the same database. The problem, however, is that the schema names are already hard coded in the hibernate mapping files. Is it possible to change the values of the schema attributes at runtime?

    Thanks.

    -karl
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  2. #2
    Join Date
    Jul 2005
    Location
    Maryland
    Posts
    40

    Default

    1. Yes, it is possible. You can do almost anything but you start to get a pretty ugly codebase.

    2. You said you use one datasource/sessionFactory. Are you allowed to have more?

    3. Have you looked into Hib3's entityName concept?


    Number 1 and 3 are both options but they'll probably require some duplicated code or some ugly code. It seems that if 2 will be allowed that it would be a cleaner idea.

  3. #3
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    1. Yes, it is possible. You can do almost anything but you start to get a pretty ugly codebase.
    I got some great help on this one from the hibernate forums. I don't think the code will be that ugly when I can abstract it behind a spring FactoryBean.

    2. You said you use one datasource/sessionFactory. Are you allowed to have more?
    Only one datasource per database.

    3. Have you looked into Hib3's entityName concept?
    I have not looked into this as of yet, but I'll take a look.

    Thanks.

    -karl
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  4. #4

    Default changing the schema during transaction

    Hi,
    I am still not clear how this is accomplished. Is there a way to do this in a cleaner way in spring2.0 + Hibernate?

    Thanks
    Matt
    Matt 'M

  5. #5
    Join Date
    Oct 2007
    Posts
    1

    Default

    For others that are interested in dynamically changing hibernate domain object schemas at runtime, here is my solution ... This works with spring 2.0.3 and hibernate 3.2 with annotations. I ended up writing a subclass of hibernate's AnnotationConfiguration to do this. To use it you have to override the session factory's configuration class in your applicationContext.xml and set the hibernate schema override, like this:

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    
    <property name="configurationClass" value="my.package.DynamicSchemaHibernateConfiguration"/> <property name="hibernateProperties"> <props> <prop key="hibernate.schema_override">JBPM_SCHEMA</prop> <prop key="hibernate.schema_override_classes">org.jbpm.*</prop> </props> </property>
    ... </bean>
    I've attached the java source code for the DynamicSchemaHibernateConfiguration .

    Enjoy.
    -Greg
    Attached Files Attached Files

  6. #6
    Join Date
    Sep 2008
    Posts
    2

    Default

    Hi gageorge,

    It's been a long time since you posted your source code. I'm trying to do something similar in my app. I want to change the table columns inside my database during run-time, so I'm studying how you implemented your solution for your problem.

    When you call the DynamicSchemaHibernateConfiguration class from inside your code, what do you do with the SessionFactory object that gets returned when you call the buildSessionFactory() method. I understand that the class DynamicSchemaHibernateConfiguration is a bean, so I'm a bit confused. Do you just discard the object?

    I should mention that I'm learning Spring and Hibernate so i'm a newbie.

    Thanks!

  7. #7
    Join Date
    Feb 2007
    Posts
    4

    Default Additional support for SequenceGenerators

    The one problem with this configuration is that it only handles tables, not sequences. I've attached an addition that will handle correcting sequences as well.

    Add this line to the constructor of DynamicSchemaHibernateConfiguration.java
    Code:
    dynamicIdentifierGeneratorFactory = new DynamicIdentifierGeneratorFactory(this);
    Add the following code to the DynamicSchemaHibernateConfiguration.java class to incorporate it:
    Code:
        public DefaultIdentifierGeneratorFactory getIdentifierGeneratorFactory()
        {
            return dynamicIdentifierGeneratorFactory;
        }
    This will only work with Hibernate 3.5 or newer (which adds schema support to sequences).
    Attached Files Attached Files

  8. #8
    Join Date
    Nov 2009
    Posts
    1

    Default Call DynamicSchemaHibernateConfiguration

    Hi,
    Only one question, how i call the DynamicSchemaHibernateConfiguration from my DAO?


    Thanks.

  9. #9
    Join Date
    Feb 2007
    Posts
    4

    Default

    You wouldn't call this from a DAO...this is only a configuration object for your sessionFactory bean. Your DAO would just be requesting sessions as appropriate.

Similar Threads

  1. Replies: 4
    Last Post: May 23rd, 2008, 11:08 AM
  2. Replies: 1
    Last Post: Sep 8th, 2005, 09:24 AM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •