I have been reading spring\'s documentation, but I must say it is a bit confusing, giving several different option on how to configure JPA.
What is the best way, and
Alternatively Spring 3+ and JPA 2.0 can be integrated with the help of dynamic proxies.
You can find all the documentation and download example here
In this case interfaces with named JPA queries are used to execute queries. Interfaces are treated as ordinary Spring beans with the help of dynamic proxies. They can be injected (or autowired) into any other beans the same way.
Also queries can be located in separate orm-mapping.xml files and split up by domain (or at your convenience). That gives a high flexibility and maintainability to persistent layer.
public interface OrganisationQueries {
@Query(named = "find.organisation.by.role.id")
public Organisation findOrganisationByRoleId(Long roleId);
@Query(named = "find.all.organisations")
public List findAllOrganisations();
}
public class OrganisationServiceImpl implements OrganisationService {
@PersistenceContext
private EntityManager em;
@Autowired
private OrganisationQueries organisationQueries;
@Override
public Organisation findOrganisationByRoleId(Long roleId) {
return organisationQueries.findOrganisationByRoleId(roleId);
}
@Override
public List findAllOrganisations() {
return organisationQueries.findAllOrganisations();
}
}