首页 > 解决方案 > Hibernate 中的错误:必须在调用 save() 之前手动分配此类的 ID:Model.Product at org.hibernate.id.Assigned.generate

问题描述

我正在使用 Hibernate 在 Spring Boot 中创建一个 CRUD。我有一份带有产品代码、名称和照片属性的产品列表;prodcode 是每次我将单个产品插入数据库时​​手动输入的主键。

我试图在 Postman 上测试代码;当我添加产品时,当我添加主键和照片时,我收到以下错误:

必须在调用 save() 之前手动分配此类的 id:Model.Product at org.hibernate.id.Assigned.generate

我该如何解决?谢谢你们。

产品.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);
    }

    
}

控制器.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);
        
    }
    
}

标签: hibernate

解决方案


推荐阅读