我正在使用 spring-boot
以及 hibernate
. 我正在使用 one to many relationships
.
在主表中,它包含用户日志的详细信息,如 jobId(pk), department, startDate. The child table is the category table(Id(pk),catId,catDesc,jobId(fk))
i、 e每个 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;
}
}
1条答案
按热度按时间zi8p0yeb1#
加入fetch。
join fetch语句的fetch关键字是特定于jpa的。它告诉持久性提供者不仅要连接查询中的两个数据库表,还要初始化返回实体上的关联。您可以将它与join和left join语句一起使用。