当前位置: 首页 > 知识库问答 >
问题:

Spring Data JPA-规范查询

农飞星
2023-03-14

有一个使用属性表达式的查询:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-方法。查询属性表达式:

List<Pupil> findByPupilInClassRoomTeacherName(String name);

试图通过规范执行查询:

public final class PupilSpecification {
    public static Specification<Pupil> findPupilByTeacherSpecification(String name) {
        return new Specification<Pupil>() {
            @Override
            public Predicate toPredicate(Root<Pupil> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                root = criteriaQuery.from(Pupil.class);
                Join<Pupil, PupilInClassRoom> pupil_PupilInClassRoomJoin = root.join("pupil_id");
                Join<Teacher, PupilInClassRoom> teacher_PupilInClassRoomJoin = pupil_PupilInClassRoomJoin.join("teacher_id");
                return criteriaBuilder.equal(teacher_PupilInClassRoomJoin.get("name"),name);
            }
        };
    }
}

我得到了一个错误:

org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute  with the the given name [pupil_id] on this ManagedType [com.SchoolJournal.SpringHibernate.model.BaseModel]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [pupil_id] on this ManagedType [com.SchoolJournal.SpringHibernate.model.BaseModel]

我做错了什么或忘记添加了什么?

查询:按教师姓名显示所有学生

基地实体:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
abstract class BaseModel {

@Id
@Column(name = "id")
@Getter
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

protected BaseModel() {
  }
}

实体教师:

@Entity
@Table(name = "teacher")
public final class Teacher extends BaseModel {

@OneToOne
@JoinColumn(name = "teacher_id")
private PupilInClassRoom pupilInClassRoom;

@Column(nullable = false)
@Getter
@Setter
private String name;

@Column(nullable = false)
@Getter
@Setter
private String surname;

@Column(nullable = false)
@Getter
@Setter
private String discipline;

public Teacher() {
  }
}

实体学生:

@Entity
@Table(name = "pupil")
public final class Pupil extends BaseModel {

@OneToOne
@JoinColumn(name = "pupil_id")
private PupilInClassRoom pupilInClassRoom;

@Column(nullable = false)
@Getter
@Setter
private String name;

@Getter
@Setter
@Column(nullable = false)
private String surname;

public Pupil() {

  }
}

实体教室:

@Entity
@Table(name = "class_room")
public final class ClassRoom extends BaseModel {

@OneToOne
@JoinColumn(name = "class_Room_id")
private PupilInClassRoom pupilInClassRoom;

@Column(nullable = false)
@Getter
@Setter
private String name;

public ClassRoom() {
   }
}

实体教室:

@Entity
@Table(name = "pupil_in_class_room")
public final class PupilInClassRoom  extends BaseModel {

@OneToOne(mappedBy = "pupilInClassRoom")
@Getter
@Setter
private Pupil pupil;

@OneToOne(mappedBy = "pupilInClassRoom")
@Getter
@Setter
private ClassRoom classRoom;

@OneToOne(mappedBy = "pupilInClassRoom")
@Getter
@Setter
private Teacher teacher;

public PupilInClassRoom() {

}

共有2个答案

柏高洁
2023-03-14

问题的解决方案:

public final class PupilSpecification {
        public static Specification<Pupil> findPupilByTeacherSpecification(String name) {
            return new Specification<Pupil>() {
                @Override
                public Predicate toPredicate(Root<Pupil> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    root = criteriaQuery.from(Pupil.class);
                    Join<Pupil, PupilInClassRoom> pupil_PupilInClassRoomJoin = root.join("pupilInClassRoom");
                    Join<PupilInClassRoom, Teacher> teacher_PupilInClassRoomJoin = pupil_PupilInClassRoomJoin.join("teacher");
                    return criteriaBuilder.equal(teacher_PupilInClassRoomJoin.get(Teacher_.Name),name);
                }
            };
        }
    }

并添加元模型教师_

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Teacher.class)
public abstract class Teacher_ {
    public static volatile SingularAttribute<Teacher,String> name;
    public static volatile SingularAttribute<Teacher,String> surmane;
    public static volatile SingularAttribute<Teacher,String> discipline;
    public static volatile SingularAttribute<BaseModel,Integer> id;

    public static final String Name = "name";
    public static final String Surname = "surname";
    public static final String Discipline = "discipline";
    public static final String Id = "id";
}

通话规格:

pupilRepository.findAll(PupilSpecification.findPupilByTeacherSpecification("Alla"))
司马高明
2023-03-14
Join<Pupil, PupilInClassRoom>   pupil_PupilInClassRoomJoin   =
        root.join("pupilInClassRoom");
Join<PupilInClassRoom, Teacher> teacher_PupilInClassRoomJoin = 
        pupil_PupilInClassRoomJoin.join("teacher");

root.join("pupil_id")-

<代码>加入

学生加入。加入(“教师id”)-

上面的更新是我猜测做你想做的事情需要的。可以试试吗?

 类似资料:
  • Number,String,Boolean和Object 不要使用如下类型Number,String,Boolean或Object。 这些类型指的是非原始的装盒对象,它们几乎没在JavaScript代码里正确地使用过。 /* 错误 */ function reverse(s: String): String; 应该使用类型number,string,and boolean。 /* OK */ fu

  • 参数规范 参数前带* 表示必填 参数命名采用小写加下划线形式 请求每个接口时,在 header里带上 XX-Token(用户 token),XX-Device-Type(类型:mobile,android,iphone,ipad,web,pc,mac,wxapp),XX-Api-Version(比如1.0.0),XX-Wxapp-AppId(小程序 AppId) 返回结果 参数命名采用小写加下划线

  • 公司及产品名称 我们的公司及产品名称是「DaoCloud」。注意这是两个单词的合成词,所以中间沒有空格(参考:GitHub)。如作为 URL 的一部分,应该使用全小写的「daocloud」。 文案风格 一定多检查,确保没有错别字。 即使是流行语中的谐音错别字也不要使用,比如「墙裂」、「童鞋」等。 我们崇尚精练的文风。请在检查中把对表达意思没有明显作用的字、词、句删除,在不影响表达效果的前提下把文案

  • 了解如何在 XD 中检查设计规范。 在接收设计规范时,您可以检查画板中不同对象的颜色、文本、测量值和热点等属性。 查看和检查测量值 在画板中选择对象以检查其测量值和样式。 要确定对象之间的间距,请选择一个对象,然后将指针悬停在画板中其他对象的上方。检查对象的测量值 查看颜色、字符样式和资源 在设计规范链接中选择画板时,该画板中使用的所有独特颜色都显示在“颜色”部分中。您可以查看可用的颜色格式,并使

  • SpringDataJpa中 Specification怎样使用in查询 实体类Menu.java如下 想通过roles属性做in查询,代码如下 错误如下: Parameter value [com.appmtce.pojo.entity.role.Role@20f81e2a] did not match expected type [java.util.Collection (n/a)] 我的S

  • AMD(异步模块定义)是为浏览器环境设计的,因为 CommonJS 模块系统是同步加载的,当前浏览器环境还没有准备好同步加载模块的条件。 AMD 定义了一套 JavaScript 模块依赖异步加载标准,来解决同步加载的问题。 模块通过 define 函数定义在闭包中,格式如下: define(id?: String, dependencies?: String[], factory: Functi