如何在java hibernate中对象名参数?
String supplierHql = "from Supplier where name='Kamil'";
Query supplierQuery = session.createQuery(supplierHql);
Supplier supplierr = (Supplier) supplierQuery.list().get(0); // ilk değerini aldık
String hql = "from Product as product where product.supplier=:supplierr";
Query query = session.createQuery(hql);
List resulList = query.list();
答案 0 :(得分:2)
String hql = "from Product as product where product.supplier=:supplierr";
List result = session.createQuery(hql)
.setParameter("supplierr", "7277")
.list();
答案 1 :(得分:1)
String hql = "from Product as product where product.supplier='"+ supplier.getSupplier() + "'";
Query query = session.createQuery(hql);
List resulList = query.list();
答案 2 :(得分:0)
您可以使用namedQuery。 Exp:在您的实体中定义namedQueries。
@NamedQueries({
@NamedQuery(name="Product.findProductBySupplier",
query="from Product p where p.supplier = :supplier")})
/ * Hibernate 5之前的多个在namedQueries标签中定义了namedQuery,在5之后定义了许多未使用NamedQueries的namedQuery。 * /
Query query = session.getNamedQuery("Product.findProductBySupplier").setString("supplier", "kamil");
query.list(); //or uniqueResult();
你应该看看这个教程。
https://examples.javacodegeeks.com/enterprise-java/jpa-named-query-example/