0

I am trying to write CriteriaQuery in Spring boot application

I have Entity class as below

@Entity
@Data
class Student extends BaseEntity implements Serializable{

    @EmbeddedId
    StudentID id;

    @Column(name = "Student_name")
    String name;
    @Column(name = "Student_age")
    String age;

    // getters and setters

}
@Embeddable
@Data
class StudentID
{

    @Column(name = "Student_id")
    String id;
    @Column(name = "Student_dept")
    String dept_id;

}
@MappedSuperClass
@Entity
class BaseEntity{
@Column(name = "start_date")
Date startDate;
@Column(name = "end_date")
Date endDate;
}
@Repository
class StudentDao {

    EntityManager em;

    // constructor

    List<Student> findStudentsByCriteria(Student student) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Book> cq = cb.createQuery(Student.class);

        Root<Student> student= cq.from(Student.class);
        Predicate studentNamePredicate = cb.equal(student.get("id"), id); --> In this line I am getting exception as the attribute is null 

        Predicate studentDeptPredicate = cb.equal(student.get("dept_id"), dept_id);
        cq.where(authorNamePredicate, titlePredicate);

        TypedQuery<Student> query = em.createQuery(cq);
        return query.getResultList();
    }

}

Predicate studentNamePredicate = cb.equal(student.get("id"), id); --> In this line I am getting exception as the attribute is null.

I tried to debug the framework code and observed that the attribute is looked up in super class. Exception is due to attribute being null.

The attribute should be looked in Entity class but not sure why it is looked up in BaseEntity class.

dariosicily
  • 4,239
  • 2
  • 11
  • 17
Venkat
  • 1
  • 1

0 Answers0