Hibernate SQLQuery neglecting aliasname - java

When i try to execute the following query in mysql , it works perfectly. But when we try it via Hibernate(3.2) ,hibernate is not differentiating m.NAME and o.NAME. It is returning the same result for both as m.NAME.
SELECT m.NAME, m.SCREENNAME, rm.ADDRESS, o.NAME FROM remoteunit rm LEFT JOIN mo m ON rm.MOID = m.ID JOIN overallcustomfields ocf ON m.ID = ocf.MOID LEFT JOIN organization o ON ocf.ORGID = o.ID WHERE m.DOMAINID = 2
Iam using the following code
Transaction transaction = session.beginTransaction();
SQLQuery query = session.createSQLQuery(queryString);
query.setLong("customId", customId);
remoteUnitList = (ArrayList<Object[]>)query.list();
transaction.commit();
Note: Forced to use SQLQuery in Hibernate since the columns are dynamically populated and more constraints.

Did you try SELECT m.NAME as m_name, o.NAME as o_name.... I faced the same problem and it went of with this change.

Related

Hibernate DetachedCriteria Query with and clause in subselect

How can I solve this query with Hibernate's detached criteria? The hardest part for me is to bring the and u1.abrechnungsDatum is null into the subselect.
I want a query like this:
select *
from
patient as p
where
p.krankenstand = ?
and
? < ( select count(*) from ueberweisung u1 where p.id = u1.patient_id
and u1.abrechnungsDatum is null)
I have tried it with this
DetachedCriteria dc = DetachedCriteria.forClass(Patient.class, "patient");
dc.add(Restrictions.eq("krankenstand", true));
DetachedCriteria nab = dc.createCriteria("ueberweisungs", "nab");
nab.add(Restrictions.isNull("nab.abrechnungsDatum"));
dc.add(Restrictions.sizeGt("ueberweisungs", 0));
which gives me the following SQL-Statement
select
*
from
patient this_
inner join
ueberweisung nab1_
on this_.id=nab1_.patient_id
where
this_.krankenstand=?
and nab1_.abrechnungsDatum is null
and ? < (
select
count(*)
from
ueberweisung
where
this_.id=patient_id
)
As you can see, the and-clause for the field abrechnungsDatum is not inside the subselect. How can I achieve to get this inside?
Try this instead:
DetachedCriteria dc = DetachedCriteria.forClass(Patient.class, "patient");
dc.add(Restrictions.eq("krankenstand", true));
DetachedCriteria subquery = DetachedCriteria.forClass(Ueberweisung.class);
subquery.add(Restrictions.isNull("abrechnungsDatum"));
dc.add(Subqueries.ltAll(0, subquery));

JPQL multiple JOIN query

Can someone help me?
I got that SQL query and need to represent that in JPQL, but i faced trouble with right join:
SELECT alrt.*
FROM
REACTION.ALERT alrt, REACTION.INVESTIGATION inv,
REACTION.CLASSIFICATION_TYPE clst, REACTION.FRAUD_TYPE frt,
REACTION.TRANS trns, REACTION.CARD crd
WHERE
alrt.ISS_INST IN(1201, 1101) AND
alrt.MODULE_TYPE = 0 AND
0 < (SELECT COUNT(*) FROM REACTION.INVESTIGATION WHERE REACTION.INVESTIGATION.ALERT_ID = alrt.ID) AND
inv.CLASSIFICATION_TYPE_ID IS NOT NULL AND
clst.CLASSIFICATION_TYPE = 10 AND
(alrt.REMINDER_USER_LOGIN = 'qwr' OR alrt.REMINDER_USER_LOGIN IS NULL) AND
alrt.ID = inv.ALERT_ID AND
alrt.TRANSACTION_ID = trns.ID(+) AND inv.CLASSIFICATION_TYPE_ID =
clst.ID AND inv.FRAUD_TYPE_ID = frt.ID(+) AND trns.HPAN = crd.HPAN(+);
After read tutorials and docs i create that JPQL query:
SELECT alrt
FROM INVESTIGATION inv
JOIN inv.CLASSIFICATION_TYPE_ID clst
RIGHT JOIN inv.FRAUD_TYPE_ID frt
JOIN inv.alert_id alrt
RIGHT JOIN alrt.transactio_id trns
RIGHT JOIN trns.HPAN crd
WHERE
alrt.ISS_INST IN(1201, 1101) AND
alrt.MODULE_TYPE = 0 AND 0 < (SELECT COUNT(inv1) FROM INVESTIGATION inv1 WHERE inv1.ALERT_ID = alrt.ID) AND
inv.CLASSIFICATION_TYPE_ID IS NOT NULL AND
clst.CLASSIFICATION_TYPE = 2 AND
(alrt.REMINDER_USER_LOGIN = 'qwr' OR alrt.REMINDER_USER_LOGIN IS NULL);
But i got error then try to execute that. Can someone tell what i did wrong pls?
If it make sense i use JPA 1.0 version
Your error is join ID, but not OR-mapping object.
Try this:
JOIN inv.CLASSIFICATIONTYPE(your mapping property name) clst
If u do not have mapping relationship between entities, u'd better to user join table i.o. OR-mapping.
Read more information on JPQL

How to formulate a JOIN in a typed query?

I want to create a typed query.
TypedQuery<PubThread> query = em.createQuery(queryString, PubThread.class);
query.setParameter("threadId", threadId);
List<PubThread> otherPubThreads = query.getResultList();
In the queryString is the following SQL (currently without param and static selection values)
SELECT pt2 FROM pubthread pt2
JOIN pub_pubthread ppt2 ON pt2.id = ppt2.pubThreads_id
JOIN pub p2 ON ppt2.pups_id = p2.id
JOIN pubcategory pc2 ON p2.pubCategoryId = pc2.id
WHERE pt2.id != 1 and EXISTS (
SELECT DISTINCT(pt.id)
FROM pubthread pt
JOIN pub_pubthread ppt ON pt.id = ppt.pubThreads_id
JOIN pub p ON ppt.pups_id = p.id
JOIN pubcategory pc ON p.pubCategoryId = pc.id
WHERE pc2.id = pc.id and pt.id = 1
)
It does work, if I limit the String to a simple select: SELECT Distinct(pt2.id), pt2.name FROM pubthread pt2. As soon I add a JOIN line to it, it will complain. How do you properly query with JOINS in JPA? The error is:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 81 [SELECT pt2 FROM com.brayan.webapp.model.PubThread pt2 JOIN pub_pubthread ppt2 ON pt2.id = ppt2.pubThreads_id ]
Doubtless, a criteria query would be nicer. I accept that as part of the solution space.
When you call createQuery you have to write HQL but not SQL (your queryString is not HQL).
In HQL you have to join objects according to your mapping entities.
If you sill need SQL query use createNativeQuery method.
See documentation about how to create HQL query.
Got it. See below a fully example of joins. It consists of:
multiple joins (join-chaining)
a subquery
a predicate correlation/equation over join tables, other than the root table.
I also commented the obsolete code lines for other to see what a wrong approach.
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery mainQuery = criteriaBuilder
.createQuery(PubThread.class);
// 1) MainQuery
// Create the FROM
Root<PubThread> rootPubThread = mainQuery.from(PubThread.class);
// Create the JOIN from the first select: join-chaining. You only need the return for ordering. e.g. cq.orderBy(cb.asc(categoryJoin.get(Pub_.title)));
Join<Pub, PubCategory> categoryJoin = rootPubThread.join(PubThread_.pups).join(Pub_.pubCategory);
// Create the WHERE
mainQuery.where(criteriaBuilder.not(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId)));
// Create the SELECT, at last
mainQuery.select(rootPubThread).distinct(true);
// 2) Subquery
Subquery<PubThread> subquery = mainQuery.subquery(PubThread.class);
Root<PubThread> rootPubThreadSub = subquery.from(PubThread.class);
//subquery.where(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId));
Join<Pub, PubCategory> categoryJoinSub = rootPubThreadSub.join(PubThread_.pups).join(Pub_.pubCategory);
subquery.select(rootPubThreadSub);
//Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);
Predicate correlatePredicate = criteriaBuilder.and(
//criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread),
criteriaBuilder.equal(categoryJoinSub.get(PubCategory_.id), categoryJoin.get(PubCategory_.id)),
criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), threadId)
);
subquery.where(correlatePredicate);
//Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);
Predicate mainPredicate = criteriaBuilder.and(
criteriaBuilder.not(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId)),
criteriaBuilder.exists(subquery)
);
//cq.where(criteriaBuilder.exists(subquery));
mainQuery.where(mainPredicate);

Syntax Error for HQL JOIN Statment

I'm trying to create a HQL query to join a result of a select with another table. What I have currently is
SELECT e FROM Experience e JOIN (SELECT f FROM Follow f WHERE f.username = :username AND f.contentType = :contentType) AS A ON (e.experienceId = A.contentId);
Its SQL equivalent that currently works is
SELECT t_experiences.* FROM t_experiences JOIN (SELECT * FROM t_follows WHERE t_follows.username = :username AND t_follows.content_type = :contentType) AS A ON (t_experiences.experience_id = A.content_id);
Where t_experiences.experience_id is equivalent to Experience.experienceId, etc. The current error in the HQL is on the first ( with unexpected token: ( error.
Any help would be greatly appreciated!
Why don.t you try this:
SELECT e.experienceId FROM Experience e, Follow f WHERE f.username = :username AND f.contentType = :contentType AND (e.experienceId = f.contentId);
I think this should work for you.
Note: Replace e.experienceId by parameters which you want.

HQL right join empty list

I am using hql with hibernate. I've tried to write a query over three tables. The tables are Medientyp, RaumMedientyp and Raum. I wanna read all Medientyp's from the whole table and all RaumMedientyp's from a specific raumId.
The realtions between the tables are:
Medientyp 1 <--> mc RaumMedientyp mc <--> 1 Raum
I need all as RaumMedientyp Entity.
The query i tried is:
String hql = "from RaumMedientyp rm right join rm.medientyp m inner join rm.raum r where r.id = :raumId";
Query query = getSession().createQuery(hql);
query.setInteger(":raumId", raumId);
But if i'm executing
query.list();
i've got a empty list as return.

Categories