View Javadoc
1   package org.myorg.jpatickets.dao;
2   
3   import java.util.List;
4   
5   import javax.persistence.EntityManager;
6   import javax.persistence.FlushModeType;
7   import javax.persistence.TypedQuery;
8   
9   import org.myorg.jpatickets.bo.Seat;
10  import org.myorg.jpatickets.bo.Venue;
11  
12  public class VenueDAOImpl implements VenueDAO {
13      private EntityManager em;
14      
15      public void setEntityManager(EntityManager em) {
16          this.em = em;
17      }
18  
19      @Override
20      public void saveVenue(Venue venue, List<Seat> seats) {
21          em.setFlushMode(FlushModeType.COMMIT);
22          em.persist(venue);        
23          if (seats!=null) {
24              for (Seat s: seats) {
25                  em.persist(s);
26              }
27          }
28          em.setFlushMode(FlushModeType.AUTO);
29      }
30      
31      public Venue getVenue(String venueId) {
32          return em.find(Venue.class, venueId);
33      }
34      
35      @Override
36      public List<Seat> getSeatsForVenue(Venue venue, int offset, int limit) {
37          TypedQuery<Seat> query = em.createNamedQuery("JPATicketSeat.getSeatsForVenue", 
38                  Seat.class)
39                  .setParameter("venue", venue);
40          if (offset > 0) { query.setFirstResult(offset); }
41          if (limit > 0) { query.setMaxResults(limit); }
42          return query.getResultList();
43      }
44  }