java Hibernate 标准——别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2543328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:41:48  来源:igfitidea点击:

Hibernate criteria -- alias

javahibernateormhibernate-criteria

提问by Ignace

I'm struggling a bit with the concept of alias in Hibernate.
My situation is the following:
Order

我在 Hibernate 中对别名的概念有点挣扎。
我的情况如下:下

@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!

所以我想搜索包含产品的每个订单。
但是,通过这个查询,它不断返回 0 个订单,我真的不明白我做错了什么。
谢谢!

采纳答案by dube

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

查询对我来说看起来没问题...尝试将“hibernate.show_sql”设置为“true”,这样您实际上可以在 System.out 中看到 SQL 或/并记录它 log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER

@lars yes you can. Criteria API - Associationsalias is just a shortname of a full name/path carCriteria.createAlias("car_parts.wheels", "wheels")

@lars 是的,你可以。Criteria API - 关联别名只是全名/路径的简称 carCriteria.createAlias("car_parts.wheels", "wheels")

回答by Rachel

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.

在你的 DAO 部分,你已经有一个名为“c”的变量——你能把它初始化的代码贴出来吗?这只是为了仔细检查您是否正在使用 Order.class 创建原始条件。

Then the next thing to check if you can retreive the product with that id with the following:

然后接下来要检查您是否可以使用以下 ID 检索产品:

Product p = (Product)session.load(Product.class, productId)

Product p = (Product)session.load(Product.class, productId)

That way you're checking that the id is correct and Hibernate can find that product.

这样您就可以检查 id 是否正确,并且 Hibernate 可以找到该产品。

Failing that we'd have to start looking at the generated SQL as the other commenters have suggested.

否则,我们将不得不按照其他评论者的建议开始查看生成的 SQL。

回答by Lars Andren

I'm not sure you can use alias for stuff that is not a column, ie does not have the @Columnor @JoinColumn-annotation.

我不确定您是否可以对不是列的内容使用别名,即没有@Columnor@JoinColumn注释。