skip to Main Content

I’m attempting to send a post request with an image attachment and employee details in json format. For ImageData and Employee, I have distinct tables. I am able to store image data with my code, but not employee data.The error message "Could not commit JPA transaction" is displayed.
Here I am sharing my code details

#Post Request
@PostMapping("/create")
    public Employee tempCreateEmployee(
            @RequestParam("json") String json,
            @RequestParam("file") MultipartFile file
    ) throws CustomException {
        try {

            ObjectMapper objectMapper = new ObjectMapper();
            logger.info("data received as string");

            Employee employee = objectMapper.readValue(json, Employee.class);
            logger.info(employee.toString());

            logger.info("Received employee data: {}", employee);
            
            int imageId=fileService.uploadImageFile(file);
            logger.info("file processing successful");
            
            employee.setImageid(imageId);
            logger.info(employee.toString());
           
            return employeeService.save(employee);
        } catch (Exception ex) {
           
            throw new CustomException("Employee object not created");
        }
    }

Service method for uploading image

public int uploadImageFile(MultipartFile file) throws IOException {
        ImageData imageData= filesRepo.save(ImageData.builder()
                .name(file.getOriginalFilename())
                .type(file.getContentType())
                .id(filesRepo.findAll().size()+1)
                .image_filedata(ImageUtils.compressImage(file.getBytes())).build());
        
       
        return imageData.getId();

    }

Employee Model class:

package com.SpringTask.SpringTask.Models;

import com.SpringTask.SpringTask.Encryption.Encrypt;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import jdk.jfr.DataAmount;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;


import lombok.*;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Setter
@Getter
@Data
@Entity
@Table(name="tableEmployees")

public class Employee {
    @Id
    @GeneratedValue
    public Integer id;


    public String firstName;

  
    public String lastName;

    
    public String address;

    
    public String email;


    
    public int phoneNumber;

  

    private int imageid;

   }

ImageData model class:

package com.SpringTask.SpringTask.Models;

import jakarta.persistence.*;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.*;
import lombok.NoArgsConstructor;
import org.hibernate.engine.internal.Cascade;

import java.util.Arrays;


@Entity
@Table(name = "tableImageData")
@Builder(toBuilder = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter

public class ImageData {
    @Id
    private int id;

    private String name;
    private String type;

    @Lob
    @Column(name = "image_filedata", columnDefinition = "LONGBLOB")
    private byte[] image_filedata;
    
}

enter image description here

I’ve tried debugging , but every time same issue appears. I have verified my database setup and code logic.
Could not commit JPA transaction means?

2

Answers


  1. enter image description here

    When sending a request in your postman, you can try to send your data via raw and json model in Body.

    Login or Signup to reply.
  2. You need to share your JSON data and EmployeeService class to understand the issue better.

    From the image you shared, I found out one issue:

    You give id for creating an employee but in the Employee model, it is generated by Hibernate automatically in your model by specifying this: @GeneratedValue. So it is wrong, you should not add id field in your JSON data. That might be the reason for getting the error.

    If you want to give id externally, then you need to remove @GeneratedValue annotation in Employee class.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search