我正在使用spring-boot
和hibernate
。我正在使用one to many relationships
。
在主表中,它包含用户日志的详细信息,例如
jobId(pk), department, startDate. The child table is the category table(Id(pk),catId,catDesc,jobId(fk))
,即父表中的每个jobId
可以具有多个类别。现在,我想从主表和子表中获取与类别列表(子表值)完全匹配的所有值。
createQuery("select * from parent p, child c where p.jobId=c.jobId AND c.catId IN ("+catId+" ) )
此处catId
是值列表。但是我只想获取与所有值匹配的那些值,并且查询是动态的。
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;
}
}
答案 0 :(得分:0)
加入比赛。
JOIN FETCH语句的FETCH关键字是JPA特定的。它告诉持久性提供程序不仅在查询中联接2个数据库表,而且还初始化返回实体上的关联。您可以将其与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();