three level hibernate query join one to many relationship - hql

I have 3 tables
table 1
country
countryid countryname
this table has a one to many join to table 2 that is state
table 2
state
stateid statename countryid
table 2 has a one to many join to city table
table 3
city
cityid cityname stateid
i tried to read country using query
session.createQuery("from Country c where c.countryName=:countryname order by c.countryName");
it gives me country object but the list of states are empty??
what am i doing wrong???

ok i fixed it.
by using cascade.all in #oneToMany annotation
and changed the hql to criteria query.
Criteria criteria = session.createCriteria(Country.class, "country");
criteria.add(Restrictions.eq("country.countryName", countryname));

Related

Display all the fields associated with the record using Impala

Suppose, I have a student table with some fields in impala. Imagine there is a field called total_mark and I should find the student details with maximum mark from each branch.
My table is like this :-
In this table I have to get the details of student with maximum marks from each department.
My query will be like this :-
select id,max(total_marks) from student_details group by department;
But using this query I can get only the id and total_marks. Provided there can be students with same name,age I can't group with fields like age,name .
So how should I query the table to get all the details of top student from each department ??
Thanks in advance.
You can make use of the JOIN concept
select stu.*
from student_details stu
join
( select department,max(total_marks) as max
from student_details
group by department
) rank
on stu.department=rank.department and stu.total_marks=rank.max;

Create a calculated field from q query to another table ms-access 2013

I have table with and ProjectId and more data for instance Name and Quantity.
I have another table with ProjectId, ProjectType and NumberOfthings
How can I created a calculated column that has the summatory of the NumberOfThings values by Project Type and ProjectId ?
For instance getting data by projecttype F in this example I should get in the calculated columns the follow values.
You can use a query to summarize the number of things by ProjectID and ProjectType
SELECT Table1.ProjectID AS ID, Table2.ProjectType AS Type, Sum(Table2.NumberOfThings) AS Things
FROM Table1 INNER JOIN Table2 ON Table1.ProjectID = Table2.ProjectID
GROUP BY Table1.ProjectID, Table2.ProjectType;
Results:
ID Type Things
1 F 3
2 G 34
3 F 100

LINQ Join and performing aggregate functions

I am facing issue in writing LINQ query to perform join on three tables and then performing aggregate functions on the rows. Kindly do provide some help.
I have three tables
Table 1: Students (Id, Name)
Table 2: Subject (SubID, Title, Id)
Table 3: Grade (Id, SubID, marks)
I have to write LINQ query to get the results as following
Count of Students table rows
Count of Grade table rows
Sum of
marks of all rows in Grade table
I am writing query as following but it is not up to the mark as i feel it is not correct.
var _Count = from student in _context.Students
join subject in _context.Subject on student.Id equals subject.Id
join grade in _context.Grade on subject.SubID equals grade.SubID
// How to group them?
select new { //How to take and return the counts?};

LINQ TO Entitites : Combining Two Non Related Tables in Query

I have two tables:
Relationships (ID, UserID, Type, Contact ID)
Contacts (ID, Name, Address)
When the type is 5 in Relationship table, Contact ID is the ID in the Contacts table.
I want to get all the contacts information for a particular user
Here is what I have :
IEnumerable<Relationship> rels = user.Relationships.Where(r => r.Type==5)
foreach (Relationship r in rels)
{
contact = contactRepository.Find(r.ContactID); // Returns Contact Object
Relation relation = new Relation(r, contact);
RelationList.Add(relation);
}
Is this correct way to do this?
I have seen other post mentioning TPC. However, I did not quite understand all that and it seemed TPC only works for code first process.
You can user followng linq statement to get the contacts of a given userID (let's say UserID=15) by using relatonships and contacts tables:
var contacts=from r in Relationships
join c in Contacts on r.ContactID equals c.ID
where r.Type=5 and r.UserID=15
select c;

LINQ Query for fetching data from Multiple Tables

I am working in Asp.Net 4.0 C#-- MVC-3. I have one problem and don't know how to solve. I have 4 tables and want to fetch data from that tables with LINQ.
TABLES
1) Project_Master
Field Names :
project_id (pk)
project_name
company_id (FK with company_master)
company_category_id (FK with Company_Category_master)
project_status_id (FK with Project_Status_Master)
2)Company_Master
Field Names :
company_id
company_name
company_category_id (FK with Company_Category_Master)
3) Company_Category_Master
Field Names :
Company_Category_Id
Company_Category_Name
4) Project_Status_Master
Field Name :
Project_Status_Id
Project_Status_Name
Below are the fields I need to fetch..(using LINQ Query)
Company_Name
Total completed project using status id(1)=complete (where staus 1 means completed)
Total Project
Company_category_name
So, how can I fetch data with linq query??
Thanks in advance...
Try the below example:
(From lse In Me.Leases, nty In Me.Entities, psg In Me.ProductionStages, lsg In LeaseStages _
Where lse.LeaseName = leaseName _
Select lse, lsg, nty, psg).Single
or you can use below example too:
var employeesQuery = from populationTable in db.Populations
join personTable in db.Persons on populationTable.Guid equals personTable.PopulationGuid
join employeeTable in db.Employees on personTable.Guid equals employeeTable.PersonGuid
select new { populationTable, personTable, employeeTable};

Resources