Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JPA Criteria : LEFT JOIN with an AND condition

840578Feb 17 2011 — edited May 3 2011
Hi all,

I have a question regarding JPA criteria.

Here is my JPA query :

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<Email> emailRoot = criteriaQuery.from(Email.class);
criteriaQuery.distinct(true);

Predicate globalCondition = criteriaBuilder.equal(emailRoot.get(Email_.type), EmailType.in);

Predicate responseMandatoryCondition = criteriaBuilder.equal(emailRoot.get(Email_.responseMandatory), true);
Predicate typeCondition = criteriaBuilder.notEqual(emailRoot.join(Email_.emailsOut, JoinType.LEFT).get(Email_.responseType),ResponseType.response);
globalCondition = criteriaBuilder.and(globalCondition, responseMandatoryCondition);
globalCondition = criteriaBuilder.and(globalCondition, typeCondition);

em.createQuery(mainCriteria.where(globalCondition)).getSingleResult();

Here is the result of this query :

SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
AND emailso1_.RESPONSE_TYPE <> 'response'
LIMIT 0 , 30

And here is the request I needed :

SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN AND emailso1_.RESPONSE_TYPE <> 'response'
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
LIMIT 0 , 30

As you can see I need to check if the RESPONSE_TYPE is equals on the same line that the LEFT OUTER JOIN. Does anybody know how to write such an JPA criteria query ?

Thanks ,
Louis

Edited by: user13105928 on 17 févr. 2011 03:06

Comments

JustinCave
If you're familiar with nextval, I assume you are familiar with creating sequences and using sequences to populate primary keys...

You can always create a BEFORE INSERT trigger that automatically generates the primary key for the table. You would have to modify your INSERT statement to use the RETURNING clause if you want the client to have the value of the just-inserted primary key after the INSERT.

Justin
45878
You could create a trigger on the table which performs the "nextval" from a sequence table, that way your Java application does not need to do the work.
519688
use a sequence, and populate it via a "before insert for each row" trigger.

no other way.
514521
Not working..

From w/i SQLPLUS i execute this line:
create trigger dbame.p_trigger before insert on dbame.plant for each row

dbame is my oracle username
plant is the table name

SQLPLUS is looking for more information and/or a closing "tag" (not a semi-colon)

any help would be appreciated..

Thanks
Kamal Kishore
http://download-east.oracle.com/docs/cd/B10501_01/appdev.920/a96590/adg13trg.htm#431
519688
read the manuals
look for the section on table triggers
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 31 2011
Added on Feb 17 2011
2 comments
18,864 views