简体   繁体   中英

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 . I am using 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. 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. 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 .

The FETCH keyword of the JOIN FETCH statement is JPA-specific. 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. You can use it with a JOIN and a LEFT JOIN statement.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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