我正在编写一个在Hibernate中有多个连接的查询。 现在我需要查询使用某个项目的所有模型和相关产品。
我创建了hql查询,它看起来像:
"FROM Model m inner join fetch m.products p inner join p.itemsInProducts iip inner join iip.item i where i.number = '" + itemID + "' and m.active = '1' and p.active = '1' and iip.active = '1'"
它给了我一些结果。但是我有多个Model对象是相同但具有不同的产品。有没有办法组合所有Model对象并将Product放在同一个Model中?
我在Model类中的变量如下所示:
public class Model implements java.io.Serializable {
private long id;
private ItemClass itemClass;
private OrderProcessorCode orderProcessorCode;
private String name;
private boolean active;
private long companyId;
@JsonManagedReference
private Set products = new HashSet(0);
private Set itemsConsumedPerModels = new HashSet(0);
private Set itemsInModels = new HashSet(0);
产品:
public class Product implements java.io.Serializable {
private long id;
private ItemClass itemClass;
private ItemType itemType;
private OrderProcessorCode orderProcessorCode;
private String number;
private String description;
private boolean baseProduct;
private boolean otmProduct;
private boolean active;
private long companyId;
private Set itemsInProducts = new HashSet(0);
@JsonBackReference(value = "product-models")
private Set models = new HashSet(0);
private Set optionsPerProductsForProductId = new HashSet(0);
private Set optionsPerProductsForOptionId = new HashSet(0);
private Set productionOrders = new HashSet(0);
答案 0 :(得分:0)
稍微更改了我的查询和代码。现在,所有查询过的产品都会自动添加到模型中。
Query query = session.createQuery("FROM Model m inner join fetch m.products p inner join p.itemsInProducts iip inner join iip.item i where i.number = '" + itemID +"' and m.active = '1' and p.active = '1' and iip.active = '1'");
ArrayList<Object[]> items = (ArrayList<Object[]>) query.list();
session.close();
for (Object[] objects : items) {
Model tempModel = (Model) objects[0];
if(!allmodels.contains(tempModel))
{
allmodels.add(tempModel);
}
}