如何使用休眠条件加入 3 table?
How to join 3 table using hibernate criteria?
我有三张表。
- parent
- parent_child_mapping
- child
我想加入 3 个表并使用 'CriteriaBuilder' 和 'Predicate' 按 child 名称过滤。
我如何使用 'CriteriaBuilder' & 'Predicate'.
实现以下 sql 查询
SELECT p.parent_id
FROM parent p
JOIN parent_child_mapping pcmap on p.parent_id = pcmap.mapping_parent_id
JOIN child c on pcmap.mapping_child_id = c.child_id
WHERE c.child_name = 'abc'
Parent实体
@Entity
@Table(name = "parent")
public class Parent {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "parent_id")
private Long parentId;
....
@OneToMany(cascade = CascadeType.ALL, mappedBy = "mappingParentId")
private Collection<ParentChildMapping> parentChildMappingCollection;
}
Parent child映射实体
@Entity
@Table(name = "parent_child_mapping")
public class ParentChildMapping{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "mapping_id")
private Long mappingId;
@JoinColumn(name = "mapping_child_id")
@ManyToOne(optional = false)
private ChildEntity mappingChildId;
@JoinColumn(name = "mapping_parent_id")
@ManyToOne(optional = false)
private ParentEntity mappingParentId;
}
Child实体
@Data
@Entity
@Table(name = "child")
public class Child implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "child_id")
private Long childId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "child_name")
private String childName;
}
您只需为每个实体创建一个根并将条件添加到 where 子句。像这样:
Root<Parent> parent = criteriaQuery.from(Parent.class);
Root<ParentChildMapping> mapping = criteriaQuery.from(ParentChildMapping.class);
Root<Child> child = criteriaQuery.from(Child.class);
criteriaQuery.select(parent.get("parentId"));
criteriaQuery.where(
criteriaBuilder.and(
criteriaBuilder.equal(parent.get("parentId"), mapping.get("mappingParentId")),
criteriaBuilder.equal(child.get("childId"), mapping.get("mappingChildId")),
criteriaBuilder.equal(child.get("childName"), "abc")
)
);
我有三张表。
- parent
- parent_child_mapping
- child
我想加入 3 个表并使用 'CriteriaBuilder' 和 'Predicate' 按 child 名称过滤。 我如何使用 'CriteriaBuilder' & 'Predicate'.
实现以下 sql 查询 SELECT p.parent_id
FROM parent p
JOIN parent_child_mapping pcmap on p.parent_id = pcmap.mapping_parent_id
JOIN child c on pcmap.mapping_child_id = c.child_id
WHERE c.child_name = 'abc'
Parent实体
@Entity
@Table(name = "parent")
public class Parent {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "parent_id")
private Long parentId;
....
@OneToMany(cascade = CascadeType.ALL, mappedBy = "mappingParentId")
private Collection<ParentChildMapping> parentChildMappingCollection;
}
Parent child映射实体
@Entity
@Table(name = "parent_child_mapping")
public class ParentChildMapping{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "mapping_id")
private Long mappingId;
@JoinColumn(name = "mapping_child_id")
@ManyToOne(optional = false)
private ChildEntity mappingChildId;
@JoinColumn(name = "mapping_parent_id")
@ManyToOne(optional = false)
private ParentEntity mappingParentId;
}
Child实体
@Data
@Entity
@Table(name = "child")
public class Child implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "child_id")
private Long childId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "child_name")
private String childName;
}
您只需为每个实体创建一个根并将条件添加到 where 子句。像这样:
Root<Parent> parent = criteriaQuery.from(Parent.class);
Root<ParentChildMapping> mapping = criteriaQuery.from(ParentChildMapping.class);
Root<Child> child = criteriaQuery.from(Child.class);
criteriaQuery.select(parent.get("parentId"));
criteriaQuery.where(
criteriaBuilder.and(
criteriaBuilder.equal(parent.get("parentId"), mapping.get("mappingParentId")),
criteriaBuilder.equal(child.get("childId"), mapping.get("mappingChildId")),
criteriaBuilder.equal(child.get("childName"), "abc")
)
);