content: package org.agoncal.application.petstore.domain;
  
  import org.agoncal.application.petstore.constraint.NotEmpty;
  import org.agoncal.application.petstore.constraint.Price;
  
  import javax.persistence.*;
  import javax.validation.constraints.NotNull;
  import javax.validation.constraints.Size;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  /**
   * @author Antonio Goncalves
   *         http://www.antoniogoncalves.org
   *         --
   */
  
  @Entity
  @NamedQueries({
          @NamedQuery(name = Item.FIND_BY_PRODUCT_ID, query = "SELECT i FROM Item i WHERE i.product.id = :productId"),
          @NamedQuery(name = Item.SEARCH, query = "SELECT i FROM Item i WHERE UPPER(i.name) LIKE :keyword OR UPPER(i.product.name) LIKE :keyword ORDER BY i.product.category.name, i.product.name"),
          @NamedQuery(name = Item.FIND_ALL, query = "SELECT i FROM Item i")
  })
  @XmlRootElement
  public class Item {
  
      // ======================================
      // =             Attributes             =
      // ======================================
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      @Column(nullable = false, length = 30)
      @NotNull
      @Size(min = 1, max = 30)
      private String name;
      @Column(length = 3000)
      private String description;
      @Column(nullable = false)
      @Price
      private Float unitCost;
      @NotEmpty
      private String imagePath;
      @ManyToOne
      @JoinColumn(name = "product_fk", nullable = false)
      @XmlTransient
      private Product product;
  
      // ======================================
      // =             Constants              =
      // ======================================
  
      public static final String FIND_BY_PRODUCT_ID = "Item.findByProductId";
      public static final String SEARCH = "Item.search";
      public static final String FIND_ALL = "Item.findAll";
  
      // ======================================
      // =            Constructors            =
      // ======================================
  
      public Item() {
      }
  
      public Item(String name, Float unitCost, String imagePath, Product product, String description) {
          this.name = name;
          this.unitCost = unitCost;
          this.imagePath = imagePath;
          this.product = product;
          this.description = description;
      }
  
      // ======================================
      // =         Getters & setters          =
      // ======================================
  
      public Long getId() {
          return id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Float getUnitCost() {
          return unitCost;
      }
  
      public void setUnitCost(Float unitCost) {
          this.unitCost = unitCost;
      }
  
      public String getImagePath() {
          return imagePath;
      }
  
      public void setImagePath(String imagePath) {
          this.imagePath = imagePath;
      }
  
      public Product getProduct() {
          return product;
      }
  
      public void setProduct(Product product) {
          this.product = product;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      // ======================================
      // =   Methods hash, equals, toString   =
      // ======================================
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (!(o instanceof Item)) return false;
  
          Item item = (Item) o;
  
          if (!imagePath.equals(item.imagePath)) return false;
          if (!name.equals(item.name)) return false;
  
          return true;
      }
  
      @Override
      public int hashCode() {
          int result = name.hashCode();
          result = 31 * result + imagePath.hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          final StringBuilder sb = new StringBuilder();
          sb.append("Item");
          sb.append("{id=").append(id);
          sb.append(", name='").append(name).append('\'');
          sb.append(", unitCost=").append(unitCost).append('\'');
          sb.append(", imagePath='").append(imagePath).append('\'');
          sb.append(", description='").append(description).append('\'');
          sb.append('}');
          return sb.toString();
      }
  }