SWF-1159 Use paramets in JPA query instead of concatenating

This commit is contained in:
Rossen Stoyanchev
2010-10-28 14:34:55 +00:00
parent 21d314b9b8
commit 70b56b5702
3 changed files with 81 additions and 74 deletions

View File

@@ -41,10 +41,13 @@ public class JpaBookingService implements BookingService {
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
return em.createQuery(
"select h from Hotel h where lower(h.name) like " + pattern + " or lower(h.city) like " + pattern
+ " or lower(h.zip) like " + pattern + " or lower(h.address) like " + pattern).setMaxResults(
criteria.getPageSize()).setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList();
int startIndex = criteria.getPage() * criteria.getPageSize();
return em
.createQuery(
"select h from Hotel h where lower(h.name) like :pattern or lower(h.city) like :pattern "
+ " or lower(h.zip) like :pattern or lower(h.address) like :pattern")
.setParameter("pattern", pattern).setFirstResult(startIndex).setMaxResults(criteria.getPageSize())
.getResultList();
}
@Transactional(readOnly = true)
@@ -73,15 +76,15 @@ public class JpaBookingService implements BookingService {
private String getSearchPattern(SearchCriteria criteria) {
if (StringUtils.hasText(criteria.getSearchString())) {
return "'%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
return "%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%";
} else {
return "'%'";
return "%";
}
}
private User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username",
username).getSingleResult();
return (User) em.createQuery("select u from User u where u.username = :username")
.setParameter("username", username).getSingleResult();
}
}

View File

@@ -19,69 +19,71 @@ import org.springframework.util.StringUtils;
@Repository
public class JpaBookingService implements BookingService {
private EntityManager em;
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Booking> findBookings(String username) {
if (username != null) {
return em.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate")
.setParameter("username", username).getResultList();
} else {
return null;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
return em.createQuery(
"select h from Hotel h where lower(h.name) like " + pattern + " or lower(h.city) like " + pattern
+ " or lower(h.zip) like " + pattern + " or lower(h.address) like " + pattern).setMaxResults(
criteria.getPageSize()).setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList();
}
@Transactional(readOnly = true)
public Hotel findHotelById(Long id) {
return em.find(Hotel.class, id);
}
@Transactional(readOnly = true)
public User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username",
username).getSingleResult();
}
@Transactional(readOnly = true)
public Booking createBooking(Long hotelId, String username) {
Hotel hotel = em.find(Hotel.class, hotelId);
User user = findUser(username);
return new Booking(hotel, user);
}
// read-write transactional methods
@Transactional
public void cancelBooking(Booking booking) {
booking = em.find(Booking.class, booking.getId());
if (booking != null) {
em.remove(booking);
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Booking> findBookings(String username) {
if (username != null) {
return em.createQuery("select b from Booking b where b.user.username = :username order by b.checkinDate")
.setParameter("username", username).getResultList();
} else {
return null;
}
}
}
// helpers
private String getSearchPattern(SearchCriteria criteria) {
if (StringUtils.hasText(criteria.getSearchString())) {
return "'%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
} else {
return "'%'";
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
return em
.createQuery(
"select h from Hotel h where lower(h.name) like :pattern or lower(h.city) like :pattern "
+ " or lower(h.zip) like :pattern or lower(h.address) like :pattern")
.setParameter("pattern", pattern).setMaxResults(criteria.getPageSize())
.setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList();
}
@Transactional(readOnly = true)
public Hotel findHotelById(Long id) {
return em.find(Hotel.class, id);
}
@Transactional(readOnly = true)
public User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username")
.setParameter("username", username).getSingleResult();
}
@Transactional(readOnly = true)
public Booking createBooking(Long hotelId, String username) {
Hotel hotel = em.find(Hotel.class, hotelId);
User user = findUser(username);
return new Booking(hotel, user);
}
// read-write transactional methods
@Transactional
public void cancelBooking(Booking booking) {
booking = em.find(Booking.class, booking.getId());
if (booking != null) {
em.remove(booking);
}
}
// helpers
private String getSearchPattern(SearchCriteria criteria) {
if (StringUtils.hasText(criteria.getSearchString())) {
return "%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%";
} else {
return "%";
}
}
}
}

View File

@@ -28,8 +28,8 @@ public class JpaBookingService implements BookingService {
@Transactional(readOnly = true)
public User findUser(String username) {
return (User) em.createQuery("select u from User u where u.username = :username").setParameter("username",
username).getSingleResult();
return (User) em.createQuery("select u from User u where u.username = :username")
.setParameter("username", username).getSingleResult();
}
@Transactional(readOnly = true)
@@ -47,10 +47,12 @@ public class JpaBookingService implements BookingService {
@SuppressWarnings("unchecked")
public List<Hotel> findHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
return em.createQuery(
"select h from Hotel h where lower(h.name) like " + pattern + " or lower(h.city) like " + pattern
+ " or lower(h.zip) like " + pattern + " or lower(h.address) like " + pattern).setMaxResults(
criteria.getPageSize()).setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList();
return em
.createQuery(
"select h from Hotel h where lower(h.name) like :pattern or lower(h.city) like :pattern "
+ " or lower(h.zip) like :pattern or lower(h.address) like :pattern")
.setParameter("pattern", pattern).setMaxResults(criteria.getPageSize())
.setFirstResult(criteria.getPage() * criteria.getPageSize()).getResultList();
}
@Transactional(readOnly = true)
@@ -77,9 +79,9 @@ public class JpaBookingService implements BookingService {
private String getSearchPattern(SearchCriteria criteria) {
if (StringUtils.hasText(criteria.getSearchString())) {
return "'%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%'";
return "%" + criteria.getSearchString().toLowerCase().replace('*', '%') + "%";
} else {
return "'%'";
return "%";
}
}