gpt4 book ai didi

mysql - 使用 Spring Data 传播外键

转载 作者:行者123 更新时间:2023-11-29 19:07:20 26 4
gpt4 key购买 nike

我正在开发一个基于 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> {
}

应用程序属性

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"}
]
}

所有这一切的输出是我在没有外键的情况下保留了一个“producto”和2个“formato”。因此,我相信我无法带来具有其格式的产品

有谁知道为什么外键没有被传播?

最佳答案

当您没有设置反向引用时,通常会发生空 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 方法中。

关于mysql - 使用 Spring Data 传播外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com