How do I write hql query with cast? - java

I need to combine 2 tables using hql, both are having common column, but table1 common column is integer and table2 common column is String
For example,
select a.id as id,a.name as name,b.address as address
from Personal as a,Home as b
where a.id=b.studid
Here a.id is an integer while b.stduid is a string, but Data of both columns is the same.
How can I get the result of the query using hql query?

HQL supports CAST (if underlying database supports it), you can use it:
select a.id as id,a.name as name,b.address as address
from Personal as a,Home as b
where cast(a.id as string) = b.studid
See also:
16.10. Expressions

You really need to think why have you got a need to join two entities by properties of different types. Most likely it suggests that some of the entities need to be refactored, which could include changing data types for columns of the underlying db tables. If the model is correct there will be no need to twist Hibernate.

I had to cast it to String like so :
#Query( value = "select new com.api.models.DResultStatus("+
"cast(ds.demoId as java.lang.String),cast(ds.comp as java.lang.String),cast(ds.dc as java.lang.String),cast(be.buildUrl as java.lang.String)")

Just noticed that you are using JPA, there you can not cast or convert datatpes. In the query language, only values of the same type can be compared! read in http://download.oracle.com/javaee/5/tutorial/doc/bnbuf.html#bnbvu

Related

join array field with ANY

Trying to join based on the ID of A that has at least one of the IDSs of table B. A.ID is of type varchar, B.IDS(which is converted as set) is of type varchar[]. I tried like below, but I get an error.
create.select()
.from(A)
.join(B)
.on(A.ID.equal(any(B.IDS))) // Cannot resolve method 'equal(QuantifiedSelect<Record1<T>>)'
.where(other conditions)
Correct jooq code matching below query.
select A.ID, B.ID from A a, B b where a.id = ANY(b.ids) and (other conditions)
The problem seems to be in this part of your question:
B.IDS(which is converted as set) is of type varchar[]
I'm assuming, you used a data type converter to turn your Field<T[]> into a Field<Set<T>>? While that is useful for projecting "better" types than the out of the box types jOOQ supports, it will prevent you from using some data type specific API, in this case array specific API, including the DSL.any(Field<T[]>) operator.
You have at least three options to work around this in this specific case:
Coerce the type back to String[] using DSL.coerce()
Use plain SQL templating, which is always a useful workaround when running into some limitation
Use rawtype casts to remove type safety
Would this do the trick?
(I assume the two table are SQL tables)
SELECT a.ID
FROM A AS a
WHERE a.ID IN (SELECT ID FROM B)

Different result mappings for the same query but with variable column - MyBatis 3

I need to get different java Pojo result mapping from same query with different column in sql "Where" condition.
Es.
#Select("Select field1,fiel2,...,fieldn From table where #{column}=1")
List<Pojo> getGenericDetails(#Param(column));
Mybatis "discriminator" doesn't make the job.
Can anyone know how to do that?
Thanks in advance
The Mapping what you have written works But it works on same POJO .
If you need different POJOs based on the column in where clause then you Have to write different queries for each of condition in where clause .
Which is as good as specifying column name in query .
So i suggest you to write different queryies and created Different POJOs and make the method name relevant to context where you will use those queryies
If want to get different java Pojo result mapping from the same query with different column in SQL "Where" condition, I will try the following:
#Select("Select field1,fiel2,...,fieldn From table where #{column}=1")
List<Map<String,Object>> getGenericDetails(#Param(column));
as a result, the key in the map is the field1,fiel2,...,fieldn the value will be the result of field1,fiel2,...,fieldn.
Hope the above helps.

convert SQL to HQL query

I want to retrieve some information from Oracle database.
The following query provides me with the required result:
select pl.*
from PEOPLE pl
INNER JOIN ANIMALS c on pl.ID = c.PEOPLE_FK
where c.ID=(select HEALTH_RES.ANIMALS_FK
FROM HEALTH_RES
WHERE HEALTH_RES='1234');
Explanation: each person may have many animals, each animal has only one health result. I want to find owner (all information about him) of animal by id of health result.
Now I have Entities mapped in java, and I want to use a HQL query to obtain such information, how would such query look in HQL?
I have tried this:
select pl from PeopleEntity as pl inner join pl.animals as c where c.Id=(select HealthResEntity.animals_FK FROM HealthResEntity WHERE HealthResEntity.id=:idParam)
But it seem to not work, what is wrong with this query?
EDIT: I am getting null pointer exception when calling method that converts Entity object to domain object which means no data was retrieved

How to fetch Data using JPA for dynamic Where Clause

I am new to JPA. I am currently using JPA2.0 in WAS 8.5.5
I have a search screen where we have lot of search criteria's. Now, I have to create Query in JPA such a way that any criteria user has selected, it automatically check for that particular column in DB.
I am not able to find any solution on that. It seems to me that for every search criteria, I have to write new named Query.
Any suggestion or pointers will be appreciated.
You could do it in named query, but you would have to check every single possible criteria if it is null in order to avoid null values affect the results. Beware of unnecessary inner joins, each nullable relation should be included as left join
select e from Employee e left join e.department d
where (:name is null or e.name = :name)
and (:email is null or e.email = :email)
and (:deptId is null or d.id = :deptId)
...
However, depending on complexity of possible combinations, better option could be to not use named queries, but dynamically construct a JPQL depending on selected criteria.

Hibernate Criteria and multiple join

is possible with Hibernate criteria do it?
select A.something, B.something, C.something, D.something
from A JOIN B on A.id = B.id_fk
JOIN C ON B.id = C.id_fk
JOIN D ON C.id = D.id_fk;
I have got exactly the same problem, and was able to resolve it like this:
return criteria.createCriteria(A.class)
.createCriteria("b", "join_between_a_b")
.createCriteria("c", "join_between_b_c")
.createCriteria("d", "join_between_c_d")
.add(Restrictions.eq("some_field_of_D", someValue));
Note: "b", "c" and "d" in code above refer to attribute names in A, B and C classes, correspondingly (class A has attribute b and so on).
For this solution you don't even need to have lazy and fetch parameters to be set in your A.hbm.xml.
There are some good examples in the Hibernate Reference material that show to use setFetchMode to fetch associations with an outer join.
An example is:
List books = sess.createCriteria(Book.class)
.setFetchMode("chapters", FetchMode.EAGER)
.setFetchMode("reviews", FetchMode.EAGER)
.list();
There is also information there about different fetching stragies that may be of use to you.
Try setting the fetch mode in your criteria, like:
criteria.setFetchMode(..., FetchMode.EAGER)
This creates a join query.
You may find more details here.
Yes, in fact there are several ways of doing this:
When mapping the association, set its lazyness to false and its fetch mode to join. This will affect all criteria queries.
Use setFetchMode as detailed by the other answers.
Use criteria.createAlias (or createCriteria). This also allows you to further restrict the rows you want joined.

Categories

Resources