NHibernate: getting object model with HQL using a join on table - join

I am having difficult time trying to figure out how to get a mapped object using a join on a table (not mapped table).
Tables:
DataForm: (Id, AliasName (FK), ...)
Customer: (Id, ...)
CustomerAliases: (PK AliasName, CustomerId (FK))
So aliases are unique for every Customer and Customer may have many aliases.
Code:
DataForm.Customer - this is what I need
So how do I write the HQL in the getter?
Since CustomerAliases table is just a list of aliases I don't have it mapped to a class but is accessed through on the Customer. How can I get Customer using AliasName?
If it was normal SQL I would just do:
SELECT * From Customer c
INNER JOIN CustomerAliases ca ON ca.AliasName = 'AliasNameProvided'
WHERE ca.CustomerId = c.Id

HQL can only be use on mapped classes. When you leave the mapped area, you need to use SQL which looks exactly like the SQL in your question.
I would map it:
class Customer
{
IList<string> AliasNames { get; private set; }
}
mapping:
<class name="Customer">
<!-- ..... -->
<bag name="AliaseNames" table="CustomerAliases">
<key column="CustomerId"/>
<element column="AliasName"/>
</bag>
</class>
query:
from Customer c join c.AliasNames n
where n = :alias

Related

F# query expression: How to do a left join and return on those records where the joined table is null?

I am using SQLProvider in a project and I would like to run a query with a left join and return all records that are missing from the joined table.
I suspect the answer to this question will involve one or both of the packages FSharp.Data.TypeProviders and FSharpComposableQuery, although, to be honest, I can't tell where one ends and the other begins.
The common example of a left join in the above links is given as
query {
for student in db.Student do
leftOuterJoin selection in db.CourseSelection
on (student.StudentID = selection.StudentID) into result
for selection in result.DefaultIfEmpty() do
select (student, selection)
}
And from what I can tell, this is equivalent to the sql:
select *
from Student s
left outer join CourseSelection cs on s.StudentID = cs.StudentID
But what I am looking for is the F# equivalent of the sql:
select *
from Student s
left outer join CourseSelection cs on s.StudentID = cs.StudentID
where s.StudentID is null
I realize that I can just return all records and then filter them in F#, but I want the filtering to happen on the database side where things are indexed and because, in my case especially, the number of non null records is huge, and I am only interested in the null ones.
I think this should do the trick:
query {
for student in db.Student do
leftOuterJoin selection in db.CourseSelection
on (student.StudentID = selection.StudentID) into result
where (not (result.Any()))
select student
}
or a nested query:
query {
for student in db.Student do
where (query {
for selection in db.CourseSelection do
all (student.StudentID <> selection.StudentID)
})
select student
}
Edit: since you're using FSharp.Data.TypeProviders, if you have a foreign key between these two tables then you should also have a property that gives the associated CourseSelections, something like this:
query {
for student in db.Student do
where (not (student.CourseSelections.Any()))
select student
}

Join multiple FK values in one table

I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.

Grails left outer join query HQL or criteria challenge

I am a newbie to hql and tried out several combinations that i could find but i seem unable to construct the correct HQL query for a left join.
I have the following domain model:
class Company {
static hasMany = [employees: Employee]
}
class Employee {
static belongsTo = [
Company
]
}
So a employee does not know anything about the companies. Now i would like to create a hql query that gives met the employees without a company. In sql i have successfully created the query using a left join but i seem not to be able to create a criteria or hql query that gives me a correct result.
Any a clue or some tips on how i could achieve the result?
Here you go, this works:
Employee.executeQuery("""
Select e
from Employee e
where e not in (Select ce from Company c left join c.employees ce)
""")

Entity Framework linq to SQL relationship

table person
personid
personname
table customer
customerid
personid
orderinfo
I am entity framework and want to select few columns but on join I am unable to join customer and person table based on personid. all foreign key and primary keys are in place
var dealercontacts = from contact in database.person join dealer in database.customer on contact.personid equals dealer.personid
select personname, orderinfo;
I am receiving error that customer does not contain personid
If all of your foreign keys are setup correctly, you should be able to call it as such:
var dealercontacts = from contact in database.person
select contact.personname, contact.customer.orderinfo
Does this work to solve your problem?
EDIT: You may have to switch the query around and write it as such:
var dealercontacts = from customer in database.customers
select customer.person.personname, customer.orderinfo

How to access relationship table when doing executeQuery in Grails?

Is it possible to access the relationship table when doing HQL statement?
As an example, I have 3 tables: account, commitment, account_commitment. It was generated using these domains:
class Account {
static hasMany = [ commits : Commitment ]
String name
}
class Commitment {
static hasMany = [ actors : Account ]
String description
}
My final and actual SQL query is something like this:
SELECT
b.id,
account_name,
d.nid,
d.title
FROM
account_commitment a, // is this available in HQL?
account b,
commitment c,
content_type_act d
where
d.nid = 3332
and a.account_id = b.id
and a.act_id = c.id
and c.act_id = d.nid
I believe HQL only allows valid class domains. Since the relationship table is automatically generated, is this possible in HQL?
Thanks.
No, HQL only works with mapped classes. If you want to run SQL queries just use groovy.sql.Sql. But if you only want to access the intermediate table to join the other two, that's unnecessary since HQL knows about the relationships between tables already.

Resources