30 December 2019

JPA: Composite Primary Key with Foreign Keys



 
Entity for the ProductSales Table:

package com.example.model;

import javax.persistence.*;

@Entity
@IdClass(ProductSalesPK.class)
public class ProductSales {

 @Column(name = "QUANTITY")
 private int quantity_sold;

 @Id
 @ManyToOne
 @JoinColumn(name = "sales_id")
 private Sales sales;

 @Id
 @ManyToOne
 @JoinColumn(name = "product_id")
 private Product product;

 public ProductSales() {
 }

 public ProductSales(Sales sales, Product product, int quantity_sold) {
  this.sales = sales;
  this.product = product;
  this.quantity_sold = quantity_sold;
 }

 public Product getProduct() {
  return product;
 }

 public Sales getSales() {
  return sales;
 }

 public void setProduct(Product product) {
  this.product = product;
 }

 public void setSales(Sales sales) {
  this.sales = sales;
 }

 public int getQuantity_sold() {
  return quantity_sold;
 }

 public void setQuantity_sold(int quantity_sold) {
  this.quantity_sold = quantity_sold;
 }
}

ProductSalesPK Id class:

package com.example.model;

import java.util.Objects;

public class ProductSalesPK {
    private int sales;
    private String product;

 public ProductSalesPK(int sales_id, String product_id) {
  this.sales = sales_id;
  this.product = product_id;
 }

 @Override
 public int hashCode() {
  int hash = 5;
  hash = 83 * hash + this.sales;
  hash = 83 * hash + Objects.hashCode(this.product);
  return hash;
 }

 @Override
 public boolean equals(Object obj) {
  if (obj == null) {
   return false;
  }
  if (getClass() != obj.getClass()) {
   return false;
  }
  final ProductSalesPK other = (ProductSalesPK) obj;
  if (this.sales != other.sales) {
   return false;
  }
  if (!Objects.equals(this.product, other.product)) {
   return false;
  }
  return true;
 } 
}
Entity class fpor Product:

package com.example.model;
package com.example.model;

import java.util.Objects;
import javax.persistence.*;
import javax.validation.constraints.*;
@Entity
public class Product {
 @NotNull(message="product id not null") @Size (min = 1,message="product id not empty") 
 @Id
    private String product_id; 
 @NotNull(message="product name not null") @Size (min = 10,message="product name length at least 10")
    private String prod_name;
 @Min(value=5, message="price >= {value}")
    private double price;
    private String prod_desc;

 public Product() {
 }

  
    public Product(String id, String name, double price, String description) {
        this.product_id = id;
        this.prod_name = name;
        this.price = price;
        this.prod_desc = description;
    }

    public String getId() {
        return product_id;
    }

    public String getName() {
        return prod_name;
    }

    public void setName(String name) {
        this.prod_name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return prod_desc;
    }

    public void setDescription(String description) {
        this.prod_desc = description;
    }
    
    @Override
    public String toString() {
        String s = String.format("Product id: %s\n"
        + "Name: %s\n"
        + "Description: %s\n"
        + "Price: $%.2f", product_id, prod_name, prod_desc, price);
        return s;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + Objects.hashCode(this.product_id);
        hash = 97 * hash + Objects.hashCode(this.prod_name);
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.price) ^ (Double.doubleToLongBits(this.price) >>> 32));
        hash = 97 * hash + Objects.hashCode(this.prod_desc);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (!Objects.equals(this.product_id, other.product_id)) {
            return false;
        }
        if (!Objects.equals(this.prod_name, other.prod_name)) {
            return false;
        }
        if (Double.doubleToLongBits(this.price) != Double.doubleToLongBits(other.price)) {
            return false;
        }
        if (!Objects.equals(this.prod_desc, other.prod_desc)) {
            return false;
        }
        return true;
    }
    
}

6 comments:

  1. thank you, you have saved the life of this humble servant

    ReplyDelete
  2. Thank you.. what is the entity class for product

    ReplyDelete
  3. added entity class for product

    ReplyDelete
  4. thank you, such a life saver

    ReplyDelete
  5. is necesary the hashCode and the equals method?

    ReplyDelete
    Replies
    1. Not necessary, but recommended. It is suffifcient to base them on the keys (I really should update the code for this)

      Delete