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

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

How do I do a “deep” fetch join in JPQL?

The JPA spec does not allow aliasing a fetch join, but some JPA providers do. EclipseLink does as of 2.4. EclipseLink also allow nested join fetch using the dot notation (i.e. “JOIN FETCH a.bs.c”), and supports a query hint “eclipselink.join-fetch” that allows nested joins (you can specify multiple hints of the same hint name). In … Read more

kotlin data class + bean validation jsr 303

You need to use Annotation use-site targets since the default for a property declared in the constructor is to target the annotation on the constructor parameter instead of the getter (which will be seen by JavaBeans compliant hosts) when there are multiple options available. Also using a data class might be inappropriate here (see note … Read more

JPA passing list to IN clause in named native query

The above accepted answer is not correct and led me off track for many days !! JPA and Hibernate both accept collections in native query using Query. You just need to do String nativeQuery = “Select * from A where name in :names”; //use (:names) for older versions of hibernate Query q = em.createNativeQuery(nativeQuery); q.setParameter(“names”, … Read more

Is it possible to output generated SQL using EclipseLink without having to increase log verbosity?

Put the following properties in your persistence.xml: <property name=”eclipselink.logging.level.sql” value=”FINE”/> <property name=”eclipselink.logging.parameters” value=”true”/> The latter is helpful, so that the values of the parameter are shown. An alternative is using log4jdbc or log4jdbc-remix.

Spring batch jpaPagingItemReader why some rows are not read?

org.springframework.batch.item.database.JpaPagingItemReader creates is own entityManager instance (from org.springframework.batch.item.database.JpaPagingItemReader#doOpen) : entityManager = entityManagerFactory.createEntityManager(jpaPropertyMap); If you are within a transaction, as it seems to be, reader entities are not detached (from org.springframework.batch.item.database.JpaPagingItemReader#doReadPage): if (!transacted) { List<T> queryResult = query.getResultList(); for (T entity : queryResult) { entityManager.detach(entity); results.add(entity); }//end if } else { results.addAll(query.getResultList()); tx.commit(); } For this … Read more