You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by aroche <ar...@gmail.com> on 2013/02/07 11:26:51 UTC

Deleting based on CriteriaQuery

We are using a 3rd party tool for displaying table lists on screen
(specifically Vaadin CriteriaContainer though it probably doesn't matter
here). It uses CriteriaQueries for filtering data in the table.

One of the use cases is to delete all items that match the filter (which is
basically a CriteriaQuery).

I'm looking for the cleanest way in OpenJPA to convert a CriteriaQuery
(SELECT) to a DELETE. 
JPA 2.1 introduces this functionality but obviously that is some way off in
the future.

So one solution I'm thinking about is to get the QueryImpl from the typed
query.
ie. org.apache.openjpa.persistence.QueryImpl query =
typedQuery.unwrap(org.apache.openjpa.persistence.QueryImpl.class);

Then to get the Select query and do wrap in a delete and bind the parameters
etc to perform the delete.

Would this work?
Is there a neater way to do this? ie. using QueryImpl, or TypedQuery etc.?

Thanks for any advice!



--
View this message in context: http://openjpa.208410.n2.nabble.com/Deleting-based-on-CriteriaQuery-tp7582729.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Deleting based on CriteriaQuery

Posted by aroche <ar...@gmail.com>.
Thats pretty much what I'm seeing. The query string probably isn't close
enough to JPQL to generate a delete query/update from.

Is there any other way in OpenJPA to execute a delete based on a
CriteriaQuery?
I know support for this is anticiipated in JPA 2.1 but need an interim
solution.

At the moment I'm looking at selecting back the IDs and feeding them into a
JPAQL update along the lines of: "DELETE FROM Entity e where e.id in (1, 2,
3, 4...)"; 


Any alternatives?

Thanks...



--
View this message in context: http://openjpa.208410.n2.nabble.com/Deleting-based-on-CriteriaQuery-tp7582729p7582736.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Deleting based on CriteriaQuery

Posted by Kevin Sutter <kw...@gmail.com>.
The getQueryString() was introduced as a helper method to aid with
understanding the Criteria API.  The returned string should be very close
to valid JPQL, since both mechanisms are using the same basic query
engine.  This method is not defined by the specification, so it doesn't
have to be "perfect" or spec-compliant.  But, if you have discovered an
issue with this method, opening a JIRA and providing a junit would be the
best means of reporting the problem.  Thank you.

On Thu, Feb 7, 2013 at 4:48 AM, aroche <ar...@gmail.com> wrote:

> Hmm, QueryImpl.getQueryString() unfortunately doesn't seem to return valid
> JPAQL or SQL
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Deleting-based-on-CriteriaQuery-tp7582729p7582730.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Deleting based on CriteriaQuery

Posted by aroche <ar...@gmail.com>.
Hmm, QueryImpl.getQueryString() unfortunately doesn't seem to return valid
JPAQL or SQL 



--
View this message in context: http://openjpa.208410.n2.nabble.com/Deleting-based-on-CriteriaQuery-tp7582729p7582730.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.