HQL 对象命名参数
我想知道是否可以从 HQL 中名为参数的对象中检索特定列。
如
public class Product
{
private int id;
private Supplier supplier;
private String name;
private String description;
private double price;
public Product()
{
super();
}
public Product(String name, String description, double price)
{
super();
this.name = name;
this.description = description;
this.price = price;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Supplier getSupplier()
{
return supplier;
}
public void setSupplier(Supplier supplier)
{
this.supplier = supplier;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
}
您所见,我创建了产品,并且其中有一个供应商对象。 那么,当我进行 HQL 并打电话时
String hql = "from Product as product where product.supplier=:supplier";
Query query = session.createQuery(hql);
query.setEntity("supplier",supplier);
List results = query.list();
displayProductsList(results);
,是否可以只获取产品供应商的名称? 而不仅仅是整个供应商?
I was wondering if it is possible to retrieve a specific column from an object named parameter in HQL.
Example
public class Product
{
private int id;
private Supplier supplier;
private String name;
private String description;
private double price;
public Product()
{
super();
}
public Product(String name, String description, double price)
{
super();
this.name = name;
this.description = description;
this.price = price;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Supplier getSupplier()
{
return supplier;
}
public void setSupplier(Supplier supplier)
{
this.supplier = supplier;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
}
as you can see I created the product and it has a supplier object within it.
So when I do HQL and call
String hql = "from Product as product where product.supplier=:supplier";
Query query = session.createQuery(hql);
query.setEntity("supplier",supplier);
List results = query.list();
displayProductsList(results);
but is it possible to just get product's supplier's name?
and not just the whole supplier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是不要将供应商实例传递给您的查询,而是直接传递供应商的名称:
Just don't pass a Supplier instance to your query, but pass the supplier's name directly: