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" />

View File

@@ -37,6 +37,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

@@ -60,10 +60,14 @@ public class JpaBookingService implements BookingService {
Hotel hotel = em.find(Hotel.class, hotelId);
User user = findUser(username);
Booking booking = new Booking(hotel, user);
em.persist(booking);
return booking;
}
@Transactional
public void persistBooking(Booking booking) {
em.persist(booking);
}
@Transactional
public void cancelBooking(Long id) {
Booking booking = em.find(Booking.class, id);

View File

@@ -11,7 +11,6 @@
<!-- Executes flows: the entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="jpaFlowExecutionListener" />
<webflow:listener ref="securityFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
@@ -20,7 +19,7 @@
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
<!-- Plugs in a custom creator for Web Flow views -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator"
development="true" validator="validator" />
@@ -31,16 +30,10 @@
<property name="useSpringBeanBinding" value="true" />
</bean>
<!-- 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" />
<!-- Bootstraps JSR-303 validation and adapts it to Spring's Validator interface -->
<!-- Bootstraps JSR-303 validation and exposes it through Spring's Validator interface -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</beans>

View File

@@ -5,8 +5,6 @@
<secured attributes="ROLE_USER" />
<persistence-context/>
<input name="hotelId" required="true" />
<on-start>
@@ -30,12 +28,14 @@
</view-state>
<view-state id="reviewBooking" model="booking">
<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="cancel" />
</view-state>
<end-state id="bookingConfirmed" commit="true" />
<end-state id="bookingConfirmed"/>
<end-state id="cancel" />

View File

@@ -8,46 +8,52 @@ import java.util.List;
*/
public interface BookingService {
/**
* Find bookings made by the given user
* @param username the user's name
* @return their bookings
*/
public List<Booking> findBookings(String username);
/**
* Find bookings made by the given user
* @param username the user's name
* @return their bookings
*/
public List<Booking> findBookings(String username);
/**
* Find hotels available for booking by some criteria.
* @param criteria the search criteria
* @return a list of hotels meeting the criteria
*/
public List<Hotel> findHotels(SearchCriteria criteria);
/**
* Find hotels available for booking by some criteria.
* @param criteria the search criteria
* @return a list of hotels meeting the criteria
*/
public List<Hotel> findHotels(SearchCriteria criteria);
/**
* Find hotels by their identifier.
* @param id the hotel id
* @return the hotel
*/
public Hotel findHotelById(Long id);
/**
* Find hotels by their identifier.
* @param id the hotel id
* @return the hotel
*/
public Hotel findHotelById(Long id);
/**
* Find user by their username.
* @param username the user's username
* @return the user
*/
public User findUser(String username);
/**
* Find user by their username.
* @param username the user's username
* @return the user
*/
public User findUser(String username);
/**
* Create a new, transient hotel booking instance for the given user.
* @param hotelId the hotelId
* @param userName the user name
* @return the new transient booking instance
*/
public Booking createBooking(Long hotelId, String userName);
/**
* Create a new, transient hotel booking instance for the given user.
* @param hotelId the hotelId
* @param userName the user name
* @return the new transient booking instance
*/
public Booking createBooking(Long hotelId, String userName);
/**
* Cancel an existing booking.
* @param id the booking id
*/
public void cancelBooking(Booking booking);
/**
* Persist the booking to the database
* @param booking the booking
*/
public void persistBooking(Booking booking);
/**
* Cancel an existing booking.
* @param id the booking id
*/
public void cancelBooking(Booking booking);
}

View File

@@ -67,7 +67,11 @@ public class JpaBookingService implements BookingService {
return new Booking(hotel, user);
}
// 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());

View File

@@ -37,7 +37,6 @@
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextLifecycleListener"/>
<webflow:listener ref="jpaFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
@@ -53,11 +52,5 @@
<!-- Installs a listener that creates and releases the FacesContext for each request. -->
<bean id="facesContextLifecycleListener" 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>
</beans>

View File

@@ -3,8 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<persistence-context/>
<input name="hotelId" value="flowScope.hotelId"/>
<on-start>
@@ -20,13 +18,13 @@
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed">
<evaluate expression="persistenceContext.persist(booking)" />
<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" />

View File

@@ -44,6 +44,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

@@ -67,6 +67,11 @@ public class JpaBookingService implements BookingService {
return new Booking(hotel, user);
}
@Transactional
public void persistBooking(Booking booking) {
em.persist(booking);
}
@Transactional
public void cancelBooking(Long id) {
Booking booking = em.find(Booking.class, id);

View File

@@ -31,11 +31,7 @@
</bean>
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="jpaFlowExecutionListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<webflow:flow-executor id="flowExecutor" />
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
@@ -47,13 +43,7 @@
<!-- Plugs in Spring's JSR-303 validator adapter -->
<webflow:flow-builder-services id="flowBuilderServices" development="true" validator="validator" />
<!-- 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>
<!-- Bootstraps JSR-303 validation and adapts it to Spring's Validator interface -->
<!-- Bootstraps JSR-303 validation and exposes it through Spring's Validator interface -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</beans>

View File

@@ -5,8 +5,6 @@
<secured attributes="ROLE_USER" />
<persistence-context/>
<input name="hotelId" />
<on-start>
@@ -20,7 +18,7 @@
<view-state id="reviewBooking" model="booking">
<transition on="confirm" to="bookingConfirmed">
<evaluate expression="persistenceContext.persist(booking)" />
<evaluate expression="bookingService.persistBooking(booking)" />
</transition>
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="cancel" />