Hi all,
been struggling with this for a few weeks now. Using Hibernate 4.3.11 and Spring 4.2.4.
I have some Entities with a primary key of java.util.UUID and a postgres database by default.
This works fine with
Code:
@org.hibernate.annotations.Type(type="pg-uuid")
.
However my unit/integration tests initialise a test context with a H2 db, because, testing, which means that the registered type is now incorrect.
So, what I'd like to be able to do is to configure the Types registry
https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch06.html#types-registry using postgres under "default" profile and use "h2" profile for testing.
The problem I'm facing is that our configuration is using spring-orm's HibernateJpaVendorAdapter into a LocalContainerEntityManagerFactoryBean which doesn't appear to allow the underlying configuration to be modified or instantiated via spring.
Does anyone know how I can register the org.hibernate.type.PostgresUUIDType using this configuration?
Code:
@Entity
@Table(name = "models")
public class ModelEntity {
@Id
@org.hibernate.annotations.Type(type="pg-uuid") // doesn't work under h2
private UUID modelId;
@Basic
private String name;
}
Code:
@Configuration
public class RealConfig {
@Value("${driverClass}")
private String driverClass;
@Value("${jdbcURL}")
private String jdbcURL;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Bean
@Profile("default")
public DataSource dataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(driverClass);
basicDataSource.setUrl(jdbcURL);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
@Bean
@Profile("default")
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL9Dialect");
return hibernateJpaVendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource);
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
localContainerEntityManagerFactoryBean.setPackagesToScan("my.entities");
return localContainerEntityManagerFactoryBean;
}
}
Code:
@Configuration
@Profile("h2")
public class TestDBConfig {
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setDatabase(Database.H2);
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setGenerateDdl(true);
return hibernateJpaVendorAdapter;
}
@Bean
public Properties hibernateProperties() {
final Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.connection.driver_class", "org.h2.Driver");
properties.put("hibernate.hbm2ddl.auto", "create-drop");
return properties;
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}
}