-->
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.  [ 4 posts ] 
Author Message
 Post subject: I need help to set a where clause correct.
PostPosted: Mon Mar 27, 2006 7:55 am 
Newbie

Joined: Mon Mar 27, 2006 7:15 am
Posts: 2
Hibernate version:
3.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 10g

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi!

In my case I have a class Eiendom who has a set of kvotes,
from Eiendom.hbm.xml:
<set name="kvotes" inverse="true" lazy="false" >
<key>
<column name="EIE_EIE_ID" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="com.tec.slf.melk.shared.persistent.Kvote" />
</set>


I want to do a select with where clauses on both Eiendom and Kvote
and my HQL goes like this:


FROM Eiendom as e where 1=1 and komnr ='0438' and gardsnr =11 and bruksnr =47 and e.kvotes.aktiv =1


I thought I could use e.kvotes.aktiv to set the where clause for the Kvote,
but the SQL I get from it is:


select eiendom0_.EIE_ID as EIE1_6_, eiendom0_.LBE_ID as LBE2_6_, eiendom0_.KOMNR as KOMNR6_, eiendom0_.GARDSNR as GARDSNR6_, eiendom0_.BRUKSNR as BRUKSNR6_, eiendom0_.FESTENR as FESTENR6_, eiendom0_.EIENDOMNAVN as EIENDOMN7_6_, eiendom0_.ENDR_DATO as ENDR8_6_, eiendom0_.ENDR_BRUKER as ENDR9_6_ from MELK.M_EIENDOM eiendom0_, MELK.M_KVOTE kvotes1_ where eiendom0_.EIE_ID=kvotes1_.EIE_EIE_ID and 1=1 and eiendom0_.KOMNR='0438' and eiendom0_.GARDSNR=11 and eiendom0_.BRUKSNR=47 and kvotes1_.AKTIV=1


And for kvotes:


select kvotes0_.EIE_EIE_ID as EIE4_1_, kvotes0_.KVO_ID as KVO1_1_, kvotes0_.KVO_ID as KVO1_18_0_, kvotes0_.DRI_DRI_ID as DRI2_18_0_, kvotes0_.AAR_AAR as AAR3_18_0_, kvotes0_.EIE_EIE_ID as EIE4_18_0_, kvotes0_.KVT_KVT_ID as KVT5_18_0_, kvotes0_.TIL_TIL_ID as TIL6_18_0_, kvotes0_.DATO_FRA as DATO7_18_0_, kvotes0_.DATO_TIL as DATO8_18_0_, kvotes0_.KOMMENTAR as KOMMENTAR18_0_, kvotes0_.KVOTE_DISPONIBEL as KVOTE10_18_0_, kvotes0_.KVOTE_TOTAL as KVOTE11_18_0_, kvotes0_.FRITAK_LOK_FOREDLING as FRITAK12_18_0_, kvotes0_.ENDR_DATO as ENDR13_18_0_, kvotes0_.ENDR_BRUKER as ENDR14_18_0_, kvotes0_.DATO_SIST_ENDRET as DATO15_18_0_, kvotes0_.BRUKERIDENT as BRUKERI16_18_0_, kvotes0_.AKTIV as AKTIV18_0_ from MELK.M_KVOTE kvotes0_ where kvotes0_.EIE_EIE_ID=139


As you can see the where clause end up in the eiendom sql.
What am I doing wrong?
Thank you very much for your time :)

Best regards, Norunn


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 10:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Is this what you're looking for?
Code:
select e
FROM Eiendom e
join e.kvotes k
where e.komnr ='0438'
  and e.gardsnr =11
  and e.bruksnr =47
  and k.aktiv =1


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 3:08 am 
Newbie

Joined: Mon Mar 27, 2006 7:15 am
Posts: 2
Thank you for looking into this.
That query gives me the same as I already got.
The correct eiendom, with all connected kvotes, both aktiv=1 and
aktiv=0, the where clause is still set on the eiendom select and not
the kvotes. The query looks correct, can it be that the error lies in the mapping files?

Eiendom.hbm.xml:

<set name="kvotes" inverse="true" lazy="false" >
<key>
<column name="EIE_EIE_ID" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="com.tec.slf.melk.shared.persistent.Kvote" />
</set>

and Kvote.hbm.xml:

<many-to-one name="eiendom" class="com.tec.slf.melk.shared.persistent.Eiendom" fetch="select">
<column name="EIE_EIE_ID" precision="9" scale="0" not-null="true" />
</many-to-one>

Regards,
Norunn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 5:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Àh I see what you mean.

The problem is that you're expecting a query for java objects to return database rows (converted to java objects). That is, you want a "bit of" the Eiendom objects: you want to exclude some of their kvotes. You need a different query for that.

Your options are:
1) Get the kvotes seperately:
Code:
select e, k
FROM Eiendom e
join e.kvotes k
where e.komnr ='0438'
  and e.gardsnr =11
  and e.bruksnr =47
  and k.aktiv =1
This query will return a list of Object[2]s, so code like this would be required to get at the contents:
Code:
for (Object[] ret : qry.list())
{
  Eiendom e = (Eiendom) ret[0];
  Kvote k = (Kvote) ret[1];
  // Do stuff with objects
}
What you're actually interested in are the Kvotes, and this query returns the correct ones, paired with the Eiendoms that hold them.

2) Just get the Kvotes, if Kvotes has a link back to Eiendom:
Code:
select k
FROM Eiendom e
join e.kvotes k
where e.komnr ='0438'
  and e.gardsnr =11
  and e.bruksnr =47
  and k.aktiv =1


3) Switch to Criteria and use the ROOT_ALIAS transformer. Though that's basically the same as option 1, just using Criteria instead of HQL.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.