I'm using hibernate annotations and i want to export my database schema.
Similar to the schemaexporttask with hbm xml files.
I'm using hibernate annotations and i want to export my database schema.
Similar to the schemaexporttask with hbm xml files.
In case someone is interested how to do this with JPA+Spring from a unit test (you can generate the sql running the unit test from inside Eclipse like a breeze):
ExportDatabaseSchema.java:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(defaultRollback=true)
public class ExportDatabaseSchema {
@Resource(name="&entityManagerFactory")
private LocalContainerEntityManagerFactoryBean entityManagerFactory;
@Test
public void exportDatabaseSchema(){
PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
Map jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();
Configuration configuration = new Ejb3Configuration().configure( persistenceUnitInfo, jpaPropertyMap ).getHibernateConfiguration();
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("resources/sql/schema.sql");
schema.create(false, false);
}
}
You need an ExportDatabaseSchema-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:applicationContext-jpa.xml" />
</beans>
The applicationContext-jpa.xml contains the annotation configured entityManagerFactory bean. The trick is to inject it with & like this: "&entityManagerFactory", to dereference the spring generated proxy.