-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem quering ono-to-one relationship
PostPosted: Tue Jul 12, 2005 7:22 am 
Hello, i have little problem (searching forum for "one-to-one" didn't helped) and just want to be sure that im doing "right thing (tm)"....

I have two tables with 1 --> 0* relationship. Master table has generated ID and second table has ID assigned by "foreign" generator. Both entities are mapped to their counterparts by one-to-one mapping:

Code:
<class name="Cra.VubScoringEngine.BLL.FinancialStatement, Cra.VubScoringEngine.BLL" table="FinancialStatement">
  <id name="Id" type="Int32" unsaved-value="0">
    <column name="Id" sql-type="int"/>
    <generator class="native" />
  </id>
  <one-to-one name="Score" class="Cra.VubScoringEngine.BLL.FinancialStatementScore, Cra.VubScoringEngine.BLL" cascade="all"/>      
</class>


Code:
<class name="Cra.VubScoringEngine.BLL.FinancialStatementScore, Cra.VubScoringEngine.BLL" table="FinancialStatementScore">
  <id name="Id" type="Int32" unsaved-value="0">
    <column name="Id" sql-type="int" not-null="true" unique="true" index="PK_FinancialStatementScore"/>
    <generator class="foreign">
      <param name="property">FinancialStatement</param>
    </generator>
  </id>
  <one-to-one name="FinancialStatement" class="Cra.VubScoringEngine.BLL.FinancialStatement, Cra.VubScoringEngine.BLL" constrained="true" >
</class>


Everything works just fine :)
BUT, now I want to find entities from FinancialStatement table with no corresponding entity in FinancialStatementScore table. So I run following query:
Code:
"SELECT fs FROM FinancialStatement as fs WHERE (fs.CisId = ?) AND (fs.Score is not null) ORDER BY fs.Year asc, fs.Type.Id asc"

But NHibernate creates SQL query which does not reference second table at all! "fs.Score is not null" is translated to "fs.Id is not null" (which is of course allways true). It seems like NHibernate is "too smart" here :))
I've found workaround by using this query:
Code:
"SELECT fs FROM FinancialStatement as fs WHERE (fs.CisId = ?) AND (fs.Score.ScoreValue is not null) ORDER BY fs.Year asc, fs.Type.Id asc"

But I am still currious :) Am I just using one-to-one mapping in the wrong way or is that bug ? Is there any better way to map ono-to-oneorzero relationships ?

Thanx for Your time !


Top
  
 
 Post subject: re: Problem quering ono-to-one relationship
PostPosted: Tue Jul 12, 2005 3:36 pm 
I've seen this too. Some times the best way is to use an OUTER JOIN from the FinancialStatementScore object back to FinancialStatement


Top
  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:30 am 
Newbie

Joined: Thu May 12, 2005 10:40 am
Posts: 16
I had a similar problem,
lead me to something like:
Code:
select from FinancialStatement st where not exists (select from FinancialStatementScore sc where st = sc)


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