I am using Criteria and I want to explicitly tell hibernate to join on specific columns rather than the primary and foreign key columns. My code is
CreateCriteria<Application>("Application")
.CreateCriteria("Application.Address", "Address", JoinType.FullJoin)
The query that runs for this is
Select * from Application app Full Join Address add ON app.AdressId = add.Id
I want the query to be as
Select * from Application app Full Join Address add ON app.PersonId = add.PersonId
I changed my criteria to
CreateCriteria<Application>("Application")
.CreateCriteria("Application.Address", "Address", JoinType.FullJoin, Restrictions.eqProperty("Application.Person.Id", "Address.Person.Id"))
The sql query that runs is
Select * from Application app Full Join Address add ON app.PersonId = add.PersonId and app.AdressId = add.Id
Is there a way I can explicitly mention in the criteria on the joins I wish to have ?
NHibernate always does joins basing on foreign keys.
I guess that you have configured relationship between Application and Address entities via Application.AddressId foreign key.
That's why you have app.AdressId = add.Id in join.
If I'm right you cannot do the join on some other property.
You can find a slower workaround with subqueries or write a sql query manually.
See other answers:
https://stackoverflow.com/a/28689750/5126411
https://stackoverflow.com/a/44924773/5126411
Related
I'm using Sequelize and Node.js, and I need to join 2 tables on 2 foreign keys,
tableA.hasOne(tableB, { foreignKey: 'fk_tableB' });
tableB.belongsTo(tableA, {foreignKey: 'fk_tableB' });
(by the way, I don't understand the functionality of "targetKey")
but I can only obtain a join on tableA.primaryKey = tableB.fk_tableB.
How can I replace the tableA.primaryKey by tableA.fk_tableA?
I also tried to define twice tableA : in 2 different structures (one with the real primary key and the other with fk_tableA as primary key), but it's also not working (because I need the real tableA mode in another place).
Has someone an idea? Is it a bug from Sequelize?
How can I replace the tableA.primaryKey by tableA.fk_tableA?
There is no tableA.fk_tableA. But if there were, we would expect you to have named it that because column tableA.fk_tableA is a FK to a key column in tableA. Because that's the convention for naming a column fk_tableA. Similarly we would expect a belongTo like yours that adds a column that is a FK to the tableA PK to call it fk_tableA, not fk_tableB. Just like your hasOne gives tableA a column fk_tableB to the tableB PK. (If you want a FK to be to some other column than the PK then you say so via targetKey.)
If you so named FKs after their target table, you seem to want tableA.fk_tableB = tableB.fk_tableA. The way you have named them now, you seem to want tableA.fk_tableB = tableB.fk_tableB.
I need to join 2 tables on 2 foreign keys
It is extremely unlikely that you need the join above. Declaring a column to be a FK says that a value of the source/referencing column is always a value of the target/referenced column. Here targets are PKs. Such a join on a FK to one table and a FK to another table will only return rows that share the same PK value, even though the PKs are from different tables.
I am having to convert code written by a former employee to work in a new database. In doing so I came across some joins I have never seen and do not fully understand how they work or if there is a need for them to be done in this fashion.
The joins look like this:
From Table A
Join(Table B
Join Table C
on B.Field1 = C.Field1)
On A.Field1 = B.Field1
Does this code function differently from something like this:
From Table A
Join Table B
On A.Field1 = B.Field1
Join Table C
On B.Field1 = C.Field1
If there is a difference please explain the purpose of the first set of code.
All of this is done in SQL Server 2012. Thanks in advance for any help you can provide.
I could create a temp table and then join that. But why use up the cycles\RAM on additional storage and indexes if I can just do it on the fly?
I ran across this scenario today in SSRS - a user wanted to see all the Individuals granted access through an AD group. The user was using a cursor and some temp tables to get the users out of AD and then joining the user to each SSRS object (Folders, reports, linked reports) associated with the AD group. I simplified the whole thing with Cross Apply and a sub query.
GroupMembers table
GroupName
UserID
UserName
AccountType
AccountTypeDesc
SSRSOjbects_Permissions table
Path
PathType
RoleName
RoleDesc
Name (AD group name)
The query needs to return each individual in an AD group associated with each report. Basically a Cartesian product of users to reports within a subset of data. The easiest way to do this looks like this:
select
G.GroupName, G.UserID, G.Name, G.AccountType, G.AccountTypeDesc,
[Path], PathType, RoleName, RoleDesc
from
GroupMembers G
cross apply
(select
[Path], PathType, RoleName, RoleDesc
from
SSRSOjbects_Permissions
where
Name = G.GroupName) S;
You could achieve this with a temp table and some outer joins, but why waste system resources?
I saw this kind of joins - it's MS Access style for handling multi-table joins. In MS Access you need to nest each subsequent join statement into its level brackets. So, for example this T-SQL join:
SELECT a.columna, b.columnb, c.columnc
FROM tablea AS a
LEFT JOIN tableb AS b ON a.id = b.id
LEFT JOIN tablec AS c ON a.id = c.id
you should convert to this:
SELECT a.columna, b.columnb, c.columnc
FROM ((tablea AS a) LEFT JOIN tableb AS b ON a.id = b.id) LEFT JOIN tablec AS c ON a.id = c.id
So, yes, I believe you are right in your assumption
I have a sqlite database that I'm trying to build a query. The table column I need to retrieve is iEDLID from the table below :
Right now all I have to go on is a known iEventID from the table below :
And the the nClientLocationID from the table below.
So the requirements are I need to get current iEDLID to write, lookup from tblEventDateLocations for dEventDate and the tblLocation.nClientLocationID based on the tblLocations.iLocationID I already have and event selected on this screen.
So I would need a query that does a "SELECT DISTINCT table EventDateLocations.iEDLID FROM tblEventDateLocations ...."
So basically from another query I have the iEventID I need, and I have the event ID i need but where the dEventDate=(select date('now')) I need to retrieve the iEventDateID from table EventDates.iEventDateID to use on the table EventDateLocations
this is the point where I'm trying to wrap my head around the joins for this query and the syntax...
It seems like you want this:
select distinct edl.iEDLDID
from
tblEventDateLocations edl
join tblEventDates ed on edl.EventDateId = ed.EventDateId
where
ed.EventId = ?
and ed.dEventDate = date('now')
and edl.nClientLocationID = ?
where the ? of course represent the known event ID and location ID parameters.
Since nClientLocationId appears on table tblEventDateLocations you do not need to join table tblLocations unless you want to filter out results whose location ID does not appear in that table.
I have an issue: When I am trying to join two tables which do not have a foreign key or a direct entity relation through my java code within themselves. I am using the below JPQL query: -
SELECT p FROM P p, OM orgm WHERE p.o.id = orgm.o.id and p.u.id = orgm.u.id and orgm.ma = true and p.u.id = ? AND p.o.id IN (:oId);
But this turns to a MySQL query which has a "cross join" which obviously is expensive.
What I need is to make sure that a similar query gives me an inner join MySQL query between the two tables.
I am trying to make usage of the "WITH" clause but seems that it doesn't work with inner join.
Please revert what can be done in this scenario.
Thanks in advance.
Here is the query (MS SQL):
use DB_432799_satv
select [DB_432799_satv].[dbo].ac_Orders.OrderNumber, OrderDate, PaymentDate,
CompletedDate, ShipDate,DateTimeFinishedPicking,DateTimeShipped,
ShipMethodName, PaymentMethodName
from [DB_432799_satv].[dbo].ac_Orders
Inner join [DB_432799_satv].[dbo].ac_Payments on
[DB_432799_satv].[dbo].ac_Orders.OrderId =
[DB_432799_satv].[dbo].ac_Payments.OrderId
Inner join [DB_432799_satv].[dbo].ac_OrderShipments on
[DB_432799_satv].[dbo].ac_Orders.OrderId =
[DB_432799_satv].[dbo].ac_OrderShipments.OrderId
Inner join [DB_432799_satv].[dbo].[ac_Transactions] on
[DB_432799_satv].[dbo].ac_Payments.paymentid =
[DB_432799_satv].[dbo].ac_Transactions.paymentid
Inner join [SuperATV].[dbo].[tblPartsBoxHeader] on
[DB_432799_satv].[dbo].[ac_transactions].[ProviderTransactionId] =
[SuperATV].[dbo].[tblPartsBoxHeader].[ordernumber]
How would I change this into a stored procedure?
I suspect what you want is what a "view" will give you. Specifically, to store that query in the database and select from it like:
SELECT * FOM myLongQueryView WHERE OrderNumber = 1234;
I don't develop MSSQL, but a quick search turned up this doc page on msdn. I suppose it would go something like this:
CREATE VIEW [DB_432799_satv].myLongQueryView
AS
select [DB_432799_satv].[dbo].ac_Orders.OrderNumber, OrderDate, PaymentDate,
CompletedDate, ShipDate,DateTimeFinishedPicking,DateTimeShipped,
ShipMethodName, PaymentMethodName
from [DB_432799_satv].[dbo].ac_Orders
...
Simply prepend your query with CREATE VIEW [DB_432799_satv].myLongQueryView AS, and use the docs page to decide if any options are desired.
It's important to note that query is stored, not it's current results. So it stays up to date with your data. It is joinable, aggregatable, etc...