3

I'm trying to send the following JSON to a REST API and persist on database, but only the Product is created, the Image it is not.

{"name":"pen", "description":"red pen", "images":[{"type":"jpeg"}] }

@Controller

@POST
@Path("/product/add")
@Consumes("application/json")
public Response addProduct(Product product) {
    service.createProduct(product);
}

@Service

@Autowired
private ProductDAO productDAO;

@Autowired
private ImageDAO imageDAO;

public void createProduct(Product product) {
    productDAO.save(product);
}

@Product

@Entity
@Table
public class Product implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer productId;

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

@Column(name = "DESCRIPTION")
private String description;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="product")
private Set<Image> images;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="parent")
private Set<Product> children;

@ManyToOne
@JoinColumn(name = "PARENT_PRODUCT_ID")
private Product parent;  

@Image

@Entity
@Table
public class Image implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer imageId;

@Column(name = "TYPE")
private String type;

@ManyToOne
@JoinColumn(name = "PRODUCT_ID", nullable = false)
private Product product;

At the @POST method, when print the Product object received, this is what returns:

Product [productId=null, name=pen, description=red pen, images=[Image [id=null, type=jpeg, product=null]], children=null, parent=null]

The correct way is to first persist the Product, and then persist the Image or the Hibernate can automatically persist the Image when I persist the Product?

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
Pedro Grandi
  • 65
  • 1
  • 10

1 Answers1

0

Hibernate takes care of persisting your child entities if your bidirectional mapping is correctly implemented and you have set proper relationships between your entity objects.

You have a Product entity that has a collection of Image. Product entity is the parent entity here. You can simply set proper relations between Product and Image entities and persist only Product. Hibernate will persist your parent as well as your child entities.

What you need to do

Product product = new Product();
product.setName("PRODUCT_NAME");

Set<Image> productImages = new HashSet<>();

Image productProfileImage = new Image();
productProfileImage.setType("PROFILE");
productProfileImage.setProduct(product);
//..set other fields
productImages.add(productProfileImage);

Image productCoverImage = new Image();
productCoverImage.setType("COVER");
productCoverImage.setProduct(product);
//..set other fields
productImages.add(productCoverImage);

product.setImages(productImages);
productRepository.save(product);  //Persist only your product entity and the  mapped child entities will be persisted 

Check out this similar answer.

PS: I have not tested the code but this should work.

Community
  • 1
  • 1
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • I would suggest to test your code before submitting it here. It can always be the case, that you missed something and others may be confused about it. For that reason, I downvoted the answer. – alexander May 31 '20 at 14:45
  • @alexander Brother this is a platform to help others and not post throughly tested pieces of code. There might be situations where through testing is not possible or very cumbersome. – Abdullah Khan May 31 '20 at 15:05
  • And this is not a platform to be called brother. I will not withdraw my opinion, I saw so many answers which are not tested and which does not work. – alexander May 31 '20 at 19:44
  • @alexander Ok Sir – Abdullah Khan Jun 01 '20 at 12:13