HQL 对象命名参数

发布于 2024-11-28 15:13:39 字数 1571 浏览 0 评论 0原文

我想知道是否可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2024-12-05 15:13:39

只是不要将供应商实例传递给您的查询,而是直接传递供应商的名称:

String hql = "from Product as product where product.supplier.name = :supplierName";
Query query = session.createQuery(hql);
query.setString("supplierName", supplier.getName());
List results = query.list();
displayProductsList(results);

Just don't pass a Supplier instance to your query, but pass the supplier's name directly:

String hql = "from Product as product where product.supplier.name = :supplierName";
Query query = session.createQuery(hql);
query.setString("supplierName", supplier.getName());
List results = query.list();
displayProductsList(results);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文