城市。
地址实体内的 1 个城市。
是利兹并用这些公司的产品/服务列表填充
产品或服务。之后,用户可以选择例如牙医
从第三个名单。
最后一份附有公司的名单(利兹的牙医)
public class Company implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Short companyId;
@OneToOne(cascade=CascadeType.PERSIST)
private Address address;
private String companyName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "company",cascade=CascadeType.PERSIST)
private Set<Product> products = new HashSet<Product>(0);
public class Product implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "productId", unique = true, nullable = false)
private Integer productId;
private Short branchId;
private String productName;
private String sku;
private String category; ------> I am using this field in company search (dentists, garages etc.)
如何只查询那些有牙医类产品的公司?
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Company> criteria = criteriaBuilder.createQuery( Company.class );
Root<Company> companyRoot = criteria.from( Company.class );
//criteria.select(companyRoot);
TypedQuery<Company> q = em.createQuery(criteria);
List<Company> results = q.getResultList();
现在我已经有了每家公司,我如何才能只选择具有正确类别的公司?我想我需要
JOIN
但我不知道如何使用它。
最佳答案
使用 join(),
criteriaBuilder.equal(companyRoot.join("products").get("category"), "dentist")
看,
http://en.wikibooks.org/wiki/Java_Persistence/Querying#Joining.2C_querying_on_a_OneToMany_relationship
关于jpa - 如何在 JOIN 中使用 JPA Criteria API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8980869/