Querying indirectly associated entities in HQL - join

NB: I am using NHibernate 2.1 (for legacy code support)
If I have three entities:
A related to B
C related to B
That is, having the following properties:
Entity A
A
- id
- B
Entity B
B
- id
Entity C
C
- id
- B
How do I do it using a non-theta-style join?
If I try something like this
from A a
inner join a.B b
inner join C.b cB
where
a.B_id = b.id and
cB.id = b.id
I get the exception: Path expected for join!
Is a theta join the only way I can get it to work or am I missing something?
If I have no direct association from A to C why can I not associate through another entity? Or does this not work?

Related

Solr outer join / not join query

I may be asking too much but I want to do a left outer join between two cores
and get data from A only where B does not have related data.
Following is exactly my equivalent SQL query (for simplicity I have removed other conditions),
1. SELECT A.* FROM A AS A
WHERE A.ID NOT IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I understand that solr join is actually subquery, I need data from only A.
It would be very easy if the not was not there in where condition for sub query.
For example,
2. SELECT A.* FROM A AS A
WHERE A.ID IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I can have q={!join from=aId to=id fromIndex=b}(-statusId:1).
How can I do a nagete here, i.e. solr query for 1

Using self join and left outer join on the same table?

Newbie with SQL development. I got this strange scenario where I want to join 3 tables Table A,B,C. The use case is to return column X which is a primary key in table C. The column X is also a FK in table A and B.
Now I want to create a view by left joining all the 3 tables. The view has have 4 columns, A.id, B.id, C.A_X, C.B_X
The users can either use A.id or B.id to get the data. Now that's the scenario.
How should I join these tables so that I don't miss any values for C.X for every A.id and B.id.
Sample results:
A.id B.id C.A_X C.B_X
1 null ABC null
null 2 null XYZ
Cheers!!

Doctrine join WITH vs WHERE

I'm joining multiple tables and I found two solution. I can't ready figure out what is the diference. Can anyone tell me?
Solution 1:
$query = $entityManager->createQuery("
SELECT m FROM Model m
JOIN m.battery b
JOIN m.camera c
JOIN m.connectivity co
JOIN m.hardware h
JOIN m.screen s
JOIN m.sensor se
WHERE b = m.battery
AND c = m.camera
AND co = m.connectivity
AND h = m.hardware
AND s = m.screen
AND se = m.sensor"
);
Solution 2:
$query = $entityManager->createQuery("
SELECT m FROM Model m
JOIN m.battery b
WITH m.battery = b
JOIN m.camera c
WITH m.camera = c
JOIN m.connectivity co
WITH m.connectivity = co
JOIN m.hardware h
WITH m.hardware = h
JOIN m.screen s
WITH m.screen = s
JOIN m.sensor se
WITH m.sensor = se
");
You are writing DQL and you should not confuse it with SQL. In a DQL query join you don't need to add id fields explicitly to the join Doctrine takes care of it for you.
"SELECT m FROM Model m JOIN m.battery b" is sufficient.
to fetch-join add b to your select clause:
"SELECT m, b FROM Model m JOIN m.battery b"
If you want to join only batteries with a certain property, that is when you need to use the WITH clause in your DQL. For example join only batteries who's battery status is empty:
"SELECT m, b FROM Model m JOIN m.battery b WITH b.status = 'empty'"
Check the documentation on DQL joins for more information.
From what i have read i can say:
When you use Where instead of On in join, there is one difference, On will apply before selection and Where will apply after the selection process by join. WITH is similar to On clause which lets you specify another condition in addition to On.
So I think when you use With, It will work like ON but Where will eliminate the result after selection by Join.
It may helps: Stack Overflow:
Selected Answer of this question also used two of them together with explanation.

oracle outer join query

I have 3 oracle tables. A joins to B and B joins to C. I want all records from A irerspective of whether a corresponding record exists in B or C. I wrote a query like this:
select a.name from a,b,c where a.a_id = b.b_id(+) and b.b_id = c.c_id(+)
This query does not seem right to me, particularly with the second join. What will exactly happen if there is a record in A but correspondingly nothing in B and C? Will it still fetch the record?
For some reason the above query returns same count of records as select a.name from a
So I am guessing that the query is right? Also is there a better way to rewrite the query?
I presume the better query can be
Select a.name from A a left join B b on a.a_id=b.b_id inner join C c on b.b_id=c.c_id
This should give the result as you have expected
http://rajanmaharjan.com.np

successive Joins in Rhino.ETL

I have 3 tables i want to join in 1 table.
How can i do that with Rhino.ETL ?
I know how to join 2 tables, but not 3...
Thanks
John
Use a nested join.
Let's say your tables are A, B and C.
First join A and B to get AB.
Then join AB to C.

Resources