I have my spring data JPA configuration up and running. I'd like to get a ddl sql file from my annotated entities.
I know this has been asked many times. But I could not find something usefull explaing how to do this with java based spring configuration.
This is my JPA set-up:
@Bean(name = BEAN_ENTITY_MANAGER_FACTORY)
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = createLocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaVendorAdapter(jpaVendorAdapter());
em.setPersistenceUnitName(getPersistenceUnitName());
em.setPackagesToScan(getPackagesToScan());
processOptionalProperty(Environment.HBM2DDL_AUTO, em);
processOptionalProperty(HIBERNATE_JDBC_BATCH_SIZE, em);
processOptionalProperty(EHCACHE_CONFIGURATIONRESOURCENAME_PROPERTY, em);
em.afterPropertiesSet();
return em.getObject();
}
@Bean(name = BEAN_HIBERNATE_JPA_VENDOR_ADAPTER)
public HibernateJpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(Boolean.parseBoolean(getPropertyValue(HIBERNATE_SHOW_SQL_PROPERTY)));
adapter.setDatabasePlatform(getPropertyValue(HIBERNATE_DATABASE_PLATFORM_PROPERTY));
adapter.setGenerateDdl(true);
return adapter;
}
The hbm2ddl is set like this:
hibernate.hbm2ddl.auto=create
This configuration creates the necessary database tables at application start-up. How can I get an sql script containing all the ddl commands to set-up the DB?
After a lot of googling and try and error this is the solution I found.
To generate a ddl script you need the maven hibernate3:hbm2ddl plugin . To get the plugin I added this to my pom.xml
:
<plugin>
<!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>jpaconfiguration</implementation>
</component>
</components>
<componentProperties>
<persistenceunit>eai</persistenceunit>
<outputfilename>ddl-file.sql</outputfilename>
<drop>false</drop>
<create>true</create>
<export>false</export>
<format>true</format>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
</dependencies>
</plugin>
The nasty bit to find was the the dependency section. Thanks to this blog post to point this out.
The second thing I got stuck was the persistence unit name. This is configured by placing a file called persistence.xml
into your META-INF
directory.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="eai">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<!-- other properties ... -->
</properties>
</persistence-unit>
</persistence>
This file also contains the hibernate dialect. This is important to get the sql that makes your db happy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.