-->
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.  [ 5 posts ] 
Author Message
 Post subject: Complex SQL code in the where attribute in set element
PostPosted: Wed Oct 03, 2007 11:50 am 
Newbie

Joined: Tue Oct 02, 2007 6:41 am
Posts: 4
Hi,

When loading collections I need to filter some instances (to control orphans in an inheritance hierarchy). For doing so I have the following SQL code (which has a good performance in my MySQL 5.0.1 server)

Code:
SELECT *
FROM PDBElement
WHERE
idCode="1AGI" and pdbElementId IN
(
SELECT pdbElementId FROM
(
(SELECT P.pdbElementId FROM PDBElement P, Atom A
WHERE A.pdbElementId=P.pdbElementId AND idCode = "1AGI") UNION
(SELECT P.pdbElementId FROM PDBElement P, Residue R
WHERE R.pdbElementId=P.pdbElementId AND idCode = "1AGI")
) as q
)


1AGI is the id of the object where the collection is (so it should be a parameter in the where attribute of the mapping file). PDBElment is an abstract superclass and Atom and Residue are concrete subclasses.

These are the important parts of the mapping file:

Code:
<hibernate-mapping>
   <class name="org.mmb.model.PDBEntry" table="entries">
      <id name="idCode" type="string"/>

...
              <set name="pdbElements" inverse="true" where="I_want_my_previous_query_here">
         <key column="idCode"/>         
         <one-to-many class="org.mmb.model.PDBElement"/>      
      </set>

...


I've tried many different aproaches but it seems that Hibernate does not generate the SQL exactly how is written. It interpretes the SQL code and it seems not to like things like UNION or alias in subqueries.

Another proble is that I don't know how to specify parameters in the where attribute. I've tried this:

Code:
              <set name="pdbElements" inverse="true" where="
pdbElementId IN (SELECT pdbElementId FROM((SELECT P.pdbElementId FROM PDBElement P, Atom A WHERE A.pdbElementId=P.pdbElementId AND idCode = ?) UNION (SELECT P.pdbElementId FROM PDBElement P, Residue R WHERE R.pdbElementId=P.pdbElementId AND idCode = ?)) as q)">



But then in the logs I receive a message that hibernate cannot bind these additional parameters...

I suppose I cannot parametrize the SQL code in the where attribute, but not sure...

Any ideas?

Thanks,


Top
 Profile  
 
 Post subject: did you find an answer for parameterization?
PostPosted: Tue Feb 12, 2008 11:41 am 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
did you find an answer for parameterization?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 11:58 am 
Newbie

Joined: Tue Oct 02, 2007 6:41 am
Posts: 4
Unfortunately not.

I performed a workaround with native SQL

Iván


Top
 Profile  
 
 Post subject: That's too bad...
PostPosted: Tue Feb 12, 2008 12:06 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
Can someone from hibernate confirm that there is no way to do this...

i.e:

Parent = class A
class A has collection childObjects
I want child objects to have a where clause that references class A field

In my case childObjects contains instances of class A as well so I basically want the child object not to include the parent again.

i.e:

A pk 1
B pk 2
C pk 3

A pk1 has child objects (pk1, pk2, pk3 (but want pk1 eliminated))


Top
 Profile  
 
 Post subject: ivanator..
PostPosted: Wed Feb 13, 2008 1:52 am 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
ivanator did you do work around in mapping file or in code?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

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.