SWF-1408 Remove use of persistence context in samples

This commit is contained in:
Rossen Stoyanchev
2010-12-06 12:13:34 +00:00
parent 55db452771
commit 3a97ce08bf
16 changed files with 108 additions and 101 deletions

View File

@@ -40,6 +40,12 @@ public interface BookingService {
*/
public Booking createBooking(Long hotelId, String userName);
/**
* Persist the booking to the database
* @param booking the booking
*/
public void persistBooking(Booking booking);
/**
* Cancel an existing booking.
* @param id the booking id

View File

@@ -46,20 +46,22 @@ public class JpaBookingService implements BookingService, Serializable {
String pattern = getSearchPattern(criteria);
orderBy = (orderBy != null) ? orderBy : "name";
String orderDirection = (ascending) ? " ASC" : " DESC";
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 order by h." + orderBy
+ orderDirection).setParameter("pattern", pattern).setMaxResults(criteria.getPageSize())
.setFirstResult(firstResult).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 order by h."
+ orderBy + orderDirection).setParameter("pattern", pattern)
.setMaxResults(criteria.getPageSize()).setFirstResult(firstResult).getResultList();
}
@Transactional(readOnly = true)
public int getNumberOfHotels(SearchCriteria criteria) {
String pattern = getSearchPattern(criteria);
Long count = (Long) em.createQuery(
"select count(h.id) 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).getSingleResult();
Long count = (Long) em
.createQuery(
"select count(h.id) 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).getSingleResult();
return count.intValue();
}
@@ -73,11 +75,14 @@ public class JpaBookingService implements BookingService, Serializable {
Hotel hotel = em.find(Hotel.class, hotelId);
User user = findUser(username);
Booking booking = new Booking(hotel, user);
em.persist(booking);
return booking;
}
// read-write transactional methods
@Transactional
public void persistBooking(Booking booking) {
em.persist(booking);
}
@Transactional
public void cancelBooking(Booking booking) {
booking = em.find(Booking.class, booking.getId());
@@ -97,8 +102,8 @@ public class JpaBookingService implements BookingService, Serializable {
}
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

@@ -12,7 +12,6 @@
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
<webflow:listener ref="jpaFlowExecutionListener" />
<webflow:listener ref="securityFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
@@ -27,12 +26,6 @@
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
<!-- Installs a listener that manages JPA persistence contexts for flows that require them -->
<bean id="jpaFlowExecutionListener" class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
<constructor-arg ref="entityManagerFactory" />
<constructor-arg ref="transactionManager" />
</bean>
<!-- Installs a listener to apply Spring Security authorities -->
<bean id="securityFlowExecutionListener" class="org.springframework.webflow.security.SecurityFlowExecutionListener" />

View File

@@ -5,8 +5,6 @@
<secured attributes="ROLE_USER" />
<persistence-context/>
<input name="hotelId" required="true" />
<on-start>
@@ -19,12 +17,14 @@
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="confirm" to="bookingConfirmed">
<evaluate expression="bookingService.persistBooking(booking)" />
</transition>
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>
<end-state id="bookingConfirmed" commit="true" />
<end-state id="bookingConfirmed" />
<end-state id="bookingCancelled" />