Java Crudepository(spring boot)中的自定义方法存在问题
我正在尝试使用spring boot为一所小型驾驶学校制作一个web应用程序。此时,我正在尝试创建使用外键的表:一个学生将有多个评估,因此我正在进行1:N关联。问题是,当我尝试运行项目进行测试时,我得到了这个错误(只想说明原因): 我做了一个自定义方法来获取一个学生的所有评价,但由于某些原因,它不起作用。我查看了数据库,查看列是否实际创建,这是我看到的代码:Java Crudepository(spring boot)中的自定义方法存在问题,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我正在尝试使用spring boot为一所小型驾驶学校制作一个web应用程序。此时,我正在尝试创建使用外键的表:一个学生将有多个评估,因此我正在进行1:N关联。问题是,当我尝试运行项目进行测试时,我得到了这个错误(只想说明原因): 我做了一个自定义方法来获取一个学生的所有评价,但由于某些原因,它不起作用。我查看了数据库,查看列是否实际创建,这是我看到的代码: -- Table: public.resultados_psicotecnicos -- DROP TABLE public.resul
-- Table: public.resultados_psicotecnicos
-- DROP TABLE public.resultados_psicotecnicos;
CREATE TABLE public.resultados_psicotecnicos
(
id bigint NOT NULL,
fecha_evaluacion character varying(255) COLLATE pg_catalog."default",
nota1 character varying(255) COLLATE pg_catalog."default",
nota2 character varying(255) COLLATE pg_catalog."default",
nota3 character varying(255) COLLATE pg_catalog."default",
nota4 character varying(255) COLLATE pg_catalog."default",
rut bigint NOT NULL,
CONSTRAINT resultados_psicotecnicos_pkey PRIMARY KEY (id),
CONSTRAINT fkf57g1daekqxergtw9kck81esq FOREIGN KEY (rut)
REFERENCES public.students (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE public.resultados_psicotecnicos
OWNER to postgres;
我对这个框架有点陌生,所以可能错误很明显,但我看不到。现在我将展示类和存储库
这是本项目的结构:
应用程序属性
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.show-sql=true
spring.datasource.driver-class-name=org.postgresql.Driver
## Hibernate Propierties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL94Dialect
# Hibernate auto ddls
spring.jpa.hibernate.ddl-auto = create
Student.java
package net.BBB.ProjectB.entity;
import java.sql.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "nombre")
private String nombre;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "nacionalidad")
private String nacionalidad;
@NotNull(message = "Este campo es obligatorio")
@Column(name = "edad")
private long edad;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "rut")
private String rut;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "sexo")
private String sexo;
@NotNull(message = "Este campo es obligatorio")
@Column(name = "fecha_nacimiento")
private Date fecha_nacimiento;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "domicilio")
private String domicilio;
@NotBlank(message = "Este campo es obligatorio")
@Column(name = "email")
private String email;
@NotNull(message = "Este campo es obligatorio")
@Column(name = "telefono")
private long telefono;
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
private Set<ResultadosPsicotecnicos> resultadosPsicotecnicos;
/* constructors, getters and setters ommited */
resultadospicotecnicosrepository.java
package net.BBB.ProjectB.repository;
import java.util.List;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import net.BBB.ProjectB.entity.ResultadosPsicotecnicos;
@Repository
public interface ResultadosPsicotecnicosRepository extends CrudRepository<ResultadosPsicotecnicos, Long> {
@EntityGraph(value = "Student.resultadosPsicotecnicos", type = EntityGraphType.FETCH)
List<ResultadosPsicotecnicos> findByRut(long rut);
}
package net.BBB.ProjectB.repository;
导入java.util.List;
导入org.springframework.data.jpa.repository.EntityGraph;
导入org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
导入net.BBB.ProjectB.entity.resultadospicotecnicos;
@存储库
公共接口结果ospsicotecnicosepository扩展了crudepository{
@EntityGraph(value=“Student.resultadospicotecnicos”,type=EntityGraphType.FETCH)
列表findByRut(长车辙);
}
最后是导致错误的自定义方法。我现在将使用学生的id
,稍后如果我解决了这个问题,我将使用rut
,但是现在外键的列称为rut
,它使用学生的id
欢迎提供任何帮助。实际上,JPA正在尝试在ResultadosPsicotecnicos中查找列rut,正如您在ResultadOSpicoteCnicosRespository中指定的findByRut
@NamedEntityGraph(name=“Student.resultadoscsicotecnicos”,attributeNodes={@NamedAttributeNode(“resultadoscsicotecnicos”)})忘记回答:这是运行服务的诀窍,非常感谢。从Resultados_Psicotecnicos表获取数据时遇到一些问题,但这是另一回事。
package net.BBB.ProjectB.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "Resultados_Psicotecnicos")
public class ResultadosPsicotecnicos {
@Id
@Column(name = "id")
private long id;
@ManyToOne
@JoinColumn(name = "rut", nullable = false, referencedColumnName = "id")
private Student student;
private String fecha_evaluacion;
private String nota1;
private String nota2;
private String nota3;
private String nota4;
public ResultadosPsicotecnicos() {}
public ResultadosPsicotecnicos(Student student) {
this.student = student;
}
/* getters and setters ommited */
package net.BBB.ProjectB.repository;
import java.util.List;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import net.BBB.ProjectB.entity.ResultadosPsicotecnicos;
@Repository
public interface ResultadosPsicotecnicosRepository extends CrudRepository<ResultadosPsicotecnicos, Long> {
@EntityGraph(value = "Student.resultadosPsicotecnicos", type = EntityGraphType.FETCH)
List<ResultadosPsicotecnicos> findByRut(long rut);
}