Skip to content

Instantly share code, notes, and snippets.

@jdewind
Created August 10, 2012 18:03
Show Gist options
  • Save jdewind/3316217 to your computer and use it in GitHub Desktop.
Save jdewind/3316217 to your computer and use it in GitHub Desktop.
Refresh Entity
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"))));
/*
/ Refresh all the school buses
*/
for (SchoolBus schoolBus : query.getResultList()) {
schoolBus.getStudents().clear();
getEntityManager().refresh(schoolBus);
}
return query.getResultList();
}
public SchoolBus findById(Long id) {
SchoolBus schoolBus = getEntityManager().find(SchoolBus.class, id);
/*
/ Refresh school bus
*/
schoolBus.getStudents().clear();
getEntityManager().refresh(schoolBus);
return schoolBus;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment