Projeto CRUD
Product Model:
package com.example.springboot.models;
import jakarta.persistence.*;
import org.springframework.hateoas.RepresentationModel;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.UUID;
@Entity
@Table(name = "TB_PRODUCTS")
public class ProductModel extends RepresentationModel<ProductModel> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID idProduct;
private String name;
private BigDecimal value;
@ManyToOne
@JoinColumn(name = "brand_id", referencedColumnName = "idBrand")
private BrandModel brand;
public UUID getIdProduct() {
return idProduct;
}
public void setIdProduct(UUID idProduct) {
this.idProduct = idProduct;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public BrandModel getBrand() {
return brand;
}
public void setBrand(BrandModel brand) {
this.brand = brand;
}
}
Brand Model:
import jakarta.persistence.*;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.UUID;
@Entity
@Table(name = "TB_BRANDS")
public class BrandModel implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID idBrand;
private String name;
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProductModel> products;
public BrandModel() {
}
public BrandModel(String name) {
this.name = name;
}
public UUID getIdBrand() {
return idBrand;
}
public void setIdBrand(UUID idBrand) {
this.idBrand = idBrand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Tudo está funcionando, apenas a tabela que relaciona id da marca e id do produto nao está cadastrando nada. Como posso resolver? Acredito q utilizei a anotacao @OneToMany ou @ManyToOne de forma errada.