#我怀疑是Product表中的partId可以为null,因此如果partId为null,我将无法看到该产品。如果我的产品表有11个条目,并且2个条目的partId为null,那么我只能看到9个条目
String hql = "from " + Product.class.getName() + " bs, "
+ Part.class.getName() + " dm, "
+ Manufacturer.class.getName() + " m where "
+ " m.id = bs.manufacturerId and dm.id = bs.partId ";
========================================
输出必须是这样的 productName | PartName |制造商名称
答案 0 :(得分:1)
你需要做左连接而不是内连接。但这只有在您的实体关联在一起而不是包含彼此的ID时才有可能。
原来,使用HQL几乎不可能。
根据您的查询,您可能应该在产品和制造商之间使用ManyToOne,在产品和零件之间使用ManyToOne。
此外,如果您没有连接类名并且使用了正确的别名,那么您的查询将更具可读性:
String hql = "from Product product, Part part, Manufacturer manufacturer"
+ " where manufacturer.id = product.manufacturerId"
+ " and part.id = product.partId";
一旦存在关联,查询应该只是
String hql = "select product.name, part.name, manufacturer.name"
+ " from Product product"
+ " left join product.part part"
+ " left join product.manufacturer manufacturer";