Last active
August 29, 2015 13:56
-
-
Save tonussi/9294121 to your computer and use it in GitHub Desktop.
Persistencias de Modos Diferentes
This file contains hidden or 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
@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