Fetch data from two tables in hibernate - spring

I have to fetch students data, whose account status is true from Student and User tables.as account status has been mentioned in User table and remaining information of the student is in student table,i need students registered under particular college since i have to check college_id in where condition.I have tried many ways to join tables but getting error,please help me to get out of this. below is my code
Query query=sessionFactory.getCurrentSession().createQuery("select stud.student_name, stud.college.college_id, stud.enroll_no, stud.enrollment_year from Student stud,User u where stud.college.college_id=:college_id and u.account_active=:account_active");
query.setParameter("college_id", college_Id);
query.setParameter("account_active", true);
List<Student> list_1 = query.list()

u have to join by using pojo like below
Query query=sessionFactory.getCurrentSession().createQuery("
select stud.student_name, stud.college.college_id, stud.enroll_no, stud.enrollment_year
from User u
join u.Student stud
join stud.college college
where college.college_id=:college_id
and u.account_active=:account_active
");
query.setParameter("college_id", college_Id);
query.setParameter("account_active", true);
List<Student> list_1 = query.list()
if Student relationship is availabe in your User table and all pojo must have object of another table in there pojo

Related

Is it possible to join eloquent relationship query?

I have a users, departments, and positions table.
I want to select one department with all the users in that department with their position name.
Currently, I have this.
$department = DepartmentView::with('users')
->findOrFail($departmentId);
It returns me a department with users, but I want to join the users with positions table so I can also get the position name. (user table only has position id)
Assuming you have your relationships setup properly in your User model and the relationship is called position, it should be like this:
$department = DepartmentView::with('users.position')
->findOrFail($departmentId);
Look at eager loading -> nested eager loading.
You can do
$department = DepartmentView::with('users.position')->findOrFail($departmentId)
position is referred to the relationship set on the User model to get the user position.

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;

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;

System.Linq.Dynamic to select a collection inside of a collection from Entity Framework IQueryable

I have an EF model with the relationship User->Roles. It's a one to many relationship in our database. I would like to select the ID from role's record and the name from the user's records.
What I have come up with works for one-to-one relationships. I'm not sure how to generate the a list inside of dynamic linq. Maybe a SelectMany?
User table: Users
Join Table: User_Role
Role Table: Roles
//does not select inside a collection of USER_ROLES
q= query.Select(" new ( ID, (new (USER_ROLES.ID as ID) as USER)")
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ID as ID) as USER)") something I figure this would give me a list of User_Role's ID
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ROLE.ID as ID) as USER)") something I figure this would give me a list of Role's ID
Update I'm getting closer. I used a selectmany, but the results are duplicated
q = query.SelectMany("USER_ROLES","new (inner as myUSER,outer as myROLE) ").SelectD("new (myROLE.ID as ROLE_ID, new( myROLE.NAME, myUSER.USER.FIRSTNAME,myUSER.USER.ID)as user)")
The results look like this:
Role->User_Role A-> User A
User_Role A-> User B ..notice the repeat of "User_Role A"
User_Role A-> User C
it should be
Role->User Role a -> User A
+ User B
+ User C

How can I use a compound condition in a join in Linq?

Let's say I have a Customer table which has a PrimaryContactId field and a SecondaryContactId field. Both of these are foreign keys that reference the Contact table. For any given customer, either one or two contacts may be stored. In other words, PrimaryContactId can never be NULL, but SecondaryContactId can be NULL.
If I drop my Customer and Contact tables onto the "Linq to SQL Classes" design surface, the class builder will spot the two FK relationships from the Customer table to the Contact table, and so the generated Customer class will have a Contact field and a Contact1 field (which I can rename to PrimaryContact and SecondaryContact to avoid confusion).
Now suppose that I want to get details of all the contacts for a given set of customers.
If there was always exactly one contact then I could write something like:
from customer in customers
join contact in contacts on customer.PrimaryContactId equals contact.id
select ...
...which would be translated into something like:
SELECT ...
FROM Customer
INNER JOIN Contact
ON Customer.FirstSalesPersonId = Contact.id
But, because I want to join on both the contact fields, I want the SQL to look something like:
SELECT ...
FROM Customer
INNER JOIN Contact
ON Customer.FirstSalesPersonId = Contact.id OR Customer.SecondSalesPersonId = Contact.id
How can I write a Linq expression to do that?
It's rarely correct to use join in LINQ to SQL.
Since you want contacts, why not start your selection there? Presuming the association between Customer and Contact is two-way, you should be able to write something like:
IEnumerable<Guid> customerIds = // ...
var q = from contact in Context.Contacts
where customerIds.Contains(contact.Customer.Id)
select contact;
Use anonymous classes. EG
new { A.Foo, B.Bar } equals new { Foo = B.Baz, Bar = C.Ork }

Resources