Crudepository(spring boot)中自定义方法的java问题
我正在尝试使用spring boot为一所小型驾校开发一个web应用程序。此时,我正在尝试创建使用外键的表:一个学生将有多个评估,所以我正在进行1:N关联。问题是,当我试图运行项目进行测试时,我得到了这个错误(只想说明原因):
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property rut found for type ResultadosPsicotecnicos!
我做了一个自定义方法来获取一个学生的所有评估,但由于某种原因,它不起作用。我查看了数据库,看看这个列是否真的被创建了,下面是我看到的代码:
-- 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
学生。爪哇
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 */
不要把Student中的rut
和Resultadospicotecnicos中的混淆
结果球囊炎。爪哇
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 */
结果:ospicotecnicose阳性。爪哇
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);
}
最后是导致错误的自定义方法。现在我将使用学生的id
,稍后如果我解决了这个问题,我将使用rut
,但是现在外键的列名为rut
,它使用学生的id
欢迎任何帮助
# 1 楼答案
实际上,JPA正试图在ResultadosPsicotecnicos中找到列rut,正如您在ResultadOSpicoteCnicosRespository中指定的findByRut一样
按如下所示修改Resultadoscsicotecnicosrepository中的findByRut,因为您需要在jpa存储库中指定实体的对象名,而不是db列名 列出findByStudent_id(long studentid)
或者,您可以尝试以下解决方案。 要获得特定学生的结果分类列表,您需要在StudentRepository中添加以下方法
@EntityGraph(value=“Student.resultadospicotecnicos”,type=EntityGraphType.FETCH) 学生findById(长id); 请在学生课的最上面加上下面一行 @NamedEntityGraph(name=“Student.resultadospicotencicos”,attributeNodes={@NamedAttributeNode(“resultadospicotencos”)})