Announcement Announcement Module
Collapse
No announcement yet.
Obtaining Hibernate configuration via EntityManagerFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Obtaining Hibernate configuration via EntityManagerFactory

    Hello,

    I want to use hibernate's SchmaExport class in my application but everything I have is a EntityManagerFactory instance. To initialize a SchmaExport instance I need the underlaying hibernate configuration. Is there a way to get it? JPA is configured with LocalContainerEntityManagerFactoryBean.


    Best

    Oliver

  • #2
    You can cast the entity manager to a session and then get the SessionFactory from it.

    Comment


    • #3
      Originally posted by Costin Leau View Post
      You can cast the entity manager to a session and then get the SessionFactory from it.
      Yes, but how to obtain the HibernateConfiguration from the SessionFactory?

      (BTW: You can also cast EntityManagerFactory to HibernateEntityManagerFactory and obtain the SessionFactory from this)

      Comment


      • #4
        Cast it to SessionFactoryImpl and then get the settings from there (you can get the Configuration object even through reflection). It's ugly, I know but it will work.

        Comment


        • #5
          Originally posted by Costin Leau View Post
          Cast it to SessionFactoryImpl and then get the settings from there (you can get the Configuration object even through reflection). It's ugly, I know but it will work.
          SessionFactoryImpl has no getter for the stored Configuration object, so reflection seem to be the only way to get it (but then, there's no need for casting it to SessionFactoryImpl, right?).

          I found another solution -

          I hooked the postProcessEntityManagerFactory() method of LocalContainerEntityManagerFactoryBean and store the PersistenceUnitInfo. Then I can get the configuration with

          Code:
          Configuration conf = new Ejb3Configuration().configure(persistenceUnitInfo, null).getHibernateConfiguration();
          Anything bad with this?

          Comment


          • #6
            No, actually that's a very nice way of getting hold of the Configuration object (as long as your configure JPA through Spring).

            Comment


            • #7
              Hi,

              I know this thread is very old, but actually I have exactly the some problem. By now I was happy with the solution mentioned above, but Ejb3Configuration was removed in Hibernate 4.3 and I didn't find an alternative. So does anybody know an elegant solution to obtain the Hibernate Configuration object in version 4.3 or higher? Rolling back to hibernate 4.2 is unfortunately not a solution because I need some JPA 2.1 features.


              BR,
              Patrick

              Comment


              • #8
                Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
                We need to clean this forum (old unanswered question) before close it.

                Thanks for understanding

                Comment

                Working...
                X