简体   繁体   English

Java Spring 投影内投影

[英]Java Spring projection inside projection

Is it possible to use a projection and in some related object use it's own projection?是否可以使用投影并在某些相关对象中使用它自己的投影?

For example, a have Exam , that has List<Question> .例如,有Exam ,有List<Question> I'd like to request a list of exams (which I have a @projection ), but I'd like to define the attributes to be retrieved for each related Question我想请求一份考试列表(我有一个@projection ),但我想定义要为每个相关Question检索的属性

If I understand correctly you want to use Projection as children of Projection.如果我理解正确,您想将 Projection 用作 Projection 的子项。 If it is the case, yes, you can.如果是这样,是的,你可以。 You can create a QuestionProjection and use inside the ExamProjection.您可以创建一个 QuestionProjection 并在 ExamProjection 中使用。

Example:示例:

@Projection(name = "questionProjection", types = { Question.class }) 
public interface QuestionProjection {
    // Getters
}

@Projection(name = "examProjection", types = { Exam.class }) 
public interface ExamProjection {
    List<QuestionProjection> getQuestionList();

    // Other Getters
}

You can do it something like this:你可以这样做:

Assuming that your Exam entity might be:假设您的Exam实体可能是:

@Entity
@Table(name = "EXAM")
public class Exam implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "DESCR")
    private String descr;

    @OneToMany(mappedBy = "exam")
    private List<Question> questions;

      // Getters and Setters

and your Question entity和你的Question实体

@Entity
@Table(name = "QUESTION")
public class Question implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "DESCR")
    private String descr;

    @Column(name = "CONT")
    private String cont;

    @ManyToOne
    @JoinColumn(name = "EXAM_ID")
    @JsonIgnoreProperties("questions")
    private Exam exam;

So, create projections所以,创建投影

public interface ExamProjection {
    Long getId();

    String getDescr();

    List<QuestionProjection> getQuestions();
}

and

public interface QuestionProjection {
    Long getId();

    String getDescr();
}

Your repository您的存储库

@Repository
public interface ExamRepository extends JpaRepository<Exam, Long> {

    List<Exam> findAll();

    @Query("SELECT e FROM Exam e")
    List<ExamProjection> findAllProjection();
}

Observe that using the findAll() method and passing the list type as ExamProjection, for some reason, causes a incompatible return type error.请注意,由于某种原因,使用findAll()方法并将列表类型作为 ExamProjection 传递会导致不兼容的返回类型错误。 To avoid that, create a custom method, in this case, findAllProjection() .为避免这种情况,请创建一个自定义方法,在本例中为findAllProjection()

The service服务

@Service
public class ExamService {

    @Autowired
    ExamRepository examRepository;

    public List<ExamProjection> findAllProjection() {
        return examRepository.findAllProjection();
    }
}

and finally, the resource最后,资源

@RestController
@RequestMapping(value = "/exam")
public class ExamResource {

    @Autowired
    ExamService examService;

    @GetMapping
    public ResponseEntity<List<ExamProjection>> findAll() {
        return ResponseEntity.ok().body(examService.findAllProjection());
    }
}

Using the above, the json returned don't contains the field cont , because de QuestionProjection don't have the method getCont() .使用上述内容,返回的 json 不包含字段cont ,因为 de QuestionProjection没有方法getCont()

[
    {
        "id": 1,
        "descr": "First Exam",
        "questions": [
            {
                "id": 1,
                "descr": "First Question"
            },
            {
                "id": 2,
                "descr": "Second Question"
            }
        ]
    }
]

If the QuestionProjection changes to如果QuestionProjection更改为

public interface QuestionProjection {
    Long getId();

    String getCont();
}

the json returned changes to json 返回更改为

[
    {
        "id": 1,
        "descr": "First Exam",
        "questions": [
            {
                "id": 1,
                "cont": "First Question Content"
            },
            {
                "id": 2,
                "cont": "Second Question Content"
            }
        ]
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM