Multiple data source and schema creation in Spring Boot

spring.jpa.hibernate.ddl-auto=create has stopped working, not because you have two DataSources, but because your application’s creating its own LocalContainerEntityManagerFactoryBeans. This has the effect of disabling the auto-configuration of a LocalContainerEntityManagerFactoryBean so you now have to configure it yourself. You can configure the two entity managers to have different schema generation behaviour like this (the first’s doing … Read more

Adding entity classes dynamically at runtime

JPA doesn’t offer this feature yet. Here are three options you can check out : This work around : 1. Generate a persistence.xml on the fly (simple XML file creation) with a new persistence unit. 2. Add persistence file to classpath dynamically (URLCLassLoader) 3. Ask PersistenceProvider to load new persistence unit (createEntityManagerFactory) I did implement … Read more

Rollback on every checked exception, whenever I say @Transactional

Custom Shortcut Annotations I know, that I could create a custom annotation, but that seems unnatural. No, this is exactly the use case for a Custom Annotation. Here’s a quote from Custom Shortcut Annotations in the Spring Reference: If you find you are repeatedly using the same attributes with @Transactional on many different methods, then … Read more

Spring Data JPA and Querydsl to fetch subset of columns using bean/constructor projection

Looks like custom repository implementation is the way to go for now until something similar available in spring data. I have gone through http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations Here is my implementation which works. However it would be good to have this method available directly in Spring-Data-JPA Step 1: Intermediate interface for shared behavior public interface CustomQueryDslJpaRepository <T, ID … Read more

Is it possible to write a generic enum converter for JPA?

Based on @scottb solution I made this, tested against hibernate 4.3: (no hibernate classes, should run on JPA just fine) Interface enum must implement: public interface PersistableEnum<T> { public T getValue(); } Base abstract converter: @Converter public abstract class AbstractEnumConverter<T extends Enum<T> & PersistableEnum<E>, E> implements AttributeConverter<T, E> { private final Class<T> clazz; public AbstractEnumConverter(Class<T> … Read more

JPA: JOIN in JPQL

Join on one-to-many relation in JPQL looks as follows: select b.fname, b.lname from Users b JOIN b.groups c where c.groupName = :groupName When several properties are specified in select clause, result is returned as Object[]: Object[] temp = (Object[]) em.createNamedQuery(“…”) .setParameter(“groupName”, groupName) .getSingleResult(); String fname = (String) temp[0]; String lname = (String) temp[1]; By the … Read more

Testing an EJB with JUnit

The accepted answer requires mocking a lot of code, including the persistence layer. Use an embedded container to test the actual beans, instead; otherwise, mocking the persistence layer results in code that barely tests anything useful. Use a session bean with an entity manager that references a persistence unit: @Stateless public class CommentService { @PersistenceContext(unitName … Read more

tech