我正在尝试检索具有一个“ coche”的所有“ facturas”。我建立了千丝万缕的关系,因为每个“ factura”在数据库中都有自己的“ idCoche”。我一直在寻找解决此错误的方法,但是没有帖子可以帮助我。
我有这些实体:

FACTURA:

@Entity
@Table(name = "factura")
public class Factura implements Serializable {

private static final long serialVersionUID = 1L;


@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

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

@Column(name = "imagenFactura")
private byte[] imagenFactura;

@ManyToOne
@JoinColumn(name="idCoche")
private Coche coche;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public byte[] getImagenFactura() {
    return imagenFactura;
}

public void setImagenFactura(byte[] imagenFactura) {
    this.imagenFactura = imagenFactura;
}

public Coche getCoche() {
    return coche;
}

public void setCoche(Coche coche) {
    this.coche = coche;
}
}


COCHE:

@Entity
@Table(name = "coche")
public class Coche implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

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

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

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

@OneToMany(mappedBy="coche")
private List<Factura> facturas;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getMatricula() {
    return matricula;
}

public void setMatricula(String matricula) {
    this.matricula = matricula;
}

public String getMarca() {
    return marca;
}

public void setMarca(String marca) {
    this.marca = marca;
}

public String getModelo() {
    return modelo;
}

public void setModelo(String modelo) {
    this.modelo = modelo;
}

public String getTitular() {
    return titular;
}

public void setTitular(String titular) {
    this.titular = titular;
}

public List<Factura> getFacturas() {
    return facturas;
}

public void setFacturas(List<Factura> facturas) {
    this.facturas = facturas;
}
}


这是我的JPA资料库

@Repository
public interface FacturaRepository extends JpaRepository<Factura, Long> {

    List<Factura> findByCocheId(Long id);


}


最后,这是我的资源(调用存储库方法时会生成异常):

public ResponseEntity<List<Factura>> getAllFacturas(@PathVariable Long id) {
    List<Factura> facturas = facturaRepository.findByCocheId(id);
    return new ResponseEntity<>(facturas, HttpStatus.OK);
}


谢谢

最佳答案

像这样修改Factura中的关系:

@ManyToOne
@JoinColumn(name = "id",
      referencedColumnName = "id")
private Coche coche;

08-06 20:42