views:

690

answers:

3

Hello,
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!

A: 

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

Lars Andren
+1  A: 

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

dube
When I started to filter out some parts to see where it went wrong I found out it was a mistake in some other part that influenced the outcome.So thanks :-)
Ignace
A: 

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.

Rachel