JPQL - Joins - MultipleTables and empty ResultList - spring

I have a query in JPQL through 5 tables, but if one of the table is NULL, the whole query fails and resultList is empty and it throws the exception. On the other hand if all tables do not contain null, it works as expected.
How to implement it to return all objects where table is not null and objects where is null as null? So I would get - Object1[], null, null, null for example... and not only empty result list...
Thank you very much :)
public List<Object[]> getAdditionalInformation(String ppin) {
Query query = em.createQuery("SELECT p, pl, r, d, do FROM Patient p JOIN p.placements pl JOIN pl.room r"
+ " JOIN r.department d JOIN d.doctors do where p.pin = :ppin");
query.setParameter("ppin", ppin);
return query.getResultList();
}
#Transactional
public AdditionalPD getAdditional(String ppin) {
List<Object[]> list = hr.getAdditionalInformation(ppin);
AdditionalPD adp = new AdditionalPD();
Patient patient = null;
Placement placement = null;
Room room = null;
Department department = null;
Doctor doctor = null;
for(Object[] object : list) {
patient = (Patient) object[0];
placement = (Placement) object[1];
room = (Room) object[2];
department = (Department) object[3];
doctor = (Doctor) object[4];
}
adp.setPatientFirstName(patient.getFirstName());
adp.setPatientLastName(patient.getLastName());
adp.setAge(countAge(ppin));
adp.setFrom(placement.getFrom());
adp.setTo(placement.getTo());
adp.setRoomName(room.getName());
adp.setDepartmentName(department.getName());
adp.setDoctorFirstName(doctor.getFirstName());
adp.setDoctorLastName(doctor.getLastName());
return adp;
}
#GetMapping("/additional/pin/{ppin}")
public String additionalInformation(#PathVariable String ppin, Model model) {
AdditionalPD adp = has.getAdditional(ppin);
model.addAttribute("adp", adp);
return "additional";
}

Use LEFT JOIN instead of JOIN. JPQL joins are INNER JOINs by default.

Related

How to return FirstOrDefaultAsync EF Linq to entity List query

I have this Linq to entity query that returns a list of visitors with joins.
I want a similar query to return a single record without the query being a List collection, but changing it to a basic select with FirstOrDefaultAsync gets the error "A query body must end with a select clause or a group clause"
public async Task<List<VisitorDetail>> GetOneVisitor(int visitorId)
{
var query = await (from v in _context.Visitors
where v.Id == visitorId
join d in _context.Categories on v.VisitCategoryId equals d.Id
join c in _context.Counters on v.AssignedCounter equals c.Host into counterGroup
from cg in counterGroup.DefaultIfEmpty()
select new
{
FirstName = v.FirstName,
LastName = v.LastName,
CounterDescription = cg.Description,
VisitCategory = d.Description
}).ToListAsync();
List<VisitorDetail> visitors = new();
foreach (var p in query)
{
visitors.Add(new VisitorDetail
{
FirstName = p.FirstName,
LastName = p.LastName,
CounterDescription = p.CounterDescription,
CategoryDescription = p.VisitCategory
});
}
return visitors;
}
If you want to keep DRY with LINQ to Entities, return IQueryable for your common queries and invoke materialization only when it is needed.
Your method can be rewritten in the following way:
public IQueryable<VisitorDetail> GetVisitorDetails(int visitorId)
{
var query =
from v in _context.Visitors
where v.Id == visitorId
join d in _context.Categories on v.VisitCategoryId equals d.Id
join c in _context.Counters on v.AssignedCounter equals c.Host into counterGroup
from cg in counterGroup.DefaultIfEmpty()
select new VisitorDetail
{
FirstName = v.FirstName,
LastName = v.LastName,
CounterDescription = cg.Description,
CategoryDescription = d.Description
};
return query;
}
var many = await GetVisitorDetails(visitorId).ToListAsync();
var one = await GetVisitorDetails(visitorId).FirstOrDefaultAsync();

Querydsl - filter on Left join with subquery

I have one of the complex query dynamically generated through Querydsl predicate and JPQL. I am also using the Q classes.
I am able to generate the following query by passing a predicate to the JPA repository.
select company0_.id as id1_18_,
company0_.name as name2_18_
from company company0_
left outer join companyAddress companyadd1_ on company0_.id=companyadd1_.company_id
where company0_.id in
(select companyadd2_.company_id
from companyAddress companyadd2_
where companyadd2_.address_type='1')
order by companyadd1_.addressline1;
but I want the query mentioned below
select company0_.id as id1_18_,
company0_.name as name2_18_
from company company0_
left outer join companyAddress companyadd1_ on company0_.id=companyadd1_.company_id
and companyadd1_.status = 'Active' -- New added(Failed to implement this)
where company0_.id in
(select companyadd2_.company_id
from companyAddress companyadd2_
where companyadd2_.address_type='1'
and and companyadd2_.status = 'Active') -- New Added(I am able to achieve this)
order by companyadd1_.addressline1;
We are using following kind of code, I can not possibly to share exact code due to security concern but you can help me by providing basic structure or code to achieve this.
final JPQLQuery<QCompanyAlias> subQuery = new JPAQuery<>();
BooleanExpression exp = null;
QueryBase<?> q = (QueryBase<?>) subQuery.from(qCompanyAddress);
if (requestMap.containsKey(CompanyQueryConstants.ADDRESS_TYPE)) {
BooleanExpression addrExp = null;
for (String addressType : addressTypes) {
if (addrExp == null) {
addrExp = qCompanyAddress.addressType.addressTypeCode.eq(addressType);
} else {
addrExp = addrExp.or(qCompanyAddress.addressType.addressTypeCode.eq(addressType));
}
}
exp = addrExp;
}
To add join on two conditions use
new JPAQuery(em)
.from(qCompany)
.leftJoin(qCompany, qCompanyAddress.company)
.on(
qCompany.id.eq(qCompanyAddress.company.id)
.and(qCompanyAddress.status.eq(status))
);
For subquery try to use this
final JPQLQuery<QCompanyAlias> subQuery = new JPAQuery<>();
BooleanExpression exp = null;
QueryBase<?> q = (QueryBase<?>) subQuery.from(qCompanyAddress);
if (requestMap.containsKey(CompanyQueryConstants.ADDRESS_TYPE)) {
BooleanExpression addrExp = null;
for (String addressType : addressTypes) {
if (addrExp == null) {
addrExp = qCompanyAddress.addressType.addressTypeCode.eq(addressType);
} else {
addrExp = addrExp.or(qCompanyAddress.addressType.addressTypeCode.eq(addressType));
}
}
exp = addrExp;
}
BooleanExpression statusExp = qCompanyAddress.status.eq(status);
if(exp == null) {
exp = statusExp;
} else {
exp = statusExp.and(exp);
}
But in your case I can't understand the reason for filtering by status twice. Subquery filtering should be enought. I suspect that you can achieve the same result without subquery. It depends on your entities.

EF Core LINQ use scalar function

I use Entity Framework Core 2.1.
I have a scalar function in the database which adds specified number of days.
I created an extension method to execute it:
public static class AdventureWorks2012ContextExt
{
public static DateTime? ExecFn_AddDayPeriod(this AdventureWorks2012Context db, DateTime dateTime, int days, string periodName)
{
var sql = $"set #result = dbo.[fn_AddDayPeriod]('{dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}', {days}, '{periodName}')";
var output = new SqlParameter { ParameterName = #"result", DbType = DbType.DateTime, Size = 16, Direction = ParameterDirection.Output };
var result = db.Database.ExecuteSqlCommand(sql, output);
return output.Value as DateTime?;
}
}
I try to use a scalar function in the query (to simplify things I use AdventureWorks2012) as follows:
var persons =
(from p in db.Person
join pa in db.Address on p.BusinessEntityId equals pa.AddressId
where p.ModifiedDate > db.ExecFn_AddDayPeriod(pa.ModifiedDate, 100, "DayPeriod_day")
select p).ToList();
But get an System.InvalidOperationException: 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.'
How can I achieve this?
UPDATE:
I managed to do it with the help of Ivan's answer:
var persons =
(from p in db.Person
join bea in db.BusinessEntityAddress on p.BusinessEntityId equals bea.BusinessEntityId
join a in db.Address on bea.AddressId equals a.AddressId
where p.ModifiedDate > AdventureWorks2012ContextFunctions.AddDayPeriod(a.ModifiedDate, 100, "DayPeriod_day")
select p).ToList();
But now I need to update ModifiedDate for filtered persons. So I'm doing like this:
var persons =
(from p in db.Person
join bea in db.BusinessEntityAddress on p.BusinessEntityId equals bea.BusinessEntityId
join a in db.Address on bea.AddressId equals a.AddressId
let date = AdventureWorks2012ContextFunctions.AddDayPeriod(a.ModifiedDate, 100, "DayPeriod_day")
where p.ModifiedDate > date
select new { Person = p, NewDate = date }).ToList();
foreach (var p in persons)
p.Person.ModifiedDate = p.NewDate ?? DateTime.Now;
db.SaveChanges();
But got System.NotSupportedException: 'Specified method is not supported.'
How can I use scalar function in select statement?
I tried to split the query by two parts:
var filteredPersons = // ok
(from p in db.Person
join bea in db.BusinessEntityAddress on p.BusinessEntityId equals bea.BusinessEntityId
join a in db.Address on bea.AddressId equals a.AddressId
where p.ModifiedDate > AdventureWorks2012ContextFunctions.AddDayPeriod(a.ModifiedDate, 100, "DayPeriod_day")
select new { Person = p, a.ModifiedDate }).ToList();
var persons = // here an exception occurs
(from p in filteredPersons
select new { Person = p, NewDate = AdventureWorks2012ContextFunctions.AddDayPeriod(p.ModifiedDate, 100, "DayPeriod_day") }).ToList();
Instead of invoking the function client side (which is this particular case happens as part of the client evaluation of the query filter, while the query reading is still in progress), you can use EF Core Database scalar function mapping so it
can be used in LINQ queries and translated to SQL.
One way to do that is to create a public static method in the derived context class and mark it with DbFunction attribute:
public partial class AdventureWorks2012Context
{
[DbFunction("fn_AddDayPeriod")]
public static DateTime? AddDayPeriod(DateTime dateTime, int days, string periodName) => throw new NotSupportedException();
}
and use
where p.ModifiedDate > AdventureWorks2012Context.AddDayPeriod(pa.ModifiedDate, 100, "DayPeriod_day")
Another way is to create a public static method in another class
public static class AdventureWorks2012DbFunctions
{
[DbFunction("fn_AddDayPeriod")]
public static DateTime? AddDayPeriod(DateTime dateTime, int days, string periodName) => throw new NotSupportedException();
}
but then you'll need to register it with fluent API (which happens automatically for methods defined inside the context derived class):
modelBuilder
.HasDbFunction(() => AdventureWorks2012DbFunctions.AddDayPeriod(default(DateTime), default(int), default(string)));
The usage is the same:
where p.ModifiedDate > AdventureWorksDbFunctions.AddDayPeriod(pa.ModifiedDate, 100, "DayPeriod_day")

Compare a linq value (int) to an int List using where clause

I have a linq query which joins a couple of tables and returns the value into an object. The query was working fine, till i added a where clause to in. Aftre the where clause, my query returns null.
Here's the code:
List<Int32> resourceSupervisorIdList = new List<Int32>();
resourceSupervisorIdList.Add(searchCriteriaTimesheet.ResourceId);
foreach (resource res in allSubordinateResources)
{
if (!resourceSupervisorIdList.Contains(res.id_resource))
resourceSupervisorIdList.Add(res.id_resource);
}
using (tapEntities te = new tapEntities())
{
var timesheetAll = (from tsh in te.timesheet_header
join rs in te.resources on tsh.id_resource equals rs.id_resource
join tsd in te.timesheet_detail on tsh.id_timesheet equals tsd.id_timesheet
where (resourceSupervisorIdList.Contains(rs.id_resource_supervisor))
select new TimesheetHeaderDetailsItem()
{
OrganizationId = rs.id_organization,
ProjectId = tsd.id_project,
StartDate = tsh.dte_period_start,
EndDate = tsh.dte_period_end,
ApprovedDate = tsh.dte_approved,
RejectedDate = tsh.dte_rejected,
SubmittedDate = tsh.dte_submitted,
});
if (timesheetAll == null || timesheetAll.Count() == 0)
{
return result;
}
}
Now, after adding the where clause, the code runs into the if condition. There are matching records in the table, but still i'm not able to get any records.
rs.id_resource_supervisor
is of type int in the mysql db.

LINQ anonym object with result to delimited string (LINQ to Entities does not recognize the method 'System.String ToString()' method)

I am trying to get a ; demlimited string of all the relatives that a person has.
Four tables are involved:
USERTAB
PERSON
PERSON_RELATION
RELATION_TAB
Query
from u in USERTAB
select new
{
Person = from p in PERSON where p.USERID == u.USERID
select new
{
PNo = p.NO,
Name = p.NAME
Relatives = (from r in PERSON_RELATION where r.PSEQ == p.PSEQ select new
{
Description = (from rel in RELATION_TYPE where rel.TYPE_SEQ == r.TYPE_SEQ select rel.DESCRIPTION).ToArray() //(or also tried .ToString())
})
}
}
I'd like the Description field to be a ";" delimited list of all the relatives a user (person) has.
Using a ToString on my Relatives object it only fails runtime with. LINQ to Entities does not recognize the method 'System.String ToString()' method
Example: Description = "Father, Brother, Cousin"
You need to be clear about what parts of your query get translated to SQL to run on the server, and what parts run in your local application. The key is to construct a simple query to retrieve all the data, then use .AsEnumerable() to ensure that the remaining transformations don't get translated to SQL, and finally transform the data into a form useful for you. Something like
var query =
from u in USERTAB
select new
{
Person =
from p in PERSON
where p.USERID == u.USERID
select new
{
PNo = p.NO,
Name = p.NAME
Relatives =
from r in PERSON_RELATION
where r.PSEQ == p.PSEQ
select new
{
Description =
from rel in RELATION_TYPE
where rel.TYPE_SEQ == r.TYPE_SEQ
select pos.DESCRIPTION
}
}
}
};
var enumerable =
from u in query.AsEnumerable()
select new
{
Person =
from p in u.Person
select new
{
PNo = p.PNo,
Name = p.Name
Relatives =
string.Join(", ",
from r in p.Relatives
from d in r.Description
select d.Description)
}
}
};
should do the trick.
This should work: write an extension method for strings of array, like so:
public static ToCsv(this string[] strings)
{
return String.Join("," strings);
}
Then just tack .ToCsv() at the end of your .ToArray() call above, and it should do the trick!

Resources