-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Attempting to use new Subquery support for Criteria
PostPosted: Tue Feb 15, 2005 5:18 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Hibernate version:
Hibernate 3 beta 4

I'm trying to use the following Detached Criteria to use as a subQuery and am encountering some weirdness I can't quite figure out.

Based on executing this by itself:
Code:
DetachedCriteria subQueryCriteria = DetachedCriteria.forClass(AgmtContractLine.class);
        subQueryCriteria.createAlias(AgmtContractLine.VERSIONS_PROPERTY, AgmtContractLine.VERSIONS_PROPERTY);
        subQueryCriteria.add(Restrictions.eq(AgmtContractLine.VERSIONS_PROPERTY + "." + AgmtContractLine.END_USE_PROPERTY, OmniType.END_USE_SUBTYPE_SALES_TYPE));
        subQueryCriteria = subQueryCriteria.setProjection(Property.forName(AgmtContractLine.VERSIONS_PROPERTY + "." + AgmtContractLine.SOURCE_LOCATION_PROPERTY));


I get the following SQL:
Code:
select versions1_.SRC_ADDR_ID as y0_
  from AGMT_CNTRCT_LINE this_ inner join AGMT_CNTRCT_LINE_V versions1_ on this_.AGMT_CNTRCT_LINE_ID = versions1_.AGMT_CNTRCT_LINE_ID
where versions1_.END_USE_CODE = 'SALES'



Which is perfect so far. So I'm getting a list of SRC_ADDR_IDs from a joined table where END_USE_CODE is sales. What isn't shown here is a layer of filtering that applies to the VERSIONS_PROPERTY but I'm trying to get this working for a simple case first.

So, expanding on this I take the subquery and wind up with this:
Code:
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Location.class);

        DetachedCriteria subQueryCriteria = DetachedCriteria.forClass(AgmtContractLine.class);
        subQueryCriteria.createAlias(AgmtContractLine.VERSIONS_PROPERTY, AgmtContractLine.VERSIONS_PROPERTY);
        subQueryCriteria.add(Restrictions.eq(AgmtContractLine.VERSIONS_PROPERTY + "." + AgmtContractLine.END_USE_PROPERTY, OmniType.END_USE_SUBTYPE_SALES_TYPE));
        subQueryCriteria = subQueryCriteria.setProjection(Property.forName(AgmtContractLine.VERSIONS_PROPERTY + "." + AgmtContractLine.SOURCE_LOCATION_PROPERTY));

        detachedCriteria.add(Subqueries.in(Location.ADDR_ID_PROPERTY, subQueryCriteria));


Which generates a sql query with the following WHERE clause (I've cut out the rest since all it contains is a bunch of column selects from my ADDRESS table (mapped to Location.class):
Code:
from ADDRESS this_
where ? in (select versions1_.SRC_ADDR_ID as y0_
               from AGMT_CNTRCT_LINE this0__
              where versions1_.END_USE_CODE = ?)


What I was hoping to get was something along the lines of:
Code:
from ADDRESS this_
where addr_id in (select versions1_.SRC_ADDR_ID as y0_ from AGMT_CNTRCT_LINE this0__ inner join AGMT_CNTRCT_LINE_V versions1_ on this_.AGMT_CNTRCT_LINE_ID = versions1_.AGMT_CNTRCT_LINE_ID WHERE versions1_.END_USE_CODE = 'SALES')



So, in this case I end up pondering 4 things:

1 - What have I done so that the inner join doesn't get generated in the SubQuery?
2 - The SRC_ADDR_ID is actually mapped to a Location instance so I get a class cast trying to compare the ADDR_ID which is a Long to the Location.class
Is there any way to accomplish this?
3 - since the where clause is built up with "Where ? in (select....." does that mean the subquery is actually going to cause a separate select for each Location I have? i.e. will it load all locations and then try to match them based on the subQuery.
4 - Am I missing something so basic that gavin will appear and beat me soundly with a stick?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try using Subqueries.propertyIn()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 5:42 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Ok, you got me halfway there. Using propertyIn I ended up with

Code:
where this_.ADDR_ID in
       (select versions1_.SRC_ADDR_ID as y0_
          from AGMT_CNTRCT_LINE this0__
         where versions1_.END_USE_CODE = ?)


And now it didn't complain about the class cast but I'm still missing the inner join. I hate to ask but a bug perhaps?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Before you do anything, try without those strange Constants you are using everywhere and repost - I am totaly unable to read that stuff properly.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 6:18 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
All the strange constants are simply strings, but will do. The stange thing is that the subquery executed by itself will generate the inner join based on the createAlias() but passed as a property into the propertyIn() it doesn't.

Code:
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Location.class);

        DetachedCriteria subQueryCriteria = DetachedCriteria.forClass(AgmtContractLine.class);
        subQueryCriteria.createAlias("versions", "versions");
        subQueryCriteria.add(Restrictions.eq("versions.endUse", "Sales"));
        subQueryCriteria = subQueryCriteria.setProjection(Property.forName("versions.sourceLocation"));

        detachedCriteria.add(Subqueries.propertyIn("addrId", subQueryCriteria));


Gives me the following where clause (without the expected inner join):
Code:
from ADDRESS this_
where this_.ADDR_ID in
       (select versions1_.SRC_ADDR_ID as y0_
          from AGMT_CNTRCT_LINE this0__
         where versions1_.END_USE_CODE = ?)


Where if the subQueryCriteria is executed standalone I get (with the expected inner join):
Code:
select versions1_.SRC_ADDR_ID as y0_
  from AGMT_CNTRCT_LINE this_ inner join AGMT_CNTRCT_LINE_V versions1_ on this_.AGMT_CNTRCT_LINE_ID = versions1_.AGMT_CNTRCT_LINE_ID
where versions1_.END_USE_CODE = ?


When executed as an actual subquery I the following exception (which is expected since I don't end up with the version1_ alias from the join):
Caused by: java.sql.SQLException: ORA-00904: "VERSIONS1_"."END_USE_CODE": invalid identifier

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:579)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1894)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:831)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2496)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2840)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:608)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:536)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:108)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1158)
at org.hibernate.loader.Loader.doQuery(Loader.java:357)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:199)
at org.hibernate.loader.Loader.doList(Loader.java:1417)
... 31 more


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 6:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hm, this might be a bug - give Gavin some time to reply here, and file a JIRA entry sometimes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 6:34 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Thanks for taking the time to prod me in the right direction Michael. I'll log a JIRA if gavin either
a)requests one
or
b)he doesn't reply to the thread

but before I execute on b i'll take a peek up into the bowels to try to figure out what's going on. I saw some comments regarding the not allowing implicit joins in the code but I would assume I would be able to walk a table via the alias.

Using the subquery will save me from adding a special association and implementing a new set of filters. (assuming filters get applied on the subQuery in the first place :))

I was quite happy to see some movement in the Criteria API the past couple of releases.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Taking a look at the source code is definitely a good idea ;) Have patience with the new APIs :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 7:04 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Oh, I've got patience, don't get me wrong there. I'm not screaming bloody murder or anything. I just wanted to determine that I wasn't absolutely daft before I went barking up any trees.

Most of the stuff works fine, especially with the test examples. But I didn't see any tests that were doing quite what I wanted to do so I had to question my thought pattern. The in() vs. the propertyIn() was a case in point. I completely missed that.

I was just excited to try out the new features and if I get a chance to help you guys knock out the cobwebs then that's what it's all about.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
That was no critisism, people exploring the new API and well done posts about things that are not working as excpected are always welcome.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 10:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
For now, joins are not possible in Criteria subselects. I need to do a little bit of a refactor of the OuterJoinLoader class and subclasses to make that possible. I hope to get this done "soon".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 11:37 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
No problem gavin, just wanted to make sure I wasn't going simple or that it's on the books as it seemed a bit off that it didn't work.

I'm highly anticipating this as it should help me dump some associations that are there simply so we can do the criteria queries.

Criteria API is getting a massive shove in the right direction as far as I'm concerned.

As a bit of an aside, does this also mean that there are limitations with the grouping work as well?

PS, any more thoughts on that FetchMode.IMMEDIATE_SELECT? :D I can fire it off as an enhancement request if it helps you keep track of it better.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 4:15 pm 
Newbie

Joined: Sat Apr 02, 2005 2:17 am
Posts: 11
Has this been fixed yet?

All I can find on the JIRA is a bug which is closed as a duplicate, but no indication what it's a duplicate of. I could really do with finding out whether it's been fixed, as we might have to radically change our implementation if it's not fixed and isn't likely to be.

Unfortunately I can't easily test it with Hibernate 3.0.3 as the Annotations project we're also relying on doesn't work with 3.0.3 yet...

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 4:46 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Don't believe so, I had actually opened another thread about this 2 weeks ago, forgetting that I had one open from before :)

It's the alzheimer's setting in, I swear.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 4:59 pm 
Newbie

Joined: Sat Apr 02, 2005 2:17 am
Posts: 11
Rats. Ah well.

I wonder - is there likely to be any point in me trying to fix the bug myself? I've done a certain amount of patching on open source projects before, but the Hibernate code base is probably rather more complex than anything I've tackled before...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.