-1
imageという名前のエンティティに画像ファイルを添付しようとしています。画像と一緒に製品を作ることはできますが、画像を削除しようとすると次のようなエラーが発生します。親クラスの子エンティティをセットから削除する
HTTP 500 - 要求処理に失敗しました。ネストされた例外はorg.hibernate.PersistentObjectExceptionです:永続化するために渡されたデタッチエンティティ:com.IJM.model.Product
製品のクラス
@Entity
@Table(name = "Product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private Long id;
@NotNull
@Size(min = 3, max = 10)
@Column(name = "Code", nullable = false)
private String code;
@NotNull
@Size(min = 3, max = 50)
@Column(name = "Name", nullable = false)
private String name;
@Size(max = 50)
@Column(name = "Description", nullable = false)
private String description;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "Id_Category")
private Category category;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "Id_Unit")
private Unit unit;
@OneToMany(cascade = CascadeType.ALL,
fetch= FetchType.EAGER,
orphanRemoval = true,
mappedBy="product")
private Set<Image> images;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
if(this.category==null||!this.category.equals(category))
{
this.category=category;
}
return;
}
public Unit getUnit() {
return unit;
}
public void setUnit(Unit unit) {
if(this.unit==null||!this.unit.equals(unit))
{
this.unit=unit;
}
return;
}
public Set<Image> getImages() {
return images;
}
public void setImages(Set<Image> images) {
this.images = images;
}
}
Imageクラス
@Entity
@Table(name = "product_Image")
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", nullable = false)
private long id;
@Lob
@Column(name = "File", nullable = false)
private byte[] file;
@Column(name = "Checksum",nullable = false)
private String checksum;
@Column(name = "Extension",nullable = false)
private String extension;
@Column(name = "File_Name",nullable = false)
private String file_name;
@Column(name = "Size",nullable = false)
private int size;
@Column(name = "Last_Updated",nullable = false)
private Timestamp last_Updated;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="Id_Product")
private Product product;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="Id_Directory")
private Directory directory;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
public String getChecksum() {
return checksum;
}
public void setChecksum(String checksum) {
this.checksum = checksum;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getFile_name() {
return file_name;
}
public void setFile_name(String file_name) {
this.file_name = file_name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Timestamp getLast_Updated() {
return last_Updated;
}
public void setLast_Updated(Timestamp last_Updated) {
this.last_Updated = last_Updated;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Directory getDirectory() {
return directory;
}
public void setDirectory(Directory directory) {
this.directory = directory;
}
そして、これがためのコードであります削除方法を呼び出すコントローラー
@RestController
@RequestMapping("/image")
public class ImageController {
@Autowired
ProductService productService;
@Autowired
ImageService imageService;
private static final String productImagePath="C:\\IJM\\Images\\Product\\";
@RequestMapping(value = "/product/{code}", method = RequestMethod.DELETE)
public ResponseEntity<Image> deleteProductImage(@PathVariable("code") String code) {
System.out.println("Fetching & Deleting Image for product " + code);
code = code.toUpperCase();
if (!productService.isProductExist(code)) {
System.out.println("Product with code " + code + " not found");
return new ResponseEntity<Image>(HttpStatus.NOT_FOUND);
}
else
{
Product product = productService.findProductByCode(code);
if(product.getImages()!=null)
{
for(Image image:product.getImages())
{
product.getImages().remove(image);
image.setProduct(null);
imageService.deleteImage(image.getId());
}
productService.saveProduct(product);
try{
File file = new File(productImagePath+code);
if(FileDeleter.removeDirectory(file)){
System.out.println(file.getName() + " is deleted!");
}else{
System.out.println("Delete operation is failed.");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
return new ResponseEntity<Image>(HttpStatus.NO_CONTENT);
}
}
他の人が不思議に思っている場合サービスは、ちょうどこれはこれはこれらの行が適切な順序になっていないようだ私の抽象DAOクラスのコード
public void delete(T entity) {
getSession().delete(entity);
}
:それは私のコードに違いはありませんでした..私は何かが間違って取得するための詳細を投稿しました。 – user3676579
deleteImage呼び出しを削除してみましたか?取得したエラーは、すでに削除したイメージエンティティによるものです。JPAの場合は、saveProductの実装は、休止状態の場合はsaveOrUpdate(製品)、JPAの場合はマージ(製品)である必要があります。 –
Lol ..ええ、それは更新でなければならなかった。それは、申し訳ありません、私はそれが私の部分からダムだったと思う!あなたの時間と知識にとても感謝しています! – user3676579