显示与产品相关的所有图片

问题描述 投票:0回答:1

这是我的产品类别:

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.EAGER, 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;

这是我的产品图片类:

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;

目前,当使用 swagger 测试端点(获取所有产品)时,我得到的是一个空的产品图像数组。

服务:

  @Transactional(readOnly = true)
    public Page<Product> findAll(Pageable pageable) {
        return productRepository.findAll(pageable);
    }

可以合理地认为,如果产品缺少相应的 ID,则可能无法接收图像,这意味着产品实体不知道图像关联。但是,可以在不改变实体之间的关系的情况下检索响应中的图像。有什么解决办法吗?

java spring spring-boot spring-data-jpa jhipster
1个回答
0
投票
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    @EntityGraph(attributePaths = "productImages")
    List<Product> findAll();
}
© www.soinside.com 2019 - 2024. All rights reserved.