View Javadoc
1   package org.myorg.jpatickets.bo;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.LinkedList;
6   import java.util.List;
7   
8   import javax.persistence.*;
9   
10  @SuppressWarnings("serial")
11  @Entity
12  @Table(name="JPATICKETS_EVENT")
13  @NamedQueries({
14      @NamedQuery(name="JPATicketEvent.fetchEventTickets", 
15          query="select e from Event e "
16                  + "join fetch e.tickets "
17                  + "where e.id=:eventId"),
18      @NamedQuery(name="JPATicketEvent.fetchEventTicketsSeats", 
19          query="select e from Event e "
20                  + "join fetch e.venue "
21                  + "join fetch e.tickets t "
22                  + "join fetch t.seat "
23                  + "where e.id=:eventId")
24  })
25  @NamedNativeQueries({
26      @NamedNativeQuery(name="JPATicketEvent.fetchEventDTO",
27              query="select event.EVENT_ID, event.EVENT_NAME, event.START_TIME, event.VENUE_ID, "
28                      + "venue.NAME venueName, count(ticket.*) numTickets "
29                      + "from JPATICKETS_EVENT event "
30                      + "join JPATICKETS_VENUE venue on venue.VENUE_ID = event.VENUE_ID "
31                      + "join JPATICKETS_TICKET ticket on ticket.EVENT_ID = event.EVENT_ID "
32                      + "where event.EVENT_ID = :eventId "
33                      + "group by event.EVENT_ID, event.EVENT_NAME, event.START_TIME, event.VENUE_ID, venue.NAME",
34              resultSetMapping="JPATicketEvent.EventDTOMapping")
35  })
36  @SqlResultSetMappings({
37      @SqlResultSetMapping(name="JPATicketEvent.EventDTOMapping",
38              entities={
39                  @EntityResult(entityClass=Event.class)
40                  },
41              columns={
42                  @ColumnResult(name="venueName", type=String.class),
43                  @ColumnResult(name="numTickets", type=Long.class)
44                  }
45      )
46  })
47  public class Event /* implements Serializable */ {
48      @Id @GeneratedValue
49      @Column(name="EVENT_ID")
50      private int id;
51      
52      @Column(name="EVENT_NAME", length=40, nullable=false)
53      private String name;
54      
55      @ManyToOne(fetch=FetchType.LAZY, optional=false)
56      @JoinColumn(name="VENUE_ID", nullable=false, updatable=false)
57      private Venue venue;
58  
59      @Temporal(TemporalType.TIMESTAMP)
60      @Column(name="START_TIME")
61      private Date startTime;
62      
63      @OneToMany(mappedBy="event", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
64      private List<Ticket> tickets;
65      
66      protected Event() { }
67      public Event(Venue venue) { 
68          this.venue=venue;
69      }
70      public Event(int id) { this.id=id; }
71  
72      public int getId() { return id; }
73      public Venue getVenue() { return venue; }
74  
75      public String getName() { return name; }
76      public void setName(String name) {
77          this.name = name;
78      }
79      public Event withName(String name) {
80          setName(name);
81          return this;
82      }
83      
84      public Date getStartTime() { return startTime; }
85      public void setStartTime(Date startTime) {
86          this.startTime = startTime;
87      }
88      public Event withStartTime(Date startTime) {
89          setStartTime(startTime);
90          return this;
91      }
92  
93      public List<Ticket> getTickets() { return tickets; }
94      public void setTickets(List<Ticket> tickets) {
95          this.tickets = tickets;
96      }
97      public Event withTicket(Ticket ticket) {
98          if (tickets==null) {
99              tickets = new LinkedList<Ticket>();
100         }
101         if (ticket!=null) {
102             tickets.add(ticket);
103         }
104         return this;
105     }
106     
107     @Override
108     public String toString() {
109         StringBuilder builder = new StringBuilder();
110         builder.append("Event [id=").append(id)
111                 .append(", name=").append(name)
112                 .append(", venue=").append(venue==null ? null : venue.getName())
113                 .append(", startTime=").append(startTime)
114                 .append("]");
115         return builder.toString();
116     }
117 }