I'm writing a backend with Spring Boot and PostgreSQL. Basically i need to save images as base64 string in postgres and retrieve them. Response from POST method is ok, but when i try use GET spring throws this exception in the end of the image string:
Could not write JSON: Unable to access lob stream; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unable to access lob stream (through reference chain: java.util.ArrayList[0]->ru.mangaone.mangaone.domain.Manga[\\\"chapters\\\"]->org.hibernate.collection.internal.PersistentBag[0]->ru.mangaone.mangaone.domain.Chapter[\\\"pages\\\"])
And this as root cause:
org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
Entities:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Manga {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String author;
private String artist;
private String year;
private int views;
private double rating;
@Lob
private String image;
@Enumerated(EnumType.STRING)
private Status status;
@ElementCollection(targetClass = Genre.class, fetch = FetchType.EAGER)
@CollectionTable(name = "genres", joinColumns = @JoinColumn(name = "manga_id"))
@Enumerated(EnumType.STRING)
private List<Genre> genres;
@OneToMany(mappedBy = "manga", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Chapter> chapters;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Chapter {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "manga_id")
@JsonIgnoreProperties("chapters")
private Manga manga;
private int number;
private String name;
@OneToMany(targetEntity = Page.class, mappedBy = "chapter",
cascade = CascadeType.ALL, orphanRemoval = true)
private List<Page> pages;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "chapter_id")
@JsonIgnoreProperties("pages")
private Chapter chapter;
private int number;
@Lob
private String image;
}
POST methods for chapter and page:
@PostMapping()
public Chapter createChapter(@RequestBody Manga manga) {
List<Chapter> chapters = manga.getChapters();
Chapter result = chapterRepository.save(chapters.get(chapters.size() - 1));
mangaRepository.save(manga);
return result;
}
@PostMapping(value = "/pages")
public List<Page> createPages(@RequestBody List<Page> pages) {
return pageRepository.saveAll(pages);
}
GET methods for manga:
@GetMapping
public List<Manga> listManga() {
return mangaRepository.findAll();
}
@GetMapping("{id}")
public Manga getManga(@PathVariable("id") Manga manga) {
return manga;
}