I have a following working SQL query:
SELECT count(*)
FROM A a
LEFT OUTER JOIN B b
ON a.id = b.aid
AND a.x = 700 AND b.y = 800
WHERE a.type = 1 AND b.type = 2;
I want to use an HQL equivalent.
I've tried the following but it doesn't work (compilation):
SELECT count(*)
FROM A AS a
LEFT JOIN B AS b
AND a.x = 700 AND b.y = 800
WHERE a.type = 1 AND b.type = 2;
W/o the "and a.x = 700 and b.y = 800" part it works fine.
Can you please advise?
p.s.
I need the "and a.x = 700 and b.y = 800" part before the "where" cause it's indexed and partitiouned, and I need the indexes and partitions to be used during join.
thanx,
Pavel.
Hi,
It seems that my problem is more complex than the usual case, so I"ll just post the exact code:
#Entity
#AttributeOverride(name = "id", column = #Column(name = HFrEvent.COL_EVENT_ID))
#Table(name = HFrEvent.TBL_HFREVENT)
public class HFrEvent extends HOrgEntity {
..
#OneToMany(fetch = FetchType.EAGER)
#Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
#JoinColumn(name = COL_EVENT_ID)
public List<HFrEventKeys> getEventKeys() {
return eventKeys;
}
..
}
#Entity
#Table(name = "HFREVENTKEYS")
public class HFrEventKeys extends HOrgEntity {
..
}
The following query works, although there is no explicit "on" statement:
select count(event) from HFrEvent as event left join event.eventKeys as key where event.orgFields.customerId = 110 AND event.orgFields.institutionId = 121 and key.orgFields.customerId = 110 AND key.orgFields.institutionId = 121 and key.name=:name and key.value=:value and event.eventType = 1113;
I want to move some the customerid and institutionid into the join part.
the following not compiles:
SELECT count(event)
FROM HFrEvent AS event
LEFT JOIN event.eventKeys AS key
ON event.orgFields.customerId = 110
AND event.orgFields.institutionId = 121
AND key.orgFields.customerId = 110
AND key.orgFields.institutionId = 121
WHERE key.name=:name
AND key.value=:value
AND event.eventType = 1113;
thank you,
Pavel.
Related
I need to join 3 tables where A.id == B.id and B.id == C.id using JPA #SecondaryTables where I need to map these tables to a single entity. what is the way I should I should try?
Since A.ID = B.ID = C.ID, you can just have 2 secondary tables, with the relationship A.ID = B.ID, and A.ID = C.ID. Your "main" table will be A, and B and C are your secondary tables. You can reference the table as follows in your column declaration. (many other parameters in the annotations left out for brevity)
#Entity
#Table(name = "A")
#SecondaryTables({
#SecondaryTable(name="B", #PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")),
#SecondaryTable(name="C", #PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID"))
})
public Claass Blah {
#ID
private int id;
#Column(table = "B")
private String someColumn;
#Column(table = "C")
private String someOtherColumn;
etc...
}
How do i change the training events into a left outer join in training events im very basic at linq so excuse my ignorance its not retrieve records that don't have any trainnevent reference attached to it
var q = from need in pamsEntities.EmployeeLearningNeeds
join Employee e in pamsEntities.Employees on need.EmployeeId equals e.emp_no
join tevent in pamsEntities.TrainingEvents on need.TrainingEventId equals tevent.RecordId
where need.EmployeeId == employeeId
where need.TargetDate >= startdate
where need.TargetDate <= enddate
orderby need.TargetDat
It's best to use where in combination with DefaultIfEmpty.
See here: LEFT JOIN in LINQ to entities?
var query2 = (
from users in Repo.T_Benutzer
from mappings in Repo.T_Benutzer_Benutzergruppen.Where(mapping => mapping.BEBG_BE == users.BE_ID).DefaultIfEmpty()
from groups in Repo.T_Benutzergruppen.Where(gruppe => gruppe.ID == mappings.BEBG_BG).DefaultIfEmpty()
//where users.BE_Name.Contains(keyword)
// //|| mappings.BEBG_BE.Equals(666)
//|| mappings.BEBG_BE == 666
//|| groups.Name.Contains(keyword)
select new
{
UserId = users.BE_ID
,UserName = users.BE_User
,UserGroupId = mappings.BEBG_BG
,GroupName = groups.Name
}
);
var xy = (query2).ToList();
Which is equivalent to this select statement:
SELECT
T_Benutzer.BE_User
,T_Benutzer_Benutzergruppen.BEBG_BE
-- etc.
FROM T_Benutzer
LEFT JOIN T_Benutzer_Benutzergruppen
ON T_Benutzer_Benutzergruppen.BEBG_BE = T_Benutzer.BE_ID
LEFT JOIN T_Benutzergruppen
ON T_Benutzergruppen.ID = T_Benutzer_Benutzergruppen.BEBG_BG
Anyone know how can I write the following update code by using the BLToolkit syntax, where I need to join two tables, and update one of them. In SQL Server this is done like this:
update Table1 set
Col1 = T.Col1 - TT.Col2
from
#tempTable as TT
inner join Table1 as T on **T.ColX = TT.ColX and T.ColY = TT.ColY**
This is how I have done the updates so far.
db.SomeTable.Where( x => x.ColName == someColName )
.Update( x => new SomeTable
{
//update columns here
} );
Example from BLToolkit unit tests:
var q =
from c in db.Child
join p in db.Parent on c.ParentID equals p.ParentID
where c.ChildID == id && c.Parent.Value1 == 1
select new { c, p };
q.Update(db.Child, _ => new Child { ChildID = _.c.ChildID + 1, ParentID = _.p.ParentID });
var q = (from Labels in dc.tblArtworkDataLabels select Labels).ToList();
But I need this to do the quivalent of:
SELECT d.ID, d.labelID, d.dataID, d.data, l.templateID
FROM tblArtworkDataLabels AS d INNER JOIN
tblArtworkData AS l ON d.dataID = l.ID
WHERE (l.templateID = 238)
How do I do this in LINQ?
Edit
Sorry! Missed the WHERE clause on original statmenet!
var result = dc.tblArtworkDataLabels
.Join(dc.tblArtworkData, l => l.ID, d => d.dataID, (l, d) => new {l, d})
.Select(o => new {
Id = o.d.ID,
LabelId = o.d.labelID,
DataId = o.d.dataID,
Data = o.d.data,
TemplateId = o.l.templateID,
})
.Where(o => o.l.templateID == 238);
If you have a correct foreign key on tblArtworkData to the primary key on the tblArtworkDataLabels and have imported them correctly into the DBML designer you can have LINQ2SQL implicitly creating the join:
from l in tblArtworkData
where l.templateID = 238
select new {
Id = l.tblArtworkDataLabel.ID,
LabelId = l.tblArtworkDataLabel.labelID,
DataId = l.tblArtworkDataLabel.dataID,
Data = l.tblArtworkDataLabel.data,
TemplateId = l.templateID,
}
See my answer on the question "LINQ to SQL: Multiple joins ON multiple Columns. Is this possible?" for how the implicit join translates to SQL.
Edit:
In the case I misunderstood your relations and you have many tblArtworkDataLabels to one tblArtworkData you have to turn the query the other way around
from d in tblArtworkDataLabels
where d.tblArtworkData.templateID = 238
select new {
Id = d.ID,
LabelId = d.labelID,
DataId = d.dataID,
Data = d.data,
TemplateId = d.tblArtworkData.templateID,
}
try
var q = (from Labels in dc.tblArtworkDataLabels
join data in dc.tblArtworkData on Labels.ID equals data.DataID select Labels).ToList();
[NUnit.Framework.Test]
public void Test2()
{
NHibernate.ISession session = Z.Core.NHibernateCore.NHibernateHelper.GetCurrentSession();
var crit = session.CreateCriteria("_School");
crit.CreateCriteria("_ListStudent", "__ListStudent", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
crit.Add(NHibernate.Criterion.Expression.Eq("__ListStudent.Name", "Abc"));
var list = crit.List();
Console.Write(list.Count);
}
NHibernate:
SELECT * FROM Tst_School this_ left outer join Tst_Student liststud1_ on this_.Guid=liststud1_.Guid WHERE liststud1_.Name = 'Abc'
How to create sql:
SELECT * FROM Tst_School this_ left outer join Tst_Student liststud1_ on this_.Guid=liststud1_.Guid AND liststud1_.Name = 'Abc'
Thanks
You should check WITH clause in HQL. I don't know if it's possible in CriteriaQuery.
http://nhforge.org/blogs/nhibernate/archive/2009/05/17/nhibernate-2-1-0-hql-with-clause.aspx