我正在尝试使用弹簧靴为小型驾驶学校制作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;
我对这个框架有点陌生,所以也许错误很明显,但是我看不到。现在,我将显示类和存储库。
这是项目的结构:
application.properties
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
学生.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 */
不要将Student中的此
rut
与ResultadosPsicotecnicos中的id
混淆。ResultadosPsicotecnicos.java
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 */
ResultadosPsicotecnicosRepository.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);
}
最后是导致错误的自定义方法。目前,我将使用学生的
rut
,稍后如果修复此问题,我将使用rut
,但现在外键的列称为id
,并且使用学生。欢迎任何帮助。
最佳答案
实际上,正如您在ResultadosPsicotecnicosRepository中指定findByRut一样,JPA尝试在ResultadosPsicotecnicos中查找列rut。
如下修改ResultadosPsicotecnicosRepository中的findByRut,因为您需要在jpa存储库中指定实体的对象名称,而不是db列名称
列出findByStudent_id(long studentid);
或者,您可以尝试以下解决方案。
要获取特定学生的ResultadosPsicotecnicos列表,则需要在StudentRepository中添加以下方法
@EntityGraph(值=“ Student.resultadosPsicotecnicos”,类型= EntityGraphType.FETCH)
学生findById(long id);
然后在学生班级的顶部添加以下行
@NamedEntityGraph(name =“ Student.resultadosPsicotecnicos”,attributeNodes = {@NamedAttributeNode(“ resultadosPsicotecnicos”)})