Right join JPA query fails to get exact ouput - java

I have a problem with this query in JPA
Employee table
id,....,location_id (id 1,2,3,4 assigned to location id 1)
EmployeeMaster table
id,.....,date, employee_id(employee_id 1 and 2 having records in this table)
JPA query
select me,ms from EmployeeMaster ms right join ms.employee me where ms.date between ?2 and ?3 and me.location.id = ?4
Output
employeeMaster,employee1
employeeMaster,employee2
Because only two employees having records in EmployeeMaster table between dates
I want the output be like
employeeMaster,employee1
employeeMaster,employee2
null,employee3
null,employee4
Please help me to solve this
Thanks.

instead of retrieving employeeMaster retrieve employee and you will get your result.

Related

Convert sql query to JPA. Join 3 tables

I have a database that currently got 3 tables that i want to join together.
products
product_category
category
I have an sql join that does what i need it to do, which is taking category_id and returning all products in that category.
SELECT products.name FROM products
JOIN product_category ON products.id = product_category.product_id
JOIN categories ON categories.id = product_category.category_id
WHERE categories.id = ?
I have tried to google for a while now without success.
How can i make this happen?

Element collection returning odd query

I have an many to many table which I am querying and want to return one column from it
Consider
Table 1
M to M (consisting of TABLE1_ID and TABLE2_ID)
Table2
I am in Table 2 entity and I want to return all the table 2 ids in the M to M table when I give it a table 1 id.
In my entity I have
#ElementCollection
#CollectionTable(name="M_TO_M_TABLE", joinColumns=#JoinColumn(name="TABLE1_ID"))
#Column(name="TABLE_2_ID")
private Set<Integer> tableTwoIds;
When I try and query this the JPA query that is produced is weird!
My query is
SELECT tab2 from Table2 tab2 where tab2.tableOneIds in (:idsPassedIn)
The error I get makes sense from the query that is generated. The error is
org.hibernate.exception.GenericJDBCException: Missing IN or OUT parameter at index:: 1
and the query is
select tab2.ID, tab2.NOTES
from TABLE_2 tab2, M_TO_M mToM
where tab1.ID=mToM.TAB_1_ID and (. in (? , ?))
and we have a . after the and rather than mToM.TAB_2_ID
Has anyone any ideas?
Thanks

Convert SQL to JPA 2 Criteria queries

I've successfully created some JPA 2 criteria queries, but im now converting a query that is too complex for me to be able to convert it. Anyone up for a challenge? I will be eternally thankful for any response! :)
This is the query in plain SQL (mysql):
SELECT o.orderId, oi.insertDate
FROM AIDA_ORDER o
LEFT OUTER JOIN
(SELECT orderId, MIN(insertDate) AS insertDate FROM AIDA_ORDER_INSERT
WHERE insertDate > CURDATE() GROUP BY orderId) AS oi
ON oi.orderId = o.orderId
ORDER BY oi.insertDate, o.orderId;
The AIDA_ORDER table corresponds to a OrderBean, and AIDA_ORDER_INSERT to a OrderInsertBean.
A AIDA_ORDER can have multiple AIDA_ORDER_INSERTS.

JPA/Hibernate query construction

I am fetching records from my "record" table. "record" table has many columns tow of which are
client_id, foreign key mapping to client table.
creation_date , date of record creation
I would like to do a query on this table , but I would like to fetch only one record per client(latest creation_date record has preference).
Will following work?
select r.id,r.xx,r.yy
group by(r.client_id),r.creation_date
from record r
order by creation_date desc
I tried above and seems records fetched are not of latest creation dates.
Hope my question is clear
Just keep your query and add a WHERE condition :
SELECT r.id,r.xx,r.yy
GROUP BY(r.client_id)
FROM record r
WHERE r.creation_date = (SELECT MAX(creation_date) FROM record tmp WHERE tmp.client_id = r.client_id )
Take a look at This discussion
This should give you a good starting point in HQL.
from Record as r inner join fetch r.client
where r.creation_date > (
select max(rec.creation_date) from Record rec
where rec.client.client_id = r.client.client_id
)
This of course assumes that your Record has a reference to its parent Client called client.

How to map native query to one model class?

Hi I´m using Eclipselink and I did a native query to select some fields of 2 tables. I mapped my table Logins in a model class. I would not like to map my table "B" because I need only 2 fields of this table on my sql result.. can I map this 2 fields in my Logins table to my sql result ?
My sql is this:
select l.login_id, s.lugarcerto,s.vrum, l.username, l.first_name, l.last_name, l.phone, l.fax_number, l.address, l.zip,
l.address2 as 'birth_date', l.city as 'cpf_cnpj'
from Logins l
join (select se.login_id, lugarcerto = min(case when se.service = 'IM' then '1' end), vrum = min(case when se.service = 'VE' then '1' end)
from (select distinct ad.login_id, substring(ap.Rate_code,(CHARINDEX('-', ap.Rate_code)+1),2) as 'service'
from Ad_Data.dbo.ad ad
join Ad_Data.dbo.ad_pub ap on (ad.ad_id = ap.ad_id)
where ap.ad_type =1) se
group by se.login_id) s on (s.login_id = l.login_id)
I did map Logins table and I want to map s.lugarcerto and s.vrum to my SQL query result.
There´s anyway to just add it to my Logins model ?
Not without having mappings for the attributes you want those values put into, and not without causing problems with them being cached in the entity.
Why not just return the values beside the entity, much like you would with a JPQL query such as: "Select l, subquery1, subquery2 from Logins l" ie:
Query q = em.createNativeQuery(yourQueryString, "resultMappingName");
And in the entity, include the annotation:
#SqlResultSetMapping(name="resultMappingName",
entities={#EntityResult(entityClass=com.acme.Logins.class, )},
columns={#ColumnResult(name="LUGARCERTO"), #ColumnResult(name="VRUM")}
)
Best Regards,
Chris

Categories

Resources