Hibernate HQL: no object found for object

He spent the whole day trying to solve this problem, but he still failed. I have a test object :

public class Test {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "duration", nullable = false)
private int duration;
@Column(name = "test_name", nullable = false, unique = true)
private String testName;
@Column(name = "archived", nullable = false)
private boolean archived;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "test", fetch = FetchType.EAGER)
private Set<Question> questions;
@ManyToMany(mappedBy = "tests")
private Set<User> users;

This test object has a set of questions , so Question Entity has a set of answers .

I wrote an HQL query to get the test:

 @NamedQuery(name = "getCurrentTestById",
            query = "SELECT test From Result result JOIN result.test test JOIN result.user user " +
                    "JOIN test.questions question JOIN question.answers answer " +
                    "WHERE test.id = :testId AND user.id = :userId " +
                    "AND result.permission.id = :permissionId AND question.isArchived = false AND answer.isArchived = false")

My DAO :

    public Test getCurrentTest(long id, long testId, long permissionId){
    Query query = em.createNamedQuery("getCurrentTestById");
    query.setParameter("userId", id);
    query.setParameter("testId", testId);
    query.setParameter("permissionId", permissionId);
    return (Test)query.getSingleResult();
}

So, everything is in order until I asked any question or answer as "archived". I still get everything, even if they are archived . Moreover, if I mark the entire set of questions / answers for the current test as archived, I will get an exception: no objects were found for the request

+2
2

, Hibernate.

, Hibernate , , , , , , , , , .

- Hibernate-, , , , , .

, , Hibernate, , , , , Hibernate , WHERE.

, , Test, , :

SELECT question FROM Result result 
  JOIN result.test test 
  JOIN result.user user
  JOIN test.questions question
  JOIN question.answers answer
 WHERE test.id = :testId
   AND user.id = :userId
   AND result.permission.id = :permissionId 
   AND question.isArchived = false 
   AND answer.isArchived = false

, WHERE.

, Test , get Test, , , :

   public Set<Question> getQuestionsNotArchived() {
        Set<Question> notArchivedQuestions = new HashSet<>();
        for (Question question : questions) {
            if (!question.isArchived()) {
                 notArchivedQuestions.add(question);
            }
        }
        return notArchivedQuestions;
   }

Hibernate, .

, Test .

, , , : - , getSingleResult() Query, , . , , , WHERE, , /, , , , , / , , /, .

, , !

+2

, ( , ) SQL- NATIVE:

@NamedNativeQuery(name = "getCurrentTestById",
            query = "SELECT t.id as tId, t.test_name, t.duration, q.id as qId, " +
                    "q.question, q.is_multichoice, q.is_open, a.id as aId, a.answer_text  FROM result r " +
                    "JOIN test t ON r.test_id = t.id " +
                    "JOIN user u ON r.user_id = u.id " +
                    "JOIN question q ON t.id = q.test_id JOIN answer a ON q.id = a.question_id " +
                    "WHERE t.id = :testId AND u.id = :userId AND r.permission = :permissionId " +
                    "AND q.archived = false AND a.archived = false")
+2

Source: https://habr.com/ru/post/1619083/


All Articles