我有两种模式:
@Entity
public class Product{
@Id
private String ProductID;
private String ProductName;
@ManyToOne
@JoinColumn
private Supplier supplier;
private int quantity;
private int price;
//getter setter...
//constructor...
}
@Entity
public class Supplier {
@Id
private String SupplierID;
private String SupplierName;
private String Phone;
private String Address;
private String Email;
//getter setter
//constructor
}
在spring的jdbc模板中,我可以得到我想要的所有记录:
public List<Product> getProducts(int take, int skip){
List<Product> list = new ArrayList<Product>();
String sql = "SELECT product.ProductID, product.ProductName, product.Quantity, supplier.SupplierID"
+ ", supplier.SupplierName, product.Price"
+ " FROM product INNER JOIN supplier ON product.SupplierID = supplier.SupplierID"
+ " LIMIT " + skip + "," + take + "";
list = jdbcTemplate.query(sql, new ProductMapper());
return list;
public class ProductMapper implements RowMapper<Product> {
public Product mapRow(ResultSet rs, int line) throws SQLException {
Product product = new Product();
product.setProductID(rs.getString("ProductID"));
product.setProductName(rs.getString("ProductName"));
product.setQuantity(rs.getInt("Quantity"));
product.setSupplier(new Supplier(rs.getString("SupplierID"), rs.getString("SupplierName")));
product.setPrice(rs.getInt("Price"));
return product;
}
但它在休眠方面是错误的:
//--------hibernate-------
Session session = sessionFactory.openSession();
session.beginTransaction();
String queryStr = "SELECT Product.ProductID, Product.ProductName, Product.Quantity, Supplier.SupplierID"
+ ", Supplier.SupplierName, Product.Price"
+ " FROM Product INNER JOIN Supplier ON Product.SupplierID = Supplier.SupplierID";
List<Product> list = new ArrayList<Product>();
try {
Query query = session.createQuery(queryStr);
query.setFirstResult(skip);
query.setMaxResults(take);
list = (List<Product>) query.list();
session.getTransaction().commit();
}
catch(Exception e) {
System.out.println("Exception e" + e);
session.getTransaction().rollback();
}
finally {
session.close();
}
return list;
}
我得到&#34;加入的路径&#34;和异常eorg.hibernate.QueryException:无法解析路径[Product.ProductID],意外令牌[产品] [SELECT Product.ProductID ....] 当我使用jdbc模板时,任何人都可以帮助我获得相同的结果。
答案 0 :(得分:0)
SELECT p.ProductID, p.ProductName, p.Quantity, s.SupplierID, s.SupplierName, p.Price
FROM Product p
INNER JOIN p.Supplier s
您需要在HQL查询中使用路径,从一个实体到另一个实体。有关HQL和连接的Hibernate文档提供了更多信息。
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-joins
答案 1 :(得分:0)
String sql =&#34; SELECT product.ProductID,product.ProductName, product.Quantity,supplier.SupplierID&#34; +&#34;,supplier.SupplierName,product.Price&#34; +&#34; FROM产品INNER JOIN供应商ON product.SupplierID = supplier.SupplierID&#34; +&#34;限制&#34; +跳过+&#34;,&#34; +拿+&#34;&#34;;
如果您使用的是sql查询,则需要提供SQL表和sql表列, 但是在这里你要混合使用SQL表和Java类 product可能是一个SQL表,Product是实体类。
因此请将您的查询更改为
String sql = "SELECT p.ProductID, p.ProductName, p.Quantity, s.SupplierID"
+ ", s.SupplierName, s.Price"
+ " FROM Product p INNER JOIN Supplier s ON p.SupplierID = s.SupplierID"
+ " LIMIT " + skip + "," + take + "";
另外,你的变量名不在camelCase中,这可能会破坏Hibernate将实体属性映射到表列的方式,所以要特别注意getter和setter。