Seat.java

  1. package org.myorg.jpatickets.bo;

  2. import java.io.Serializable;

  3. import javax.persistence.*;

  4. @SuppressWarnings("serial")
  5. @Entity
  6. @Table(name="JPATICKET_SEAT")
  7. @NamedQueries({
  8.     @NamedQuery(name = "JPATicketSeat.getSeatsForVenue",
  9.             query = "select s from Seat s where s.venue = :venue")
  10. })
  11. public class Seat implements Serializable {
  12.     @EmbeddedId
  13.     private SeatPK pk;
  14.    
  15.     @MapsId("venueId")
  16.     @ManyToOne(fetch=FetchType.LAZY)
  17.     @JoinColumn(name="VENUE_ID")
  18.     private Venue venue;
  19.    
  20.     protected Seat() {}
  21.     public Seat(Venue venue, String section, int row, int position) {
  22.         this.venue = venue;
  23.         this.pk = new SeatPK(venue==null ? null : venue.getId(), section, row, position);
  24.     }

  25.     public Venue getVenue()    { return venue; }
  26.     public String getVenueId() { return pk==null ? null : pk.getVenueId(); }
  27.     public String getSection() { return pk==null ? null : pk.getSection(); }
  28.     public int getRow()        { return pk==null ? 0 : pk.getRow(); }
  29.     public int getPosition()   { return pk==null ? 0 : pk.getPosition(); }

  30.     @Override
  31.     public int hashCode() {
  32.         final int prime = 31;
  33.         int result = 1;
  34.         result = prime * result + getPosition();
  35.         result = prime * result + getRow();
  36.         result = prime * result + ((getSection() == null) ? 0 : getSection().hashCode());
  37.         result = prime * result + ((getVenueId() == null) ? 0 : getVenueId().hashCode());
  38.         return result;
  39.     }
  40.    
  41.     @Override
  42.     public boolean equals(Object obj) {
  43.         if (this == obj) { return true; }
  44.         if (obj == null) { return false; }
  45.         if (getClass() != obj.getClass()) { return false; }
  46.         Seat other = (Seat) obj;
  47.         return (getPosition() == other.getPosition()) &&
  48.            (getRow() != other.getRow()) &&
  49.            (getSection() == null ? other.getSection()==null : getSection().equals(other.getSection())) &&
  50.            (getVenueId() ==null ? other.getVenueId()==null : venue.getId().equals(other.getVenueId()));
  51.     }
  52.    
  53.    
  54.     @Override
  55.     public String toString() {
  56.         StringBuilder builder = new StringBuilder();
  57.         builder.append("Seat [venue=").append(getVenueId())
  58.                 .append(", section=").append(getSection())
  59.                 .append(", row=").append(getRow())
  60.                 .append(", position=").append(getPosition())
  61.                 .append("]");
  62.         return builder.toString();
  63.     }
  64. }