N
N
nikitosis2019-05-31 16:06:45
Java
nikitosis, 2019-05-31 16:06:45

How to attach an image to a hibernate product?

I have Image:

@Entity
@Table(name="Image")
public class Image {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name="url",nullable = false)
    private String url;
}

And the Product class:
@Entity
@Table(name="Product")
public class Product {

    @OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval = true)
    @JoinColumn(name="main_img_id")
    private Image mainImage;

}

I need to bind an Image to a Product so that when the image is updated in the Product, the old image is deleted from the database. At the same time, it should be borne in mind that the pictures will be used in the Meal (several photos of the dish, i.e. List) and maybe somewhere else.
How can I delete pictures that are not attached to anything (for example, if we delete a dish, then all the pictures will also be deleted)?
Or should we just create separate ProductImage,MealImage and bind them to the corresponding Product,Meal?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
goshan_p, 2019-06-02
@goshan_p

For me there are several options, but all by hand.
1) When uninstalling a product, delete dependent images by request, first checking if they are stored somewhere else
2) Run a task to "clean" unused images in a separate thread according to the schedule

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question