我正在开发一个基于Spring Data Rest(下面使用Hibernate)和mySQL数据库的Spring Boot应用程序。这个应用程序无法填充引用条目的外键(因为我希望Hibernate为我做这件事)。 实体:
@Entity
public class Producto {
private Integer id;
private String nombre;
private List<Formato> listaFormatos;
public Producto() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@OneToMany(mappedBy = "producto", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<Formato> getListaFormatos() {
return listaFormatos;
}
public void setListaFormatos(List<Formato> listaFormatos) {
this.listaFormatos = listaFormatos;
}
}
@Entity
public class Formato {
private Integer id;
private Integer cantidad;
private String unidadMedida;
private Producto producto;
public Formato() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "producto_id", referencedColumnName = "id")
public Producto getProducto() {
return producto;
}
public void setProducto(Producto producto) {
this.producto = producto;
}
public Integer getCantidad() {
return cantidad;
}
public void setCantidad(Integer cantidad) {
this.cantidad = cantidad;
}
public String getUnidadMedida() {
return unidadMedida;
}
public void setUnidadMedida(String unidadMedida) {
this.unidadMedida = unidadMedida;
}
}
存储库:
public interface ProductoRepository extends CrudRepository<Producto, Integer> {
}
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/x1
spring.datasource.username = x2
spring.datasource.password = x3
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=create-drop
的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
让我们说我以JSON格式发送的请求如下所示:
{"nombre": "x",
"listaFormatos": [
{"cantidad": 1,
"unidadMedida":"kg"},
{"cantidad": 2,
"unidadMedida":"g"}
]
}
所有这一切的结果是我坚持产品&#39;和2&#39; formato&#39;没有外键。因此,我相信我不能带有格式的产品
有谁知道为什么外键没有被传播?
答案 0 :(得分:0)
当你没有设置反向引用时,通常会发生空FK。
请考虑以下事项:
Parent p = new Parent();
...
Child c = new Child();
...
p.setChild(c);
c.setParent(p); // this is the line you are probably missing
当然,您也可以将此逻辑放入Parent#setChild
方法中。