Switch to Hibernate 3 with <persistence-context>
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
<properties>
|
||||
<springframework-version>4.0.0.RELEASE</springframework-version>
|
||||
<springsecurity-version>3.2.0.RELEASE</springsecurity-version>
|
||||
<webflow-version>2.3.3.RELEASE</webflow-version>
|
||||
<webflow-version>2.4.0.RELEASE</webflow-version>
|
||||
<slf4j-version>1.7.5</slf4j-version>
|
||||
<thymeleaf-version>2.0.15</thymeleaf-version>
|
||||
<thymeleaf-extras-tiles2-version>2.0.0</thymeleaf-extras-tiles2-version>
|
||||
@@ -167,6 +167,9 @@
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>3.6.9.Final</version>
|
||||
<!--
|
||||
<version>4.2.8.Final</version>
|
||||
-->
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet -->
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package org.springframework.webflow.samples.booking;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Service("bookingService")
|
||||
@Repository
|
||||
public class HibernateBookingService implements BookingService {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Autowired
|
||||
public void setSessionFactory(SessionFactory sessionFactory) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Booking> findBookings(String username) {
|
||||
if (username != null) {
|
||||
return sessionFactory
|
||||
.getCurrentSession()
|
||||
.createQuery(
|
||||
"select b from Booking b where b.user.username = :username order by b.checkinDate")
|
||||
.setParameter("username", username).list();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Hotel> findHotels(SearchCriteria criteria) {
|
||||
String pattern = getSearchPattern(criteria);
|
||||
int startIndex = criteria.getPage() * criteria.getPageSize();
|
||||
return sessionFactory.getCurrentSession()
|
||||
.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()).list();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Hotel findHotelById(Long id) {
|
||||
return (Hotel) sessionFactory.getCurrentSession().get(Hotel.class, id);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Booking createBooking(Long hotelId, String username) {
|
||||
Hotel hotel = (Hotel) sessionFactory.getCurrentSession().get(Hotel.class, hotelId);
|
||||
User user = findUser(username);
|
||||
Booking booking = new Booking(hotel, user);
|
||||
return booking;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void persistBooking(Booking booking) {
|
||||
sessionFactory.getCurrentSession().persist(booking);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void cancelBooking(Long id) {
|
||||
Booking booking = (Booking) sessionFactory.getCurrentSession().get(Booking.class, id);
|
||||
if (booking != null) {
|
||||
sessionFactory.getCurrentSession().delete(booking);
|
||||
}
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
private String getSearchPattern(SearchCriteria criteria) {
|
||||
if (StringUtils.hasText(criteria.getSearchString())) {
|
||||
return "%"
|
||||
+ criteria.getSearchString().toLowerCase()
|
||||
.replace('*', '%') + "%";
|
||||
} else {
|
||||
return "%";
|
||||
}
|
||||
}
|
||||
|
||||
private User findUser(String username) {
|
||||
return (User) sessionFactory.getCurrentSession()
|
||||
.createQuery(
|
||||
"select u from User u where u.username = :username")
|
||||
.setParameter("username", username).uniqueResult();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,8 +15,8 @@ import org.springframework.util.StringUtils;
|
||||
* against the backing repository. The EntityManager reference is provided by the managing container (Spring)
|
||||
* automatically.
|
||||
*/
|
||||
@Service("bookingService")
|
||||
@Repository
|
||||
//@Service("bookingService")
|
||||
//@Repository
|
||||
public class JpaBookingService implements BookingService {
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
@@ -9,20 +9,32 @@
|
||||
<!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
|
||||
<tx:annotation-driven />
|
||||
|
||||
<!-- Drives transactions using local JPA APIs -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory" />
|
||||
<!--
|
||||
Hibernate 3: org.springframework.orm.hibernate3.HibernateTransactionManager
|
||||
Hibernate 4: org.springframework.orm.hibernate4.HibernateTransactionManager
|
||||
-->
|
||||
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
|
||||
<!--
|
||||
Hibernate 3: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
|
||||
Hibernate 4: org.springframework.orm.hibernate4.LocalSessionFactoryBean
|
||||
-->
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
|
||||
<property name="annotatedClasses">
|
||||
<list>
|
||||
<value>org.springframework.webflow.samples.booking.User</value>
|
||||
<value>org.springframework.webflow.samples.booking.Booking</value>
|
||||
<value>org.springframework.webflow.samples.booking.Hotel</value>
|
||||
</list>
|
||||
</property>
|
||||
<property name="jpaProperties">
|
||||
<property name="hibernateProperties">
|
||||
<value>
|
||||
hibernate.session_factory_name=mySessionFactory
|
||||
hibernate.dialect=org.hibernate.dialect.HSQLDialect
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.show_sql=true
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
@@ -10,8 +10,14 @@
|
||||
<webflow:flow-executor id="flowExecutor">
|
||||
<webflow:flow-execution-listeners>
|
||||
<webflow:listener ref="securityFlowExecutionListener" />
|
||||
<webflow:listener ref="hibernateFlowExecutionListener" />
|
||||
</webflow:flow-execution-listeners>
|
||||
</webflow:flow-executor>
|
||||
|
||||
<bean id="hibernateFlowExecutionListener" class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
|
||||
<constructor-arg ref="sessionFactory" />
|
||||
<constructor-arg ref="transactionManager" />
|
||||
</bean>
|
||||
|
||||
<!-- The registry of executable flow definitions -->
|
||||
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF">
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
<secured attributes="ROLE_USER" />
|
||||
|
||||
<persistence-context />
|
||||
|
||||
<input name="hotelId" required="true" />
|
||||
|
||||
<on-start>
|
||||
@@ -43,10 +45,10 @@
|
||||
<transition on="cancel" to="cancel" />
|
||||
</view-state>
|
||||
|
||||
<end-state id="bookingConfirmed">
|
||||
<end-state id="bookingConfirmed" commit="true">
|
||||
<output name="confirmed" value="'Your booking is confirmed, you can book another hotel by searching again.'"/>
|
||||
</end-state>
|
||||
|
||||
<end-state id="cancel" />
|
||||
<end-state id="cancel" commit="false"/>
|
||||
|
||||
</flow>
|
||||
|
||||
Reference in New Issue
Block a user