I'm creating a CRUD in Spring Boot using Hibernate. I have a list of products with prodcode, name and photo attributes; prodcode is the primary key which is entered manually every time I insert a single product into the database.
I was trying to test the code on Postman; when I add a product, then when I add the primary key and the photo I get the following error:
ids for this class must be manually assigned before calling save (): Model.Product at org.hibernate.id.Assigned.generate
How can I fix it? Thank you all.
Product.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import Model.Product;
@Entity
@Table(name="product")
public class Product {
    @Id
    private String prodcode; 
    private String name; 
    
    @Lob
      private byte[] photo;
      public Product() {
      }
      public Product(String prodcode, String name, byte[] photo) {
        this.prodcode = prodcode;
        this.name = name;
        this.photo = photo;
      }
    public String getProdcode() {
        return prodcode;
    }
    public void setProdcode(String prodcode) {
        this.prodcode = prodcode;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public byte[] getPhoto() {
        return photo;
    }
    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
      
      
}Product_DAO_Imp.java
package DAO;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import Model.Product;
@Repository
public class Product_DAO_Imp  implements Product_DAO{
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public boolean saveProduct(Product product) {
        boolean status=false;
        try {
            sessionFactory.getCurrentSession().save(product);
            status=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }
    
}Product_Service_Imp
package Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import DAO.Product_DAO;
import Model.Product;
@Service
@Transactional
public class Product_Service_Imp implements Product_Service {
 
    @Autowired
    private Product_DAO productdao;
    
    @Override
    public boolean saveProduct(MultipartFile file) throws IOException {
        Product product = new Product();
        
        return productdao.saveProduct(product);
    }
    
}Controller.java
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import Model.Product;
import Service.Product_Service;
@RestController
@CrossOrigin(origins="http://localhost:4200")
@RequestMapping(value="/api")
public class Controller {
    
    @Autowired
    private Product_Service productservice;
    
    @PostMapping("save-product")
    public boolean saveProduct(@RequestParam ("file") MultipartFile file) throws IOException {
         return productservice.saveProduct(file);
        
    }
    
}