JPA Criteria API group_concat usage

I figured out how to do this with Hibernate-jpa-mysql: 1.) created a GroupConcatFunction class extending org.hibernate.dialect.function.SQLFunction (this is for single column group_concat for now) public class GroupConcatFunction implements SQLFunction { @Override public boolean hasArguments() { return true; } @Override public boolean hasParenthesesIfNoArguments() { return true; } @Override public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException … 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

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

JPA transient information lost on create

This is, more or less, working as designed. The semantics of transient are precisely that the data is not persisted. The entity returned from entityManager.merge(obj) is, in fact, an entirely new entity that maintains the state of the object passed into merge (state, in this context, being anything that is not part of the persistent … Read more

JPA 2.0 native query results as map

Which JPA are you using – Hibernate, EclipseLink or something else? There is no standard way to do this in JPA but your specific implementation may allow it – for example, Eclipselink has a query result type hint. http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg03013.html Query query = entityManager.createNativeQuery(sql); query.setHint(QueryHints.RESULT_TYPE, ResultType.Map); For Hibernate, with javax.persistence.Query dbQuery: org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery) .getHibernateQuery(); hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);