Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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