在Spring Boot中更新OneToMany关系时出错。

huangapple 未分类评论54阅读模式
英文:

Error updating OneToMany relation in Spring Boot

问题

Allowed='" + customerAllowed + '\'' +
                ", categories=" + categories +
                ", image='" + image + '\'' +
                '}';
    }
}
  1. ItemPackingDetails
package com.app.shop.entity;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name = "packing_details")
public class ItemPackingDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "native")
    private int id;
    private int size;
    private char status;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "item_id")
    private ItemDetails itemDetails;

}

我编写了一个函数,将新的ItemPackingDetails对象添加到数据库中已存在的ItemDetails对象中。我正在使用PostgreSQL。

public HashMap<String, Object> addPacking(ItemDetails itemDetails){
    returnObject = new HashMap<>();
    Optional<ItemDetails> optional = productManagementRepository.findById(itemDetails.getItemId());
    ItemDetails foundItemDetails = null;
    if (optional.isPresent()){
        foundItemDetails = optional.get();
    }
    if (foundItemDetails!=null){
        for (ItemPackingDetails itemPackingDetails : itemDetails.getItemPackingDetails()) {
            itemPackingDetails.setStatus('y');
            itemPackingDetails.setItemDetails(foundItemDetails);
            foundItemDetails.getItemPackingDetails().add(itemPackingDetails);
            logger.error("Packing Details: " + itemPackingDetails.getSize() + " " + itemPackingDetails.getStatus() + " " + itemPackingDetails.getItemDetails().getItemId());
        }
        productManagementRepository.save(foundItemDetails);
        returnObject.put("message", "success");
    }
    else
        returnObject.put("message", "failure" + itemDetails.getItemId());
    return returnObject;
}

在运行查询insert into packing_details (item_id, size, status, id) values (?, ?, ?, ?)时,我收到以下错误:org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00

请帮忙,因为我没有将任何空值传递给对象。在控制台日志中,我可以正确看到ItemPackingDetails对象中的所有值。


<details>
<summary>英文:</summary>

I am using Spring Boot 2 and JPA/Hibernate. 

I have 2 entitites:

1. ItemDetails

package com.app.shop.entity;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
@Entity
@Table(name = "item_mst")
@Cacheable
public class ItemDetails {

@Id
@Column(name = &quot;item_id&quot;)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;native&quot;)
private int itemId;
@Column(name = &quot;item_name&quot;, nullable = false)
private String itemName;
private char status;
private String description;
@OneToMany(mappedBy = &quot;itemDetails&quot;)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST})
private List&lt;ItemPackingDetails&gt; itemPackingDetails;
@Column(name = &quot;customer_allowed&quot;)
private String customerAllowed;
@ManyToMany(mappedBy = &quot;itemDetails&quot;)
private Set&lt;Category&gt; categories;
String image;

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    ItemDetails that = (ItemDetails) o;
    return itemId == that.itemId &amp;&amp;
            status == that.status &amp;&amp;
            Objects.equals(itemName, that.itemName);
}


@Override
public String toString() {
    return &quot;ItemDetails{&quot; +
            &quot;itemId=&quot; + itemId +
            &quot;, itemName=&#39;&quot; + itemName + &#39;\&#39;&#39; +
            &quot;, status=&quot; + status +
            &quot;, description=&#39;&quot; + description + &#39;\&#39;&#39; +
            &quot;, itemPackingDetails=&quot; + itemPackingDetails +
            &quot;, customerAllowed=&#39;&quot; + customerAllowed + &#39;\&#39;&#39; +
            &quot;, categories=&quot; + categories +
            &quot;, image=&#39;&quot; + image + &#39;\&#39;&#39; +
            &#39;}&#39;;
}

}


2. ItemPackingDetails

package com.app.shop.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name = "packing_details")
public class ItemPackingDetails {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;native&quot;)
private int id;
private int size;
private char status;
@JsonIgnore
@ManyToOne
@JoinColumn(name = &quot;item_id&quot;)
private ItemDetails itemDetails;

}


I have written a function to add a new ItemPackingDetails object to already existing ItemDetails object in the db. I am using PostgreSQL.

public HashMap<String, Object> addPacking(ItemDetails itemDetails){
returnObject = new HashMap<>();
Optional<ItemDetails> optional = productManagementRepository.findById(itemDetails.getItemId());
ItemDetails foundItemDetails = null;
if (optional.isPresent()){
foundItemDetails = optional.get();
}
if (foundItemDetails!=null){
for (ItemPackingDetails itemPackingDetails : itemDetails.getItemPackingDetails()) {
itemPackingDetails.setStatus('y');
itemPackingDetails.setItemDetails(foundItemDetails);
foundItemDetails.getItemPackingDetails().add(itemPackingDetails);
logger.error("Packing Details: " + itemPackingDetails.getSize() + " " + itemPackingDetails.getStatus() + " " + itemPackingDetails.getItemDetails().getItemId());
}
productManagementRepository.save(foundItemDetails);
returnObject.put("message", "success");
}
else
returnObject.put("message", "failure" + itemDetails.getItemId());
return returnObject;
}


I get the following error: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding &quot;UTF8&quot;: 0x00
while running the query ```insert into packing_details (item_id, size, status, id) values (?, ?, ?, ?)```

Please help as I am not passing any null values to the object. On console logging I can see all the values correctly in the ItemPackingDetails object.

</details>


huangapple
  • 本文由 发表于 2020年4月6日 15:20:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61054767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定