Created
August 10, 2012 18:36
-
-
Save jdewind/3316568 to your computer and use it in GitHub Desktop.
Refresh with Helper Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<SchoolBus> findAll() { | |
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); | |
CriteriaQuery<SchoolBus> criteriaQuery = criteriaBuilder.createQuery(SchoolBus.class); | |
Root<SchoolBus> table = criteriaQuery.from(SchoolBus.class); | |
TypedQuery<SchoolBus> query = getEntityManager().createQuery(criteriaQuery.select(table).orderBy(criteriaBuilder.asc(table.get("busNumber")))); | |
refreshSchoolBuses(query.getResultList()); | |
return query.getResultList(); | |
} | |
public SchoolBus findById(Long id) { | |
SchoolBus schoolBus = getEntityManager().find(SchoolBus.class, id); | |
refreshSchoolBuses(schoolBus); | |
return schoolBus; | |
} | |
private void refreshSchoolBuses(SchoolBus... schoolBuses) { | |
refreshSchoolBuses(Arrays.asList(schoolBuses)); | |
} | |
private void refreshSchoolBuses(List<SchoolBus> schoolBuses) { | |
for (SchoolBus schoolBus : schoolBuses) { | |
schoolBus.getStudents().clear(); | |
getEntityManager().refresh(schoolBus); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment