#java #hibernate #predicate #hibernate-criteria
Вопрос:
У меня три стола.
- родитель
- сопоставление parent_child_mapping
- ребенок
Я хочу присоединиться к 3 таблицам и фильтровать по имени ребенка, используя «CriteriaBuilder» и «Предикат». Как я могу реализовать приведенный ниже sql-запрос, используя «CriteriaBuilder» и «Предикат».
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'
Материнская организация
@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;
}
Родительский дочерний объект сопоставления
@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;
}
Дочерняя организация
@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;
}
Комментарии:
1. В чем смысл
ParentChildMapping
? Разве вы не можете просто использовать@ManyToMany
?
Ответ №1:
Вы просто создаете корневой каталог для каждой сущности и добавляете условие в предложение 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")
)
);