简体   繁体   中英

Retriving records from database using hibernate

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM