我是Criteria的新手,无法理解下一件事。
我的实体彼此之间具有连通性
用户:
//...fields...
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
private Document document;
文档:
//...fields...
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "doc_type_id", nullable = false)
private DocType docType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "citizenship_id", nullable = false)
private Country country;
文档类型:
//...fields...
@Column(nullable = false, length = 5)
private String code;
国家/地区:
//...fields...
@Column(nullable = false, length = 5)
private String code;
我需要使用包含docCode(DocType.code)和citizenshipCode(Country.code)的文档来查找用户。
道:
//private final EntityManager em;
public List<User> filterUser(UserView userView) {
List<User> userList;
List<Document> documentList;
Long officeId = userView.officeId;
String docCode = userView.docCode;
String citizenshipCode = userView.citizenshipCode;
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<User> userQuery = criteriaBuilder.createQuery(User.class);
Root<User> userRoot = userQuery.from(User.class);
userQuery.select(userRoot);
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
//find id of DocType
Long docTypeId = null;
if (docCode != null) {
CriteriaQuery<DocType> docTypeQuery = criteriaBuilder.createQuery(DocType.class);
Root<DocType> docTypeRoot = docTypeQuery.from(DocType.class);
docTypeQuery.select(docTypeRoot);
docTypeQuery.where(criteriaBuilder.equal(docTypeRoot.get("code"), docCode));
docTypeId = em.createQuery(docTypeQuery).getSingleResult().getId();
}
//find id of Country
Long countryId = null;
if (citizenshipCode != null) {
CriteriaQuery<Country> countryQuery = criteriaBuilder.createQuery(Country.class);
Root<Country> countryRoot = countryQuery.from(Country.class);
countryQuery.select(countryRoot);
countryQuery.where(criteriaBuilder.equal(countryRoot.get("code"), citizenshipCode));
countryId = em.createQuery(countryQuery).getSingleResult().getId();
}
if (docTypeId != null || countryId != null) {
CriteriaQuery<Document> documentQuery = criteriaBuilder.createQuery(Document.class);
Root<Document> documentRoot = documentQuery.from(Document.class);
documentQuery.select(documentRoot);
if (docTypeId != null) {
//filter for Document which contains docTypeId
documentQuery.where(criteriaBuilder.equal(documentRoot.get("docType"), docTypeId));
}
if (countryId != null) {
//filter for which contains countryId
documentQuery.where(criteriaBuilder.equal(documentRoot.get("country"), countryId));
}
documentList = em.createQuery(documentQuery).getResultList();
List<Integer> idDocuments = new ArrayList<>();
//find all of required documents
documentList.stream().forEach((d) -> idDocuments.add(Math.toIntExact(d.getId())));
userQuery.where(userRoot.get("document").in(idDocuments));
}
userList = em.createQuery(userQuery).getResultList();
return userList;
}
我对这些行感兴趣
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
和
userQuery.where(userRoot.get("document").in(idDocuments));
为什么第一行的过滤器不执行?
我知道有嵌套的子查询,但是我无法正确处理。
答案 0 :(得分:1)
我不知道您想使用“ Office”做什么,但是您不应该将不属于问题的内容发布到工作中。 $ cat file{1..4}.txt
text1;
text1;
text1;
text2;
text2;
text2;
text3;
text3;
text3;
text4;
text4;
text4;
导致您遇到问题,导致@PrimaryKeyJoinColumn
被排除在user_id
之外。
Document
并使用它:
@Entity
public class User {
@Id @GeneratedValue private int id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
private Document document;
@Entity
public class Document {
@Id @GeneratedValue private int id;
@OneToOne(fetch = FetchType.LAZY)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "doc_type_id", nullable = false)
private DocType docType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "citizenship_id", nullable = false)
private Country country;
@Entity
public class DocType {
@Id @GeneratedValue private int id;
@Column(nullable = false, length = 5)
private String code;
@Entity
public class Country {
@Id @GeneratedValue private int id;
@Column(nullable = false, length = 5)
private String code;