Skip to content

Instantly share code, notes, and snippets.

@jbrackett
Created February 9, 2014 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbrackett/8899299 to your computer and use it in GitHub Desktop.
Save jbrackett/8899299 to your computer and use it in GitHub Desktop.
Load in memory database and generate schema from annotated Java files.
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.cfg.Environment;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableJpaRepositories(basePackages = { "com.example.repository" })
@ComponentScan(basePackages = { "com.example.domain",
"com.example.repository" })
@EnableTransactionManagement
public class TestAppConfig {
public static final String DB_NAME = "testdb";
@Bean(destroyMethod = "shutdown")
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).setName(DB_NAME)
.build();
}
@Bean(destroyMethod = "close")
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setPersistenceUnitName(DB_NAME);
factory.setPackagesToScan("com.example.domain");
factory.setJpaVendorAdapter(jpaAdapter());
factory.setJpaProperties(jpaProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public JpaVendorAdapter jpaAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.HSQL);
return adapter;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
@Bean
public HibernateExceptionTranslator exceptionTranslator() {
return new HibernateExceptionTranslator();
}
public Properties jpaProperties() {
Properties jpaProperties = new Properties();
jpaProperties.put(Environment.HBM2DDL_AUTO, "create");
jpaProperties.put(Environment.HBM2DDL_IMPORT_FILES, "data.sql");
jpaProperties.put("javax.persistence.schema-generation.create-database-schemas", "true");
jpaProperties.put("javax.persistence.schema-generation.scripts.action", "create");
jpaProperties.put("javax.persistence.schema-generation.scripts.create-target",
"src/test/resources/schema.sql");
jpaProperties.put("javax.persistence.database-product-name", "HSQL");
jpaProperties.put("jadira.usertype.autoRegisterUserTypes", "true");
jpaProperties.put("jadira.usertype.databaseZone", "jvm");
jpaProperties.put("jadira.usertype.javaZone", "jvm");
return jpaProperties;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment