Figure 923. Populating the database Slide presentation Create comment in forum
tx.begin();
 entityManager.persist(new StudyCourse("CSM","Computer Science and Media"));
 entityManager.persist(new StudyCourse("MMB","Mobile Media"));
 entityManager.persist(new StudyCourse("CRB","Cross Media Journalism"));
 entityManager.persist(new StudyCourse("IDB","Information Design"));
tx.commit();

Figure 924. JPQL: Domain model and database Slide presentation Create comment in forum
final List<StudyCourse> studyCourses
=
entityManager.createQuery(
  "SELECT S FROM StudyCourse AS S").getResultList();
  • No type checking whatsoever.

  • Possible runtime exceptions.


Figure 925. JPA query alternatives Slide presentation Create comment in forum
JPQL:
entityManager.createQuery(
 "SELECT S FROM StudyCourse AS S WHERE S.shortName like 'C%'")
Criteria queries:
criteria.select(studyCourseRoot);
criteria.where( builder.like( studyCourseRootRoot.
   get(StudyCourse_.shortName), "C%" ) );
Querydsl:
new JPAQuery(entityManager).
  from(qStudyCourse).
  where(qStudyCourse.shortName.like("C%"))