Posts

Showing posts from February, 2013

Hibernate schema export with Hibernate Validator constraints accepted and Spring configured persistence

What is the goal: using Spring Framework for configuration of persistence settings (JPA 2.0 with Hibernate provider) using the configuration for Hibernate schema export tool apply Hibernate Validator constraints to generated schema Spring persistence configuration // define datasource - hsqldb for testing purposes - it requires hsqldb to be on classpath EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); EmbeddedDatabase database = builder.setType(EmbeddedDatabaseType.HSQL).build(); // standard way how to configure persistence in Spring (usually included as a part of some configuration class) LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); lcemfb.setPersistenceXmlLocation("classpath:example/path/to/persistence.xml"); lcemfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); lcemfb.setDataSource(database); lcemfb.afterPropertiesSet(); Create Hibernate configuration from Spring configured persistence Ej

Hibernate delete and update queries with joins

From Hibernate 4.1.9 manual - section about bulk update and deletes : No implicit or explicit joins can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins. Unsupported (implicit join) DELETE A a WHERE a.relation.property = "dummy"; A way to go DELETE A a WHERE a.relation.id IN (SELECT r.id FROM Relation r WHERE r.property = "dummy");