How to select value only in JoinMap with JPA 2.1 CriteriaBuilder?(Eclipselink) - eclipselink

I have:
private MapJoin<Agent, String, String> idNo1;
private Root<Agent> agent;
agent = query.from(Agent.class);
idNo1 = agent.joinMap("data");
c.multiselect(agent.get("name"), idNo1.value());
RESULT PARSED SQL:
select a.name, d.value, d.key from agent a join agentdata d on a.id=d.agentid.
I wonder why d.key is selected although I only call value() method. How can I get resolve this situation to get value only without key.
Thanks.

Related

Jpa Specification ORA-01791: not a SELECTed expression

I have a problem with Jpa Specification.
My specification looks like this:
public static Specification<PersonView> getByFilter(PersonViewFilter filter) {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
SetJoin<PersonView, PersonDetailsView> personDetailsJoin =
root.join(PersonView_.personDetails);
Path<String> namePath = personDetailsJoin.get(PersonDetailsView_.name);
Path<String> surnamePath = personDetailsJoin.get(PersonDetailsView_.surname);
predicates.add(namePredicate(personDetailsJoin, criteriaBuilder, filter.getName()));
predicates.add(surnamePredicate(personDetailsJoin, criteriaBuilder, filter.getSurname()));
predicates.add(peselPredicate(personDetailsJoin, criteriaBuilder, filter.getPesel()));
predicates.removeAll(Collections.singleton(EMPTY_PREDICATE));
query.orderBy(List.of(criteriaBuilder.asc(namePath), criteriaBuilder.asc(surnamePath))).distinct(true);
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
When I try search results I can see an error:
ORA-01791: not a SELECTed expression
The problem is related to generated sql by JpaSpecificationExecutor.
It looks like this:
select
*
from
( select distinct pv.person_id
from
personview pv
inner join
persondetailsview pdv on pv.person_id=pdv.person_id
where
1=1
order by
pdv.name asc,
pdv.surname asc )
where
rownum <= ?
In Select clausule should be added name and surname then it would work, but I don't know how to do this in Specification. Without distinct query works, but i don't have duplicates.
Please for help.
From the error ORA-01791: not a SELECTed expression, you want to add the field to the select or remove the distinct with this :
query.distinct(false);
If you want to keep the distinct, have a look there :
https://stackoverflow.com/a/53549880/2641426

Entity framework join with a subquery via linq syntax

I'm trying to translate a sql query in linq sintax, but I'm having big trouble
This is my query in SQL
select * FROM dbo.ITEM item inner join
(
select SUM([QTA_PRIMARY]) QtaTotale,
TRADE_NUM,
ORDER_NUM,
ITEM_NUM
from [dbo].[LOTTI]
where FLAG_ATTIVO=1
group by [TRADE_NUM],[ORDER_NUM],[ITEM_NUM]
)
TotQtaLottiGroupByToi
on item.TRADE_NUM = TotQtaLottiGroupByToi.TRADE_NUM
and item.ORDER_NUM = TotQtaLottiGroupByToi.ORDER_NUM
and item.ITEM_NUM = TotQtaLottiGroupByToi.ITEM_NUM
where item.PRIMARY_QTA > TotQtaLottiGroupByToi.QtaTotale
and item.FLAG_ATTIVO=1
How can I translate into linq sintax?
This approach doesn't work
var res= from i in context.ITEM
join d in
(
from l in context.LOTTI
group l by new { l.TRADE_NUM, l.ORDER_NUM, l.ITEM_NUM } into g
select new TotQtaByTOI()
{
TradeNum = g.Key.TRADE_NUM,
OrderNum = g.Key.ORDER_NUM,
ItemNum = g.Key.ITEM_NUM,
QtaTotale = g.Sum(oi => oi.QTA_PRIMARY)
}
)
on new { i.TRADE_NUM, i.ORDER_NUM, i.ITEM_NUM} equals new { d.TradeNum, d.OrderNum, d.ItemNum }
I get this error
The type of one of the expressions in the join cluase is incorrect. Type inference failed in the call to 'Join'
Can you help me with this query?
Thank you!
The problem is Anonymous Type comparison. You need to specify matching property names for your two anonymous type's properties (e.g. first, second, third)
I tried it out, here's an example: http://pastebin.com/hRj0CMzs

Same property ie used twice in 'In condition' generate wrong SQL

from CardAssignInfo a left join a.card.detailSet d
where d.category.system.systemId = :systemId and a.personId is not null
and (a.state.id in (:destroyStateIds) or (a.state.id in (:returnStateIds) and d.category.reuseable = true))
this HQL will generate SQL somewhat like:
........... something.stateId in () or (something.stateId in (:destroyStateIds, :returnStateIds) and ......)
The parameter are injected into the same place of SQL in condition, left another place with null condition.
I wonder is it hibernate's specification to have this behavior?

How write a MsSQl query for this scenerio

I am new to MS SQL.
I had a problem in writing a query for this particular scenario So can any one help me
I have table as
S.no Sender Recevier Function
1 s1 R1 Read
2 R1 S1 Read
3 S1 P1 Write
4 R1 w2 Read
I would Update the values based on following criteria
that is Select the Function Read Where (Sender = S1) OR (Receiver = S1)
That I need Update the values By Selecting a Function And User may be in Sender coloumn or receiver So How can I do it.
I trited like
Update Table1 Set Sender = Null, Receiver=Null Where Function = 'Read' And Sender = 's1'OR Receiver = 's1'
But I am Unsucessful So please any one help me how solve this.
I'm guessing you get an error like Incorrect syntax near the keyword 'Function'.: when you run this because the word function is a reserved keyword in T-SQL.
Try changing it to:
Update Table1 Set Sender = Null, Receiver = Null
Where [Function] = 'Read' And (Sender = 's1' OR Receiver = 's1')
You might want to change the name of the column if possible as using reserved word is never a good idea.
Sample SQL Fiddle demo.

HQL / JPQL - Nested select on FROM

I try to convert my SQL query into HQL or JPQL (I want to benefit of the object mapping).
My SQL Request is :
SELECT *
FROM (SELECT bde, MAX(creation_date)
FROM push_campaign GROUP BY bde) temp,
push_campaign pc where pc.bde = temp.bde and pc.creation_date = temp.creation_date;
I try (unsuccessfully) to convert it in JPQL with :
select pc
from (select bde, max(creationDate)
from PushCampaign group by bde) temp,
PushCampaign pc
where pc.bde = temp.bde and pc.creationDate = temp.creationDate
But I got raised :
IllegalArgumentException occured :
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near
line 1, column 16 [select pc from (select id, max(creationDate) from
models.PushCampaign group by bde) temp, models.PushCampaign pc where
pc.id = temp.id]
I read the nested select can only be in select or where clause.
Do you have workarounds to keep the request and benefit of object-mapping ?
Not possible with JPQL or HQL in a single request.
To do this in a single request I propose this :
String campaignToLaunch = "select pc.* from PushCampaign pc ..."
//SQL request which return a resultset compatible with the models.PushCampaign class
Class className = Class.forName("models.PushCampaign");
List<PushCampaign> result = JPA.em()
.createNativeQuery(campaignToLaunch,className)
.getResultList();
this should achive similar results
select pc
from PushCampaign pc
where pc.creationDate =
(select max(creationDate) from PushCampaign inner where inner.bde = pc.bde)
A simple solution could be:
servlet
{
Query q = entityManager.createNativeQuery("SQL");
List<> li = q.getResultList();
#PersistenceContext(unitName="UNIT")
private EntityManager entityManager;
}