0

I have a class Trade and a subclass Operation. They are mapped in the database. So when I do:

trades = session.createQuery("from Trade").list() 

I get an arraylist of trade records and can access the operations for certain trade through my trade instance.

However, when I do:

trades = session.createQuery("
    from Trade as trade
    inner join trade.operations as operation
    with to_char(operation.datetime, 'yyyyMMdd') =  to_char(sysdate, 'yyyyMMdd')
    order by operation.datetime"
).list();

I get an array of objects where each element contains operation and trade instance.

How could I retrieve records as an array of trades (same as first option) with conditions (same as second option)?


I tried: select trade from Trade as trade inner join trade.operations as operation with to_char(operation.datetime, 'yyyyMMdd') = to_char(sysdate, 'yyyyMMdd') order by operation.datetime

It worked, thanks javatestcase. However, when I loop the trade.operations, I get trades which contain any operations by today, but I also get all the operations for that trade, even if it's the operation is from another day. So it doesn't satisfy the condition.

Any clue?

Thanks in advance!!

1
  • 1
    have you tried adding "select trade" from... Commented Feb 2, 2012 at 1:19

1 Answer 1

1

trade.operations always contain all operations for that trade. hibernate won't give you trades with partial initialized child collection because it breaks change tracking and also leads to many confusions down the road. So the best hibernate can do is to give you pairs of trades and matching operations. I would create a class containing all the properties you need from trade and matching operation and use AliasToBeanTransformer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. It's really useful. I used to use SQLAlchemy a library pretty similar to hibernate in Python. As far as I remember SQLAlchemy allows you to retrieve collections in base on conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.