SpringData JPA多対一クエリー

5720 ワード

エンティティークラス:
①PaperStoreクラス
package com.sssp.entity;

import Java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Table(name="PaperStore")
@Entity
public class PaperStore {

    private Integer id;
    private String paperName;
    private Vocation vocation;
    private String paperType;
    private String paperDegree;
    private Integer paperPublish;
    private Integer assembleMode;
    private Date paperDate;
    private String examTime;

    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPaperName() {
        return paperName;
    }

    public void setPaperName(String paperName) {
        this.paperName = paperName;
    }

    @JoinColumn(name="Vocation_Id")
    @ManyToOne
    public Vocation getVocation() {
        return vocation;
    }

    public void setVocation(Vocation vocation) {
        this.vocation = vocation;
    }

    public String getPaperType() {
        return paperType;
    }

    public void setPaperType(String paperType) {
        this.paperType = paperType;
    }

    public String getPaperDegree() {
        return paperDegree;
    }

    public void setPaperDegree(String paperDegree) {
        this.paperDegree = paperDegree;
    }

    public Integer getPaperPublish() {
        return paperPublish;
    }

    public void setPaperPublish(Integer paperPublish) {
        this.paperPublish = paperPublish;
    }

    public Integer getAssembleMode() {
        return assembleMode;
    }

    public void setAssembleMode(Integer assembleMode) {
        this.assembleMode = assembleMode;
    }

    public Date getPaperDate() {
        return paperDate;
    }

    public void setPaperDate(Date paperDate) {
        this.paperDate = paperDate;
    }

    public String getExamTime() {
        return examTime;
    }

    public void setExamTime(String examTime) {
        this.examTime = examTime;
    }

    @Override
    public String toString() {
        return "PaperStore [id=" + id + ", paperName=" + paperName + ", vocation=" + vocation + ", paperType="
                + paperType + ", paperDegree=" + paperDegree + ", paperPublish=" + paperPublish + ", assembleMode="
                + assembleMode + ", paperDate=" + paperDate + ", examTime=" + examTime + "]";
    }

}

②QuestionPaperクラス
package com.sssp.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Table(name="Question_Paper")
@Entity
public class QuestionPaper {

    private Integer id;
    private PaperStore paperStore;
    private QuestionInfo questionInfo;

    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @JoinColumn(name="Paper_Id")
    @ManyToOne
    public PaperStore getPaperStore() {
        return paperStore;
    }

    public void setPaperStore(PaperStore paperStore) {
        this.paperStore = paperStore;
    }

    @JoinColumn(name="Question_Id")
    @ManyToOne
    public QuestionInfo getQuestionInfo() {
        return questionInfo;
    }

    public void setQuestionInfo(QuestionInfo questionInfo) {
        this.questionInfo = questionInfo;
    }

}



③repository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.sssp.entity.PaperStore;
import com.sssp.entity.QuestionPaper;

public interface QuestionPaperRepository extends JpaRepository,JpaSpecificationExecutor{
    
    public List getByPaperStore(PaperStore paperStore);
    
    @Query("SELECT qp FROM QuestionPaper qp INNER JOIN qp.paperStore ps WHERE ps.id = ?1")
    List getQuestionPaper(Integer id);
}

需要:paperIdによってQuestionPaperが得られ、1つのpaperIdは複数のQuestionPaperに対応する
方法1:PaperStoreをクエリーしてからPaperStoreでQuestionPaperをクエリーする
サービス層:
PaperStore paperStore = paperStoreRepository.getById(id);
List questionPapers= questionPaperRepository.getByPaperStore(paperStore);
方法2:@Query注釈による
サービス層:
List questionPapers= questionPaperRepository.getQuestionPaper(id);
方法3:Repository継承JpaSpecificationExecutor
サービス層:
Specification specification = new Specification() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
                Join join = root.join("paperStore",JoinType.INNER);
                Path exp = join.get("id");
                return cb.ge(exp, id);
            }
        };
        List questionPapers= questionPaperRepository.findAll(specification);

メソッド4:プロパティの接尾辞
@Query("SELECT qp.questionInfo.questionId FROM QuestionPaper qp WHERE qp.paperStore.id = ?1")     List getQuestionId(Integer id);