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

private Set<OrderDetail> m_details; 


    private Product m_product;
    private Order m_order;


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.

Was it helpful?


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, 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")


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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow