-->
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: Want to do the join myself
PostPosted: Sun Jul 06, 2008 9:57 am 
Newbie

Joined: Sun Jul 06, 2008 9:47 am
Posts: 3
Hello,

I have got two tables which I want to join. The column where the join occures can be null so I won't get results if I do the join like A.Bid = B.id. So my statement should look like this: A.Bid = B.id OR A.Bid IS NULL but Hibernate creates A.Bid = B.id automatically and of course does not use OR to connect the next where clause.
How can I say Hibernate not to join the tables as it wants but as I want?

Statement:
SELECT DISTINCT ap, ap.Price_Period_Type.timeUnits, ap.id FROM com.example.Article_Price ap, com.example.Price_Period_Type as ppt WHERE ap.Price_Period_Type.id = ppt.id OR ap.Price_Period_Type.id IS NULL AND ap.Price_Season.id = :psid ORDER BY ap.Price_Period_Type.timeUnits, ap.id DESC")

Thank you!
Markus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 9:10 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, is there a natural join between the tables? I mean, a foreign key relationship? And from there, you have your objects mapped?

A criteria query would be a snap in this scenario. Also, if we knew your object model, a simple HQL query would do it too.

What have you tried?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 2:46 pm 
Newbie

Joined: Sun Jul 06, 2008 9:47 am
Posts: 3
I posted the HQL above. Here are the mapping files:

Code:
<hibernate-mapping>
   <class name="com.example.Article_Price" table="Article_Price" lazy="false">
      <id name="id" column="id" type="integer" unsaved-value="0">
         <generator class="increment">
         </generator>
      </id>
      <many-to-one name="Price_Period_Type" cascade="lock" column="Price_Period_Typeid" class="com.example.Price_Period_Type" not-null="false" lazy="no-proxy" access="field">
      </many-to-one>
      <many-to-one name="Price_Season" cascade="lock" column="Price_Seasonid" class="com.example.Price_Season" not-null="true" lazy="no-proxy" access="field">
      </many-to-one>
      <many-to-one name="Currency" cascade="lock" column="Currencyid" class="com.example.Currency" not-null="true" lazy="no-proxy" access="field">
      </many-to-one>
      <property name="value" column="value" type="float" not-null="false" lazy="false"/>
      <property name="validFrom" column="validFrom" type="timestamp" not-null="true" lazy="false"/>
      <property name="validUntil" column="validUntil" type="timestamp" not-null="false" lazy="false"/>
   </class>
</hibernate-mapping>


and

Code:
<hibernate-mapping>
   <class name="com.example.Price_Period_Type" table="Price_Period_Type" lazy="true">
      <meta attribute="class-description">
         Kapselt Zeitspannen (pro Tag/pro Woche/pro Monat)...
      </meta>
      <id name="id" column="id" type="integer" unsaved-value="0">
         <generator class="increment">
         </generator>
      </id>
      <property name="name" column="name" type="string" length="255" not-null="true" lazy="false"/>
      <property name="timeUnits" column="timeUnits" type="integer" not-null="true" lazy="false">
         
      </property>
      <set name="Article_Price" lazy="extra" cascade="save-update,lock" inverse="true">
         <key column="Price_Period_Typeid" not-null="false"/>
         <one-to-many class="com.example.Article_Price"/>
      </set>
   </class>
</hibernate-mapping>


So as you can see the Price_Period_Type may be NULL but I also want to select the Article_Price if this is so and the Price_Seasonid is correct. That's why I used the OR in the HQL but Hibernate creates another join itself which leads to not returning the Article_Prices where the Price_Period_Type is not specified.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:43 am 
Newbie

Joined: Sun Jul 06, 2008 9:47 am
Posts: 3
What I forgot to say. The DISTINCT is not the problem. I tried the folloing HQL as well:

SELECT ap FROM com.example.Article_Price ap, com.example.Price_Period_Type as ppt WHERE (ap.Price_Period_Type.id = ppt.id OR ap.Price_Period_Type.id IS NULL) AND ap.Price_Season.id = :psid ORDER BY ap.Price_Period_Type.timeUnits DESC


and it does not work too.


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.