Skip to content

Instantly share code, notes, and snippets.

@tonussi
Last active August 29, 2015 13:56
Show Gist options
  • Save tonussi/9294121 to your computer and use it in GitHub Desktop.
Save tonussi/9294121 to your computer and use it in GitHub Desktop.
Persistencias de Modos Diferentes
@Service
public class VehicleServiceImpl implements VehicleService {
@PersistenceContext
EntityManager em;
/**
* O(1)
*/
@Override
@Transactional
public void addVehicle(Vehicle vehicle) {
em.persist(vehicle);
}
// other method omitted ..
/**
* O(n)
*/
@Override
@Transactional
public List<Vehicle> findVehicleByBrand(String column, String order) {
return em.createNativeQuery(
"select * from vehicle as v where v." + column.toLowerCase() + " = '"
+ column + "' order by " + column.toLowerCase() + " " + order)
.getResultList();
}
// other method omitted ..
/**
* O(n)
*/
@Override
@Transactional
public List<Vehicle> findVehicles() {
CriteriaQuery<Vehicle> c = em.getCriteriaBuilder().createQuery(
Vehicle.class);
// c.from(Vehicle.class);
Root<Vehicle> from = c.from(Vehicle.class);
c.orderBy(em.getCriteriaBuilder().asc(from.get("brand")));
return em.createQuery(c).getResultList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment