Skip to content

Instantly share code, notes, and snippets.

@jjbcarreno
Created November 28, 2013 05:19
Show Gist options
  • Save jjbcarreno/7687596 to your computer and use it in GitHub Desktop.
Save jjbcarreno/7687596 to your computer and use it in GitHub Desktop.
Spring JPA Schema export without persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="your.package.goes.there" />
<jpa:repositories base-package="your.package.goes.there" />
<jdbc:embedded-database id="dataSource" type="HSQL" />
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:data.sql" />
</jdbc:initialize-database>
<bean id="entityManagerFactoryExtTest"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="your.package.goes.there" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="jpaSpringSchemaExport" class="test.JpaSpringSchemaExport">
<property name="localContainerEntityManagerFactoryBean" ref="&amp;entityManagerFactoryExtTest" />
<property name="create" value="true" />
<!-- <property name="destination" value="schema-export.sql" /> -->
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryExtTest" />
</bean>
<tx:annotation-driven />
</beans>
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-test.xml" })
public class ExportSchemaTest {
@Autowired
private JpaSpringSchemaExport jpaSpringSchemaExport;
@Test
public void testExportSchema() {
jpaSpringSchemaExport.export();
}
}
package test;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
/**
*
* @author Jose Carreno
*
*/
@SuppressWarnings("deprecation")
public class JpaSpringSchemaExport {
private LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean;
private String destination;
private boolean create = true;
private boolean format = true;
public void export() {
org.hibernate.ejb.Ejb3Configuration cfg = new org.hibernate.ejb.Ejb3Configuration();
org.hibernate.ejb.Ejb3Configuration configured = cfg.configure(
localContainerEntityManagerFactoryBean.getPersistenceUnitInfo(),
localContainerEntityManagerFactoryBean.getJpaPropertyMap());
org.hibernate.tool.hbm2ddl.SchemaExport schemaExport = new org.hibernate.tool.hbm2ddl.SchemaExport(
configured.getHibernateConfiguration());
if(getDestination() == null) {
setDestination(getClass().getResource("/").getFile() + "schema-export.sql");
}
schemaExport.setOutputFile(getDestination());
schemaExport.setFormat(isFormat());
schemaExport.execute(true, false, false, isCreate());
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public boolean isCreate() {
return create;
}
public void setCreate(boolean create) {
this.create = create;
}
public boolean isFormat() {
return format;
}
public void setFormat(boolean format) {
this.format = format;
}
public LocalContainerEntityManagerFactoryBean getLocalContainerEntityManagerFactoryBean() {
return localContainerEntityManagerFactoryBean;
}
public void setLocalContainerEntityManagerFactoryBean(
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) {
this.localContainerEntityManagerFactoryBean = localContainerEntityManagerFactoryBean;
}
}
@jasjisdo
Copy link

Great Work!
Do you know a solution to do this with Hibernate 4.3.x ?
In Hibernate 4.3.x didn't exist the class 'org.hibernate.ejb.Ejb3Configuration', only the class 'org/hibernate/cfg/Configuration.java' is available for Configuration.

@fischermatte
Copy link

Yes, nice example. Also looking for a way to do this with hibernate 4.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment