Announcement Announcement Module
Collapse
No announcement yet.
changing hibernate schemas at runtime Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


        • #5
          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

          Comment


          • #6
            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!

            Comment


            • #7
              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).

              Comment


              • #8
                Call DynamicSchemaHibernateConfiguration

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


                Thanks.

                Comment


                • #9
                  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.

                  Comment

                  Working...
                  X