Spring Data查询返回pojo而不是预期的实体

时间:2018-08-01 20:39:54

标签: spring-data-jpa

所以我在春季仓库中有一个查询,如下所示:

@Repository
public interface StateQuestionsRepository extends JpaRepository<StateQuestions, Long> {
    @Query("select sq.id, q.questionType, q.description, sqo.values from StateQuestions sq join sq.questions q join sq.stateQuestionOption sqo where sq.pages =:page and sq.states =:state")
    List<StateQuestions> findAllByPagesAndStates(@Param("page") Pages pageId, @Param("state") States stateId);
}

查询返回数据,但不返回StateQuestion实体列表,而是返回对象列表,当我尝试将返回的数据映射到DTO时会导致问题。

所以我的第一个想法是“它必须返回POJO,因为它无法映射从另一个联接表(q.description,q.questionType,sqo.values)查询的列。所以我想也许可以添加那些属性的StateQuestions实体可以解决此问题,但不能解决。这是我的StateQuestions实体:

@Entity
@Table(name = "state_questions")
public class StateQuestions implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @ManyToOne
    @JsonIgnoreProperties("stateQuestions")
    private Pages pages;

    @ManyToOne
    @JsonIgnoreProperties("stateQuestions")
    private States states;

    @ManyToOne
    @JsonIgnoreProperties("stateQuestions")
    private Questions questions;

    @OneToOne
    @JoinColumn(unique = true)
    private StateQuestionOptions stateQuestionOption;

    private QuestionType questionType;

    private String description;

    private String values;

关于为什么这样做的任何想法?

0 个答案:

没有答案