大家好,我是Hibernate的新手,我使用HQL使用SQL查询时出错了。 我使用带有hibernate和Postgres DBMS的spring mvc app。
SQL查询" SELECT c.quantity,p.name,p.price FROM cart AS c INNER JOIN products AS p ON c.product_articul = p.articul WHERE user_id = 1"
正如您所见,我已经获得了包含列的购物车表:cart_id,user_id,product_articul,quantity。适当的购物车类:
@Entity
@Table(name = "cart")
public class Cart {
@Id
@Column(name = "cart_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cartId;
@Column(name = "user_id")
private int userId;
@Column(name = "product_articul")
private String productArticul;
@Column(name = "quantity")
private int quantity;
@OneToOne
private Product product;
// constructors,get,set
}
产品表包含product_id,名称等。产品类别:
@Entity
@Table(name = "products")
public class Product {
@Id
@Column(name = "product_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String productId;
@Column(name = "articul")
private String articul;
@Column(name = "name")
private String name;
@Column(name = "price")
private BigDecimal price;
//other fields, constructor, get, set
}
当我尝试使用此选项从购物车获取列表时:
List<Cart> list = session.createQuery("SELECT c FROM Cart c INNER JOIN c.product WHERE user_id = :id")
.setParameter("id", id).list();
我抓住了这个例外 - &gt;
javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法提取ResultSet
可能是postgres序列(product_id)的问题
PS。没有一对一的注释,一切正常。
我该如何解决这个问题。我会感谢你所有的答案!