1

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 = "item_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "native")
    private int itemId;
    @Column(name = "item_name", nullable = false)
    private String itemName;
    private char status;
    private String description;
    @OneToMany(mappedBy = "itemDetails")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST})
    private List<ItemPackingDetails> itemPackingDetails;
    @Column(name = "customer_allowed")
    private String customerAllowed;
    @ManyToMany(mappedBy = "itemDetails")
    private Set<Category> 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 &&
                status == that.status &&
                Objects.equals(itemName, that.itemName);
    }


    @Override
    public String toString() {
        return "ItemDetails{" +
                "itemId=" + itemId +
                ", itemName='" + itemName + '\'' +
                ", status=" + status +
                ", description='" + description + '\'' +
                ", itemPackingDetails=" + itemPackingDetails +
                ", customerAllowed='" + 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;

}

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 "UTF8": 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.

mybrave
  • 1,662
  • 3
  • 20
  • 37
  • You posted the query: `insert into packing_details (item_id, size, status, id) values (?, ?, ?, ?)` but not actual parameters. Add logging to have them displayed, that way you'll know which field is incorrect. See also https://stackoverflow.com/questions/1347646/postgres-error-on-insert-error-invalid-byte-sequence-for-encoding-utf8-0x0 – Lesiak Apr 06 '20 at 07:36
  • I am actually adding new ItemPackingDetails. The function parameter itemDetails comes from a REST API. I am setting the status of the ItemPackingDetails attached to function parameter as y and then adding it to existing ItemDetails (retrieved using foundDetails) through the following line: foundItemDetails.getItemPackingDetails().add(itemPackingDetails); – Anish Goyal Apr 06 '20 at 07:37
  • can you add the ddl of packing_details table? look at https://stackoverflow.com/questions/1347646/postgres-error-on-insert-error-invalid-byte-sequence-for-encoding-utf8-0x0? – pero_hero Apr 06 '20 at 07:39
  • @Lesiak I can see that the status field which I set to y and the size field is going as empty in the database. Cannot really see why this is happening. The logger.error line correctly prints the values. – Anish Goyal Apr 06 '20 at 07:49
  • And what is the value of size before saving? And also type of this column in the db? – Lesiak Apr 06 '20 at 08:17
  • @Lesiak size is 20 and type is int. – Anish Goyal Apr 06 '20 at 08:32

0 Answers0