Java - Entities - Shopping Cart

Creating a shopping cart class using entities in java.

Imports

package model.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

Entities

/* @author Steven Bartsch */
@Entity
@Table(name = "user_cart")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "UserCart.findAll", query = "SELECT u FROM UserCart u"),
    @NamedQuery(name = "UserCart.findByUserid", query = "SELECT u FROM UserCart u WHERE u.userCartPK.userid = :userid"),
    @NamedQuery(name = "UserCart.findByItemid", query = "SELECT u FROM UserCart u WHERE u.userCartPK.itemid = :itemid"),
    @NamedQuery(name = "UserCart.findByQty", query = "SELECT u FROM UserCart u WHERE u.qty = :qty"),
    @NamedQuery(name = "UserCart.Update", query = "UPDATE UserCart u SET u.qty = u.qty + 1 WHERE u.userCartPK.userid = :userid AND u.userCartPK.itemid = :itemid"),
    @NamedQuery(name = "UserCart.findUseridAndItemid", query = "SELECT u.userCartPK.userid,u.userCartPK.itemid FROM UserCart u WHERE u.userCartPK.userid = :userid AND u.userCartPK.itemid = :itemid"),
    @NamedQuery(name = "UserCart.leftJoinCatalog", query = "SELECT u, i FROM UserCart u LEFT JOIN ItemCatalog i ON u.userCartPK.itemid=i.ItemCatalog.itemid"),
    @NamedQuery(name = "UserCart.Subtract", query = "DELETE FROM UserCart u WHERE u.userCartPK.userid = :userid AND u.userCartPK.itemid = :itemid"),
    @NamedQuery(name = "UserCart.RemoveUserid", query = "DELETE FROM UserCart u WHERE u.userCartPK.userid = :userid")})

Members

public class UserCart implements Serializable{
    @EmbeddedId
    protected UserCartPK userCartPK;
    @Column(name = "qty")
    private Integer qty;
    @JoinColumn(name = "userid", referencedColumnName = "userid", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private UserAccount userAccount;
    @JoinColumn(name = "itemid", referencedColumnName = "itemid", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ItemCatalog itemCatalog;

Methods

    public UserCart(){
    }

    public UserCart(UserCartPK userCartPK){
        this.userCartPK = userCartPK;
    }

    public UserCart(int userid, int itemid){
        this.userCartPK = new UserCartPK(userid, itemid);
    }

    public UserCartPK getUserCartPK(){
        return userCartPK;
    }

    public void setUserCartPK(UserCartPK userCartPK){
        this.userCartPK = userCartPK;
    }

    public Integer getQty(){
        return qty;
    }

    public void setQty(Integer qty){
        this.qty = qty;
    }

    public UserAccount getUserAccount(){
        return userAccount;
    }

    public void setUserAccount(UserAccount userAccount){
        this.userAccount = userAccount;
    }

    public ItemCatalog getItemCatalog(){
        return itemCatalog;
    }

    public void setItemCatalog(ItemCatalog itemCatalog){
        this.itemCatalog = itemCatalog;
    }

Overrides

    @Override
    public int hashCode(){
        int hash = 0;
        hash += (userCartPK != null ? userCartPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object){
        // TODO: Warning - this method won't work in the case the id fields are not set
        if(!(object instanceof UserCart)){
            return false;
        }
        UserCart other = (UserCart) object;
        if((this.userCartPK == null && other.userCartPK != null) || (this.userCartPK != null && !this.userCartPK.equals(other.userCartPK))){
            return false;
        }
        return true;
    }

    @Override
    public String toString(){
        return "model.entities.UserCart[ userCartPK=" + userCartPK + " ]";
    }
}