So this is my Product class :
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@NotNull
@Size(max = 100)
@Column(name = "title", length = 100, nullable = false)
private String title;
@NotNull
@DecimalMin(value = "0")
@DecimalMax(value = "99999")
@Column(name = "day_price", precision = 21, scale = 2, nullable = false)
private BigDecimal dayPrice;
@Lob
@Column(name = "description", nullable = false)
private String description;
@NotNull
@Column(name = "position", nullable = false)
private String position;
@Min(value = 0L)
@Max(value = 100L)
@Column(name = "week_discount")
private Long weekDiscount;
@Min(value = 0L)
@Max(value = 100L)
@Column(name = "month_discount")
private Long monthDiscount;
@Min(value = 0L)
@Column(name = "refundable_deposit")
private Long refundableDeposit;
@Column(name = "enabled")
private Boolean enabled;
@Column(name = "deleted")
private Boolean deleted;
@Column(name = "created_at")
private Instant createdAt;
@Column(name = "updated_at")
private Instant updatedAt;
@Column(name = "deleted_at")
private LocalDate deletedAt;
@Enumerated(EnumType.STRING)
@Column(name = "currency")
private Currency currency;
@OneToMany(mappedBy = "product")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.springframework.data.annotation.Transient
@JsonIgnoreProperties(value = { "productBooking", "user", "invoice", "product" }, allowSetters = true)
private Set<Booking> bookings = new HashSet<>();
@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Fetch(value = FetchMode.SELECT) // don't remove this line
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@org.springframework.data.annotation.Transient
@JsonIgnoreProperties(value = { "product" }, allowSetters = true)
@BatchSize(size = 20)
private Set<ProductImages> productImages = new HashSet<>();
@OneToMany(mappedBy = "product")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.springframework.data.annotation.Transient
@JsonIgnoreProperties(value = { "user", "product" }, allowSetters = true)
private Set<ProductReview> productReviews = new HashSet<>();
@ManyToOne(optional = false)
@NotNull
private User owner;
@ManyToOne(optional = false)
@NotNull
@JsonIgnoreProperties(value = { "products", "productBookings", "productCategories", "parentId" }, allowSetters = true)
private ProductCategory productCategory;
and this is my product image class :
public class ProductImages implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Lob
@Column(name = "image", nullable = false)
private byte[] image;
@NotNull
@Column(name = "image_content_type", nullable = false)
private String imageContentType;
@Column(name = "description")
private String description;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
@NotNull
@JsonIgnoreProperties(value = { "bookings", "productImages", "productReviews", "owner", "productCategory" }, allowSetters = true)
private Product product;
For now when using swagger to test the endpoint (Get all Products) i am getting an empty array of product images.
Service :
@Transactional(readOnly = true)
public Page<Product> findAll(Pageable pageable) {
return productRepository.findAll(pageable);
}
It is logical to think that the product might not be receiving the images if it lacks their corresponding IDs, meaning the product entity isn't aware of the image associations. However, it might be possible to retrieve the images in the response without altering the relationships between entities. any solution ?
@EntityGraph(attributePaths = "item") is used to specify the eager fetching strategy for the productImages relationship when retrieving Product entities from the database using the findAll() method. So in our case, the productImages property will be loaded eagerly, even though the default fetch strategy of the @OneToMany annotation is lazy.
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
@EntityGraph(attributePaths = "productImages")
List<Product> findAll();
}