2
I have the following entities generated by jHipster using java with oneToMany relationship:
Product
@Entity
@Table(name = "produto")
public class Produto implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "produto")
@JsonIgnore
private Set<Imagem> imagems = new HashSet<>();
//demais campos
//gets e sets
Imagery
@Entity
@Table(name = "imagem")
public class Imagem implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Lob
@Column(name = "bl_imagem")
private byte[] blImagem;
@Column(name = "bl_imagem_content_type")
private String blImagemContentType;
@ManyToOne
private Produto produto;
//gets e sets
Servico Rest
@RequestMapping(value = "/produtos",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Produto> createProduto(@Valid @RequestBody Produto produto) throws URISyntaxException {
log.debug("REST request to save Produto : {}", produto);
if (produto.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("produto", "idexists", "A new produto cannot already have an ID")).body(null);
}
Produto result = produtoService.save(produto);
return ResponseEntity.created(new URI("/api/produtos/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert("produto", result.getId().toString()))
.body(result);
}
My json on frontend made in Angularjs
Question:
The backend is saving the product in the product table but does not save anything in the picture table, as I could do this oneToMany relationship, being that a product can own multiple images?
You have to set the product in the picture object and save the picture object.
– Sidney
But if I already have the images inside the product, I wanted that when saving the product the images were automatically saved in the table, why not create two post methods.
– Jack
It seems that the problem is in @Requestbody, because when entering the Rest class method the list is empty.
– Jack
I found the problem, is that in json the name of the property is: image and in the backend is: imagems. I changed it and it worked, now the images are saved automatically. But I’m having another problem, it doesn’t save the product field_id in the picture table for each image. Wanted some way this to be done automatically tbm by Hibernate
– Jack
Oxi, if it does not save the product?
– Sidney
If the annotation @Joincolumn(name = "product") it adds without filling the product field_id in the picture table, but in select it does not bring any picture, it just inserts in the post. @Matheus_silva
– Jack
I think you have to define the product in the image object, even if you don’t save the image, just set it in the image and save the product and let Hibernate save both. Because by the looks of it, you must be saving the two objects without making the relationship between them. Bidirectional relationships are like this, you have to inform the two to whom they are linked. It requires more care.
– Sidney