I'm trying to convert a native query into JPQL. In the native query there's a subquery in from clause. Something like below
select * from table1 join (select * from table2) on ...
Since I wasn't able to write the same in JPQL, as it doesn't allow subqueries in from clause, I have created another entity class which is annotated with hibernate #SubSelect("select * from table2"). Now I'm able to use this entity in the from clause of the above query in place of subquery.
However there are 2 things that are not quite right
#SubSelect is not working with JPQL query, so I have to put native query.
#SubSelect is a hibernate specific annotation and I'm trying to avoid hibernate specific packages.
Is there a #SubSelect equivalent for JPQL?
Any hint is appreciated.
Related
Is it possible to perform a join with CriteriaBuilder on a table that is not referenced by the selected entity? Since CriteriaBuilder.join() expects as parameter the attribute name, it seems like it won't work.
To be a bit clearer, the original query looks like this:
select Vehicle v left join VehicleStatus vs on v.id = vs.vehicleId...
Vehicle does not define a relationship to VehicleStatus. And changes to the database are currently undesired though possible if needed.
Currently the code I have
final Join<Vehicle, VehicleStatus> vs = vehicle.join("vs", JoinType.LEFT);
vs.on(cb.equal(vs.get("vehicleId"), vehicle.get("id")));
fails with java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [vs] on this ManagedType
No, you need a mapped association to create a join clause with the CriteriaBuilder.
With Hibernate, you can join 2 unassociated entities in a JPQL query. The syntax is almost identical to SQL. But it's a Hibernate-specific feature and not part of the JPA standard.
I want to join three tables in a query but I do not know how to do that.
I tried:
List<Subclasses> list=em.createNativeQuery("select a.*,b.*,c.* from Subclasses a,Subs b,Klasses c where Subclasses.subid=Subs.subid and Subclasses.Klassid=Klasses.klassid");
But, I did not work.
Is there any way to write criteria query on entities not having explicit joins ? By explicit join I mean that 2 tables in database have no foreign key relationship but some columns need to be fetched from both the tables so joins are required in query. I know that queries having join can be written with 'in' clause and criteria queries can be written with "In" criteria. I have written HQL for this case but please tell me how to write criteria query for this case.
Thanks in advance
In this case, the cross join would be solution, but that is possible ONLY with HQL. Check doc (small cite):
16.2. The from clause
Multiple classes can appear, resulting in a cartesian product or "cross" join.
from Formula, Parameter
from Formula as form, Parameter as param
And, also, we can filter on any of these two Entities inside of the WHERE clause, to narrow the cartesian product...
Example:
List<Object[]> list = em.createQuery(
"SELECT 'Foo', 123 FROM IrcEvent ev", Object[].class).getResultList();
What I don't like in that example:
How do I know the table name? Can't I specify the entity class instead?
How do I know the column name? jOOQ provides auto-completion by creating a DSL from the database schema.
There could be syntax errors everywhere.
What I basically want is something like
entityManager.deleteAll(EntityClass.class);
to delete the rows of an antire table (for example).
JPA 2 Criteria API http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html is for creating type safe queries programmatically, but it does not support deletes
I am having some problem with the setParameterList api of hibernate.
I am trying to pass in a Collection to a SQLQuery and doing an "in" clause search.The records exists in the DB and doing a raw query, I am able to retrieve them or if I just replace them in the same Hibernate SQL like emp.emp_name in ('Joe','John'), I am able to get the desired result set. I am confused as to why would Hibernate fail to replace the Collection in place of the named parameter. Here is the code :
session.createSQLQuery("select emp_id as id from emp where emp.emp_name in (:empNames)")
.addScalar("id",Hibernate.INTEGER)
.setParameterList("empNames",new String[]{"Joe","John"})
.list()
I have looked at the Hibernate Documentation for setParameterList but I am not able to reason out this particular behavior.
I suspect the problem is precisely because you're using createSQLQuery. The single parameter here needs to be changed into multiple parameters in the real SQL, but by using a "raw" query you're telling Hibernate not to mess with the SQL.
Can you use a "normal" Hibernate query instead?
Just remove the parenthesis around the parameter name :
session.createSQLQuery("select emp_id as id from emp where emp.emp_name in :empNames ")
.addScalar("id",Hibernate.INTEGER)
.setParameterList("empNames",new String[]{"Joe","John"})
.list()
I would not suggest to use (N)Hibernate's parameter lists. Query plans in cache are not used when the number of elements in parameter list is different. So it means your query is often hard parsed and compiled. Queries are slower, database load is higher and plan cache is full of plans generated for the same query.