1 package org.myorg.jpatickets.bo; 2 3 import java.io.Serializable; 4 5 import javax.persistence.*; 6 7 @SuppressWarnings("serial") 8 @Entity 9 @Table(name="JPATICKET_SEAT") 10 @NamedQueries({ 11 @NamedQuery(name = "JPATicketSeat.getSeatsForVenue", 12 query = "select s from Seat s where s.venue = :venue") 13 }) 14 public class Seat implements Serializable { 15 @EmbeddedId 16 private SeatPK pk; 17 18 @MapsId("venueId") 19 @ManyToOne(fetch=FetchType.LAZY) 20 @JoinColumn(name="VENUE_ID") 21 private Venue venue; 22 23 protected Seat() {} 24 public Seat(Venue venue, String section, int row, int position) { 25 this.venue = venue; 26 this.pk = new SeatPK(venue==null ? null : venue.getId(), section, row, position); 27 } 28 29 public Venue getVenue() { return venue; } 30 public String getVenueId() { return pk==null ? null : pk.getVenueId(); } 31 public String getSection() { return pk==null ? null : pk.getSection(); } 32 public int getRow() { return pk==null ? 0 : pk.getRow(); } 33 public int getPosition() { return pk==null ? 0 : pk.getPosition(); } 34 35 @Override 36 public int hashCode() { 37 final int prime = 31; 38 int result = 1; 39 result = prime * result + getPosition(); 40 result = prime * result + getRow(); 41 result = prime * result + ((getSection() == null) ? 0 : getSection().hashCode()); 42 result = prime * result + ((getVenueId() == null) ? 0 : getVenueId().hashCode()); 43 return result; 44 } 45 46 @Override 47 public boolean equals(Object obj) { 48 if (this == obj) { return true; } 49 if (obj == null) { return false; } 50 if (getClass() != obj.getClass()) { return false; } 51 Seat other = (Seat) obj; 52 return (getPosition() == other.getPosition()) && 53 (getRow() != other.getRow()) && 54 (getSection() == null ? other.getSection()==null : getSection().equals(other.getSection())) && 55 (getVenueId() ==null ? other.getVenueId()==null : venue.getId().equals(other.getVenueId())); 56 } 57 58 59 @Override 60 public String toString() { 61 StringBuilder builder = new StringBuilder(); 62 builder.append("Seat [venue=").append(getVenueId()) 63 .append(", section=").append(getSection()) 64 .append(", row=").append(getRow()) 65 .append(", position=").append(getPosition()) 66 .append("]"); 67 return builder.toString(); 68 } 69 }