How to get Hibernate configuration after creating EntityManagerFactory?

My web framework (Play 1.2.5) creates a local EJB3Configuration for the method that it uses to create the EntityManagerFactory (source) . I am working on a script and would like to run the generateSchemaUpdateScript() method from the configuration in order to make SQL scripts that I can test and run on production deployments. The problem I am facing is that I cannot figure out how to access the Configuration object that was used or how to generate the Configuration object after the EntityManagerFactory was created.

+6
source share
2 answers

You cannot return a configuration object from an entitymanagerfactory object because the hibernate implementation of EntityManagerFactoryImpl does not contain a reference to the configuration object

Your choice

  • duplicates code from JPAPlugin in script to create your own configuration object
  • set up hibernate tools to work with your classes. I have never used this tool myself, but I assume that a properly configured one can generate ddl for you.
  • generate back ddl script from your database
+7
source

After the Entity factory manager has been created, you should not have a handle to the Configuration object, in part because the configuration must exhibit unchanged behavior because you cannot change its properties.

I mean the source of Hibernate 3.6.8, and the EJB3Configuration class has a method

 public AnnotationConfiguration getHibernateConfiguration() { //TODO make it really read only (maybe through proxying) return cfg; } 

AnnotationConfiguration is declared deprecated as all of its functions are moved to the configuration class.

Therefore, I think that with this you can get a handle to your configuration after creating it. You should be very careful, but should not change anything in this configuration.

This is explained in javadocs here

After calling #buildEntityManagerFactory () you can no longer change the configuration state (without adding a class, without changing a property, etc.)

Having said that, you are trying to ensure that it is STRICTLY NOT WARNED , especially in the production database. See here

Hibernate has a property called "hibernate.hbm2ddl.auto", which should help generate an automatic circuit in case of script development. What you are trying to achieve programmatically has the same effect that gives it the update value, as shown below in your xml persistence. In fact, under covers, the generateSchemaUpdateScript method is called by the configuration when you have the value "hibernate.hbm2ddl.auto" set to "update"

 <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="xyz" transaction-type="RESOURCE_LOCAL"> <properties> .................... <property name="hibernate.hbm2ddl.auto" value="update"/> ...................... </properties> </persistence-unit> </persistence> 

You can refer to other possible values ​​for this attribute here.

However, Hibernate docs strongly advises against using this against a production database.

In the final book on sleep, "Java persistence with Hibernate" warns about this

WARNING We have seen Hibernate users trying to use SchemaUpdate to automatically update a production database schema. This can quickly end in disaster and will not be allowed by your database administrator.

as well as several restrictions on the update scheme process

An additional option for this configuration property, updates, may be useful during development: it allows the built-in SchemaUpdate tool, which can facilitate the evolution of the circuit. If enabled, Hibernate reads the JDBC database metadata at startup and creates new tables and constraints by comparing the old schema with the current metadata mapping. Please note that this functionality depends on the quality of the metadata provided by the JDBC driver, an area in which many drivers are missing. In practice, this function is therefore less interesting and useful than it seems.

UPDATE 1: If you do not have access to EJB3Configuration and your ultimate goal is to create update schema scripts for annotated JPA objects, then you can programmatically create EJB3Configuration in the main java class using xml persistence that contains your database configuration information and runs schema export tool. See this example of how you can do this. This way you can run the tool for JPA objects outside your infrastructure or even container.

UPDATE 2

The following code example shows how you can use the Hibernate schema update when creating database migrations for playback applications. Looks like this is what you wanted!

+2
source

Source: https://habr.com/ru/post/924862/


All Articles