JPA查询一对一关系

Posted

技术标签:

【中文标题】JPA查询一对一关系【英文标题】:JPA query for one to one relation 【发布时间】:2015-08-01 17:18:39 【问题描述】:

我有一个 EJB 应用程序,我在其中使用实体 bean 作为数据库。我必须拥有具有单向一对一关系、JobPositionEntity 和 CandidateEntity 的实体 bean。

这里是候选实体 @实体 公共类 CandidateEntity 扩展 BaseEntity

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long meritNumber;

    private String seatNumber;

    private String candidateName;

    private String gender;

    public Long getMeritNumber() 
        return meritNumber;
    

    public void setMeritNumber(Long meritNumber) 
        this.meritNumber = meritNumber;
    

    public String getSeatNumber() 
        return seatNumber;
    

    public void setSeatNumber(String seatNumber) 
        this.seatNumber = seatNumber;
    

    public String getCandidateName() 
        return candidateName;
    

    public void setCandidateName(String candidateName) 
        this.candidateName = candidateName;
    

    public String getGender() 
        return gender;
    

    public void setGender(String gender) 
        this.gender = gender;
    

    @Override
    public Long getId() 
        return id;
    

    @Override
    public void setId(Long id) 
        this.id = id;
    

    @Override
    public int hashCode() 
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    

    @Override
    public boolean equals(Object object) 
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CandidateEntity)) 
            return false;
        
        CandidateEntity other = (CandidateEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) 
            return false;
        
        return true;
    

    @Override
    public String toString() 
        return "com.nisheeth.config.ejb.entity.CandidateEntity[ id=" + id + " ]";
    


这里是 JobPositionEntity @实体 公共类 JobPositionEntity 扩展 BaseEntity

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToOne(fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
    private CandidateEntity candidate;

    @ManyToOne(fetch = FetchType.EAGER)
    private SeasonEntity season;

    public SeasonEntity getSeason() 
        return season;
    

    public void setSeason(SeasonEntity season) 
        this.season = season;
    

    public CandidateEntity getCandidate() 
        return candidate;
    

    public void setCandidate(CandidateEntity candidate) 
        this.candidate = candidate;
    

    @Override
    public Long getId() 
        return id;
    

    @Override
    public void setId(Long id) 
        this.id = id;
    

    @Override
    public int hashCode() 
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    

    @Override
    public boolean equals(Object object) 
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof JobPositionEntity)) 
            return false;
        
        JobPositionEntity other = (JobPositionEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) 
            return false;
        
        return true;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    @Override
    public String toString() 
        return "com.nisheeth.config.ejb.entity.JobPositionEntity[ id=" + id + " ]";
    


我想选择不在 JobPositionEntity 中的候选人。我有这个对我不起作用的查询:

select ce.candidateName, ce.id from JobPositionEntity jp left join  jp.candidate ce where ce <> null

谁能帮忙写这个查询?提前致谢。

【问题讨论】:

我看不到发布的 JPQL 与您想要的描述之间的任何关系。你能澄清一下吗?举个例子。 如果您在 JobPositionEntity 中看到,则存在一对一关系。 这并没有说明什么。假设您有 J1-C1、J2-C2、J3-null、null-C4,其中 J 是 JobPosition,C 是 Candidate。查询应该找到什么? 【参考方案1】:

您可以使用子查询

select c from Candidate c where c.id not in
(select jp.candidate.id from JobPositionEntity jp)

更多信息: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

【讨论】:

以上是关于JPA查询一对一关系的主要内容,如果未能解决你的问题,请参考以下文章

JPA中实现双向一对一的关联关系

使用具有一对多关系的 JPA 映射实体,如何添加连接条件

JPA 与 multipartfile 的一对一关系

在 JPA 中保存具有一对一关系的实体

关于JPA一对一,一对多(多对一),多对多的详解

SpringData JPA中一对一关系映射注解@OneToOne应用