@SqlResultSetMapping columns: entities with subobjects

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(mappedBy = "test", fetch = FetchType.EAGER)
private Set<Question> questions;
@ManyToMany(mappedBy = "tests")
private Set<User> users;

Question Object :

public class Question {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "is_multichoice", nullable = false)
private boolean isMultichoice;
@Column(name = "is_open", nullable = false)
private boolean isOpen;
@Column(name = "picture")
private String picture;
@Column(name = "question")
private String question;
@ManyToOne
@JoinColumn(name = "test_id", nullable = false)
private Test test;
@Column(name = "archived", nullable = false)
private boolean isArchived;
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER)
private Set<Answer> answers;

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

I wrote an SQL query to get the test (the reason this is not HQL can be found at the Hibernate HQL link : no object was found for the query ):

@NamedNativeQuery(name = "getCurrentTestById",
            query = "SELECT t.id as tId, t.test_name, t.duration, q.id as qId, " +
                    "q.question as question, q.is_multichoice as is_multichoice, " +
                    "q.is_open as is_open, a.id as aId, a.answer_text as 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")

Now I need to map it to the object test using the @SqlResultSetMapping annotation:

@SqlResultSetMappings({
    @SqlResultSetMapping(name="toTest",
    entities = {
            @EntityResult(entityClass = com.bionic.entities.Test.class, fields = {
                    @FieldResult(name = "id", column = "tId"),
                    @FieldResult(name = "test_name", column = "test_name"),
                    @FieldResult(name = "duration", column = "duration"),
                    @FieldResult(name = "questions.question", column = "question"),
                    @FieldResult(name = "questions.id", column = "qId"),
                    @FieldResult(name = "questions.isMultichoice", column = "is_multichoice"),
                    @FieldResult(name = "questions.isOpen", column = "is_open"),
                    @FieldResult(name = "questions.answers.id", column = "aId"),
                    @FieldResult(name = "questions.answers.answer_text", column = "answer_text"),
            })
    })
})

I get an exception:

Caused by: org.hibernate.MappingException: dotted notation reference neither a component nor a many/one to one
+6
1

. hibernate, . , , .. , , , . , , , json, For JSON . JSON . , , .

0

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


All Articles