Hibernate 标准——别名
我在 Hibernate 中的别名概念上遇到了一些困难。
我的情况如下:
订单
@OneToMany(cascade=CascadeType.ALL,mappedBy="m_order")
private Set<OrderDetail> m_details;
订单详细信息
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="product_id")
private Product m_product;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="order_id")
private Order m_order;
DAO
c.createAlias("m_details", "detail").createCriteria("detail.m_product").add(Expression.idEq(productId));
所以我想搜索包含产品的每个订单。
然而,通过这个查询,它不断返回 0 个订单,我并没有真正看到我做错了什么。
谢谢!
I'm struggling a bit with the concept of alias in Hibernate.
My situation is the following:
Order
@OneToMany(cascade=CascadeType.ALL,mappedBy="m_order")
private Set<OrderDetail> m_details;
OrderDetail
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="product_id")
private Product m_product;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="order_id")
private Order m_order;
DAO
c.createAlias("m_details", "detail").createCriteria("detail.m_product").add(Expression.idEq(productId));
So I want to search every order that contains a product.
However, with this query it keeps returning 0 orders and I don't really see what I'm doing wrong.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该查询对我来说看起来没问题......
尝试将“hibernate.show_sql”设置为“true”,这样您实际上就可以在 System.out 中看到 SQL
或/并记录它 log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER
@lars 是的,你可以。 条件 API - 关联
别名只是全名/路径的简称
carCriteria.createAlias("car_parts.wheels", "车轮")
the query looks okay to me...
try to set "hibernate.show_sql" to "true" so you actually can see the SQL in the System.out
or/and log it log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER
@lars yes you can. Criteria API - Associations
alias is just a shortname of a full name/path
carCriteria.createAlias("car_parts.wheels", "wheels")
我不确定您是否可以对非列的内容使用别名,即没有
@Column
或@JoinColumn
注释。I'm not sure you can use alias for stuff that is not a column, ie does not have the
@Column
or@JoinColumn
-annotation.这看起来是正确的。
在您的 DAO 部分中,您已经有一个名为“c”的变量 - 您可以发布初始化该变量的代码吗?这只是为了仔细检查您是否正在使用 Order.class 创建原始条件。
然后接下来要检查是否可以通过以下方式检索具有该 id 的产品:
Product p = (Product)session.load(Product.class, ProductId)
这样您就可以检查id 是正确的,Hibernate 可以找到该产品。
如果做不到这一点,我们就必须按照其他评论者的建议开始查看生成的 SQL。
That looks correct.
In your DAO section, you already have a variable named 'c' - can you post the code where this is initialized? That's just to double check that you're creating the original criteria with the Order.class.
Then the next thing to check if you can retreive the product with that id with the following:
Product p = (Product)session.load(Product.class, productId)
That way you're checking that the id is correct and Hibernate can find that product.
Failing that we'd have to start looking at the generated SQL as the other commenters have suggested.