简体   繁体   English

列表中的休眠搜索查询与具有多个关系的子表完全匹配

[英]hibernate search query from a list that match exactly from the child table with one-many relation ship

I am using spring-boot and hibernate . 我正在使用spring-boothibernate I am using one to many relationships . 我正在使用one to many relationships

In the main table, it has the details of the user logs like 在主表中,它具有用户日志的详细信息,例如

jobId(pk), department, startDate. The child table is the category table(Id(pk),catId,catDesc,jobId(fk))

ie each jobId in the parent table can have multiple categories. 即,父表中的每个jobId可以具有多个类别。 Now I want to get all the values from the main table and child table that exactly matches with the List of categories(child table values). 现在,我想从主表和子表中获取与类别列表(子表值)完全匹配的所有值。

createQuery("select * from parent p, child c where p.jobId=c.jobId AND c.catId IN ("+catId+" ) )

here catId is a list of values. catId是值列表。 But I want to get only those values that match all the values and the query is dynamic. 但是我只想获取与所有值匹配的那些值,并且查询是动态的。

package com.assorted.product.model;
import java.io.Serializable;
import java.util.Date;`enter code here`
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "parent")
public class Parent implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "JOB_ID")
    private long jobId;
    @Column(name = "USER_ID")
    private String userId;
    @Column(name = "COUNTRY_NAME")
    private String countryName;
    @Column(name = "DEPT_ID")
    private long depId;
    @Column(name = "DEPT_NAME")
    private String depName;
    @Column(name = "START_DATE")
    @Temporal(TemporalType.DATE)
    private Date startDate;
    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="JOB_ID",referencedColumnName="JOB_ID")
    private Set<CategoryLogs> categoryLogs;
    public long getJobId() {
        return jobId;
    }
    public void setJobId(long jobId) {
        this.jobId = jobId;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getCountryName() {
        return countryName;
    }
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }
    public long getDepId() {
        return depId;
    }
    public void setDepId(long depId) {
        this.depId = depId;
    }
    public String getDepName() {
        return depName;
    }
    public void setDepName(String depName) {
        this.depName = depName;
    }
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Set<CategoryLogs> getCategoryLogs() {
        return categoryLogs;
    }
    public void setCategoryLogs(Set<CategoryLogs> categoryLogs) {
        this.categoryLogs = categoryLogs;
    }
}

package com.assorted.product.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "child")
public class CategoryLogs {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private String id;
    @Column(name = "CAT_ID")
    private long catId;
    @Column(name = "CAT_NAME")
    private String catName;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "JOB_ID")
    private Parent parent;
    public CategoryLogs(){
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public long getCatId() {
        return catId;
    }
    public void setCatId(long catId) {
        this.catId = catId;
    }
    public String getCatName() {
        return catName;
    }
    public void setCatName(String catName) {
        this.catName = catName;
    }
    public Parent getParent() {
        return parent;
    }
    public void setParent(Parent parent) {
        this.parent = parent;
    }
}

JOIN FETCH . 加入FETCH

The FETCH keyword of the JOIN FETCH statement is JPA-specific. JOIN FETCH语句的FETCH关键字是JPA特定的。 It tells the persistence provider to not only join the 2 database tables within the query but to also initialize the association on the returned entity. 它告诉持久性提供程序不仅在查询中联接2个数据库表,而且还初始化返回实体上的关联。 You can use it with a JOIN and a LEFT JOIN statement. 您可以将其与JOIN和LEFT JOIN语句一起使用。

List<Parent> parents = em.createQuery("SELECT p FROM Parent p JOIN FETCH p.categoryLogs c 
  WHERE c.catId IN (:cat_Ids) " , Parent.class)
  .setParameterList("cat_Ids",your_cat_id_list )
   .getResultList();

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

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