skip to Main Content

I wonder if anyone got an insights regarding this error I got from postman below:

{
    "timestamp": "2022-12-29T07:59:00.313+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement",
    "path": "/v1/personal/add"
}

Here is My Controller Class:

PersonalController.java

@PostMapping("/add")
public ResponseEntity <PersonalResponseDto> addPersonalInfo (@RequestBody final PersonalRequestDetailsDto personalRequestDetailsDto){
    PersonalResponseDto dto = personalService.addPersonalInformation(personalRequestDetailsDto);
    return ResponseEntity.ok(dto);
}

Here is My Service Class:

PersonalService.java

public PersonalResponseDto addPersonalInformation(PersonalRequestDetailsDto personalRequestDetailsDto){

       PersonalEntity personalEntity = personalMapper.toEntity(personalRequestDetailsDto);

       PersonalEntity saveInfo = personalRepository.save(personalEntity);

       return personalMapper.toDto(saveInfo);

}

and here is my Entity class:

@Data
@ComponentScan("personal")
@NoArgsConstructor
@Table(name = "student_personal_information")
@Entity
public class PersonalEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "lastname", columnDefinition = "CLOB")
    private String lastname;

    @Column(name = "firstname", columnDefinition = "CLOB")
    private String firstname;

    @Column(name = "middleinitial")
    private String middleinitial;

    @Column(name = "age")
    private Integer age;

    @Column(name = "image")
    private Byte image;

}

I’d appreciate any help as to why I’m getting the above error in postman.

Thank you.

I wanted to add an information that would store to DB but i’m getting an error.

Here is my Request DTO:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PersonalRequestDetailsDto implements Serializable {


    private static final long serialVersionUID = -7175623352146349491L;


    @NotEmpty
    private String lastname;

    @NotNull
    private String firstname;

    @NotEmpty
    private String middleinitial;

    @NotNull
    private Integer age;

}

Addendum:
Upon the advice of one commenter here, I learned that the main issue herein is my entity under column image.

Here is the error I got:

column "image" is of type bytea but expression is of type smallint Hint: You will need to rewrite or cast the expression.

To ask, what data type exactly should I declare in my entity to address this issue?

3

Answers


  1. Idealy, you should save only path to your image in your database, after you have uploaded the file to a separate image storage.

    Make the image type String.

    Login or Signup to reply.
  2. I normally save images as files and save the details in the DB. However if you still want to save it in the DB then you can change as follows:

    @Lob
    @Column(name = "image")
    private byte[] image;
    
    Login or Signup to reply.
  3. Adding to @Tonykay’s answer, you can save your images/media files to resources like Google cloud storage or S3, you can then persist the URL into your database.

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