Casts in JPQL with EclipseLink - hql

Is it possible to use casts in JPQL? In HQL it seems to be supported
cast(... as ...), where the second
argument is the name of a Hibernate
type, and extract(... from ...) if
ANSI cast() and extract() is supported
by the underlying database
source : Hibernate reference
My target JPA provider is EclipseLink, so any EclipseLink specific solution is ok as well.

According to the specification of JPA/JPA2, JPQL is not supporting query casts.
However, the most recent EclipseLink Release 2.1.0 does have a propritary support for downcasts in JPQL and expressions, please look here

Literal downcasts in EclipseLink JPQL are currently not supported, but might be if the following issue gets resolved : https://bugs.eclipse.org/bugs/show_bug.cgi?id=315087

It should be supported using JPA 2.1 TREAT operator.

Related

JpaSpecificationExecutor vs QueryDslPredicateExecutor

I want to know which approach is better when to use JpaSpecificationExecutor and QueryDslPredicateExecutor?, yes we can use both of them at the same time.
using queryDSL will create many additional classes, is it bad or good for memory usage and performance?
It's a matter of taste.
If you use JpaSecificationExecutor you relay on the standard JPA Criteria and Metamodel API. There the Metamodel API will create a meta model based on your mapping that you should use to be typesafe at compile time.
Where as QueryDSL is an additional framework that is not limited to JPA.
You can use it with JPA, SQL, Collections, MongoDB, Lucene and maybe more.

Using SOQL queries in Spring JPA

I have to use SOQL queries within a Spring Data Repository, Is there a way to do so by using #Query annotation ? If not is there a alternative way ?
As far as I know salesforce doesn't expose the table structures. Rather they expose their objects and you can write queries on them. spring-data-jpa is used on top of an entity framework like hibernate. Unless you have entity objects mapped to actual database tables, spring-data-jpa is not useful.
The best way would be to use a query builder like jooq and construct SOQL queries easily using query builders.

DB2 "With UR" Spring Data JPA

I am using a Spring Data Repository interface to retrieve data from DB2 (z/OS). I have a couple of methods that rely on the method signatues and one that relies on an explicit #Query.
How do I make the SQL generated have the "WITH UR" clause?
I added #Transactional(isolation=Isolation.READ_UNCOMMITTED) on the line above the #Query annotation and also above the two methods but it doesn't seem to append the clause to the generated SQL.
Thanks

DDL generation with EclipseLink on Oracle RDBMS: Generate varchar2 fields with character length semantics

I use eclipselink.ddl-generation.output-mode=database to generate the database schema. Can the generated SQL use character length semantics for generation of varchar2 fields? Enhance generated DDL of EclipseLink seems to related and an answer to this question would probably solve this.
There are two ways to change the SQL that is generated. The first would be to change the target database platform and is EclipseLink specific. This allows you to pick any database platform class, and you could override your database's platform to use what ever type definition you wanted, but this would be generic - ie all Strings would use a Varchar(255).
Another way is to change it using JPA annotations. The #Column annotation allows specifying the length which may or may not be used for DDL, as well as defining the columnDefinition which is used for DDL. Something like
#Column(name="..", columndefinition="Varchar2(255) NOT NULL")

Is a Spring Data JPA #Query dynamic or named?

JPA #NamedQuery is translated to SQL only once when application is deployed and generated SQL is cached.
EntityManager.createQuery translates query every time our method is called.
What Spring-data-jpa is doing with query defined in #Query annotation? Is it translated to SQL once during deployment (like NamedQuery) or translated every time (like dynamic query) ?
Spring Data JPA calls EntityManager.createQuery(…) for every invocation of a query method annotated with #Query. The reason for that is quite simple: the Query instances returned by the EntityManager are not thread-safe and actually stateful as they contain bound parameter information.
That said, most of the JPA persistence provider perform some kind of text-based query caching anyway so that they basically build the actual SQL query once for a certain JPQL query and reuse the former on subsequent invocations.
As an interesting side note, when we started building the support for #Query in 2008 we looked into possibilities to rather register the manually declared queries as named queries with JPA. Unfortunately there wasn't - and up until today - there's no way to manually register a named query programmatically via the JPA.

Resources