JPA’s EntityManager createQuery() vs createNamedQuery() vs createNativeQuery()

The createQuery method is used to create dynamic queries, which are queries defined directly within an application’s business logic. Example: public List findWithName(String name) { return em.createQuery( “SELECT c FROM Customer c WHERE c.name LIKE :custName”) .setParameter(“custName”, name) .setMaxResults(10) .getResultList(); } The createNamedQuery method is used to create static queries, or queries that are defined … 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