I'm trying to use springboot, hsql and hibeate together to persist and retrieve some fairly boring data. The issue I'm ruing into is that hibeate seems unable to reference my tables correctly, throwing the following exception:
ERROR [main] (SpringApplication.java:826) - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'strangerEntityManagerFactory' defined in class path resource [com/healz/stranger/config/profiles/GenericSqlConfig.class]: Invocation of init method failed; nested exception is org.hibeate.HibeateException: Missing column: user_USER_ID in PUBLIC.STRANGER.PROTECTED_PROPERTIES
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
...
Initially I was using HSQL's default schema name, PUBLIC, and noticed that the exception getting thrown was that the application couldn't find PUBLIC.PUBLIC.PROTECTED_PROPERTIES. This looks highly suspicious -- why is there an "extra layer" of PUBLIC here? It definitely doesn't look right. The code that does the EntityManagerFactory setup looks like this:
@Log4j
@Configuration
@EnableAspectJAutoProxy
@ComponentScan (basePackages = {"com.healz.stranger.data"})
@EnableJpaRepositories (
entityManagerFactoryRef="strangerEntityManagerFactory",
transactionManagerRef="txManager",
basePackages={"com.healz.stranger.data.model"}
)
@EntityScan (basePackages={
"com.healz.stranger.data.model"
})
@Import ( {HsqlConfig.class, DevMySqlConfig.class, ProdMySqlConfig.class} )
public class GenericSqlConfig {
@Configuration
@EnableTransactionManagement(order = Ordered.HIGHEST_PRECEDENCE)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
protected static class TransactionManagementConfigurer {
// ignore aoying bean auto-proxy failure messages
}
@Bean
public static PersistenceAotationBeanPostProcessor persistenceAotationBeanPostProcessor() throws Exception {
retu new PersistenceAotationBeanPostProcessor();
}
@Bean
public JpaDialect jpaDialect() {
retu new HibeateJpaDialect();
}
@Autowired
@Qualifier("hibeateProperties")
private Properties hibeateProperties;
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Bean (name="strangerEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean strangerEntityManagerFactory(
final @Qualifier("hibeateProperties") Properties props,
final JpaDialect jpaDialect) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource);
emf.setPackagesToScan("com.healz.stranger.data");
JpaVendorAdapter vendorAdapter = new HibeateJpaVendorAdapter();
emf.setJpaVendorAdapter(vendorAdapter);
emf.setJpaProperties(hibeateProperties);
emf.setJpaDialect(jpaDialect);
emf.setPersistenceUnitName("strangerEntityManagerFactory");
retu emf;
}
@Bean (name="sessionFactory")
public SessionFactory configureSessionFactory(LocalContainerEntityManagerFactoryBean emf) {
SessionFactory sessionFactory = emf.getObject().unwrap(SessionFactory.class);
retu sessionFactory;
}
/**
* Helper method to get properties from a path.
* @param path
* @retu
*/
@SneakyThrows (IOException.class)
public static Properties getHibeatePropertiesList(final String path) {
Properties props = new Properties();
Resource resource = new ClassPathResource(path);
InputStream is = resource.getInputStream();
props.load( is );
retu props;
}
@Bean (name="txManager")
@Autowired
public PlatformTransactionManager getTransactionManager(LocalContainerEntityManagerFactoryBean lcemfb, JpaDialect jpaDialect) {
EntityManagerFactory emf = null;
emf = lcemfb.getObject();
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(emf);
jpaTransactionManager.setJpaDialect(jpaDialect);
retu jpaTransactionManager;
}
}
The HSQL config looks like this: @Configuration @Profile ("hsql") public class HsqlConfig { @Bean(name = "dataSource") public DataSource initDataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.HSQL) .addScript("classpath:env/dbcache/hsql-schema.sql") .addScript("classpath:env/dbcache/hsql-data.sql"); builder.setName("stranger"); builder.setScriptEncoding("UTF-8"); retu builder.build(); }
@Bean(name = "hibeateProperties")
public Properties getHibeateProperties() {
Properties props = new Properties();
props.put("hibeate.dialect", "org.hibeate.dialect.HSQLDialect");
props.put("hibeate.hbm2ddl.auto", "validate"); // using auto and ignoring the hsql scripts "works", but isn't correct
props.put("hibeate.default_schema", "stranger");
props.put("hibeate.current_session_context_class", "org.hibeate.context.inteal.ThreadLocalSessionContext");
retu props;
}
}
The other noticably odd thing about this is that hibeate seems to be looking for a column with the name user_USER_ID instead of USER_ID, and I'm not sure why that's happening either. I doubt this has all been caused by a mapping error since similar code seems to work with a differently configured EntityMappingFactory but I don't want to preclude the possibility. The code for this looks as follows:
@Entity (name="properties")
@Table (name="PROTECTED_PROPERTIES")
public class DbProtectedProperties extends AbstractModel<DbProtectedPropertiesId> implements Serializable {
private static final long serialVersionUID = 1L;
public void setId(DbProtectedPropertiesId id) {
super.id = id;
}
@EmbeddedId
public DbProtectedPropertiesId getId() {
if (super.id == null) {
super.id = new DbProtectedPropertiesId();
}
retu super.id;
}
@Column (name="PROPERTY_VALUE", length=4096, nullable=false)
public String getPropertyValue() {
retu propertyValue;
}
@Setter
private String propertyValue;
}
And the ID class: @EqualsAndHashCode ( of={ "user", "propertyName" } ) @ToString public class DbProtectedPropertiesId implements Serializable {
private static final long serialVersionUID = 1L;
@Setter
private DbUsers user;
@Setter
private String propertyName;
@ManyToOne (optional=false, fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn (name="USER_ID")
public DbUsers getUser() {
retu user;
}
@Column (name="PROPERTY_NAME", length=2048, nullable=false, insertable=false, updatable=false)
public String getPropertyName() {
retu propertyName;
}
}
