hibernate - 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);
}
}
解决方案
推荐阅读
- html - 如何缩小“react-image-gallery”中图像的大小?
- python - 通过 s3_additional_kwargs 将 Pandas Dataframe 加载到 S3
- python - Python 返回文件中包含太多或没有足够选项卡的行
- javascript - 2 个范围(例如 100 和 150)之间的随机 30 个数字(浮点数),总和为 N
- reactjs - 如何在 React 中将道具传递给 HOC
- command-line-interface - 是否有一个不可知的 cli 可以快速生成新功能和项目?
- vhdl - 在 VHDL 元编程中强制字符串作为集合的成员
- php - 创建新数组仅用于从表中打印选中的行
- reactjs - 如何在服务器 SSR 上渲染的 Create React App 中添加对 Css 模块和 Sass 的支持
- c# - Ghostscript.NET.Rasterizer 设置分辨率 (Dpi) 不起作用