JPA QueryDSL: count() leads to ORA-01722: invalid number - oracle

I have the following Query, which tries to find the count of all RUNNING Trackings:
new JPAQuery(getEntityManager()).from(myTracking)
.where(myTracking.trackingStatus.isNotNull(),
myTracking.trackingStatus.eq(TrackingStatus.RUNNING))
.count()
This leads to ORA-01722: invalid number. Why?
EDIT: Logging of the SQL statement:
/* select
count(myTracking)
from
MyTracking myTracking
where
myTracking.trackingStatus is not null
and myTracking.trackingStatus = ?1 */ select
count(mytrackin0_.ID) as col_0_0_
from
owner.T_my_TRACKING mytrackin0_
where
(
mytrackin0_.STATUS is not null
)
and mytrackin0_.STATUS=?

The solution is I forgot to add the #Enumerated(EnumType.STRING) to my Enum attribute.
MyEntity{
#Enumerated(EnumType.STRING)
private TrackingStatus trackingStatus;
}
This was resulting in the Enum-Position and NOT the Enum-String beeing inserted into the Database-Column.
Once I added the annotation the still running trackings in the database were compared with their Enum-Position to the Enum-String from the query -> ORA-01722: invalid number.
The solution was to delete/migrate the few wrong mapped trackings.

Related

Laravel unable to update field to null

I can't figure out how to set a field to null on certain condition. It should be an easy query but I'n not able to set the right value for NULL on a nullable field.
Product::where('id', $product_list)->update(['family_id' => null]);
nor
Product::where('id', $product_list)->update(['family_id' => DB::raw(null)]);
did the trick.
Both cases compiled fine but the generated query seems to be wrong because I get a SQL error. In the irst case the error is :
SQLSTATE[HY093]: Invalid parameter number (SQL: update `products` set `family_id` = ?, `products`.`updated_at` = 2019-11-12 08:41:07 where `id` = ?)
and the error for the second one is :
QLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', `products`.`updated_at` = ? where `id` = ?' at line 1 (SQL: update `products` set `family_id` = , `products`.`updated_at` = 2019-11-12 08:21:50 where `id` = ?)
In the first case it seems that NULL is not being added to the parameters to the prepared query, and in the secon the NULL value is being inserted as nothing in the query. There is any simple way to set the value to NULL?
You should consider the following things first.
1) family_id column is nullable in your table. if not then set nullable.
2) Make sure you have in $fillable property of Product model column family_id.
Make sure $product_list returns id of that particular product.
if $product_list is collection/array of product data then use like:
Product::where('id', $product_list->id)->update(['family_id' => null]);
Try this
Product::where('id', $product_list)->update(['family_id' => 'NULL']);

Optional parameter in query does not work for db2 but works in oracle

Below query works fine for Oracle database, but for Db2 it throws error sqlcode -417.
I have looked up similar problems but did not get any definitive answer:
#Query(value = "select * from tableName f where (aC is null or f.a_c = aC)", nativeQuery = true)
Page<tablename> findByFilters(String aC, Pageable pageable);
On execution the error code is -417
One way to solve this problem would be to rewrite the query as the following:
select * from tableName f where f.a_c = coalesce(aC, f.a_c)
The form of query above is likely to be accepted. In order to find out the exact reason of the error you get, it is necessary to trace the actual SQL statement passed to Db2.
Another option of solving the problem may be to add the CAST operator, which would define the data type of your parameter:
select * from tableName f where (cast(aC as integer) is null or f.a_c = aC)
This is suggested in the description of the error you get

Doctrine DQL how to write SELECT EXISTS

This is based on valid SQL but I cannot get it to work in DQL
SELECT
EXISTS (
SELECT activity FROM Entity\Activity activity
WHERE activity MEMBER OF person.activities
AND activity.name = 'sports'
) AS doesSports,
person.id
FROM Entity\Person person
This gives the error:
[Syntax Error] line 0, col 7: Error: Expected known function, got 'EXISTS'
EXISTS seems to work like this after WHERE but not in select
The grammer does not permit so-called "Other Expressions" in the SELECT clause, only functions are allowed.

Issue with selecting max id rows using criteria query / hibernate query?

I am unable to select the rows where TestId is max for respective student, I wrote the code as follows which does not get the required output. my code is as follows,
Criteria c = sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList().add(Projections.property("answer"),"answer"));
c.add(Restrictions.eq("surveyId",send_Survey));
//c.add(Restrictions.eq("testId", "1" ));
//c.setProjection(Projection.max("testId"));
c.addOrder(Order.desc("testId"));
c.add(Restrictions.eq("questionid",FinalQuestionsOne));
List<String> age=c.list();
My table structure is as follows,
I need the following output. select the answer column for max TestId's. How can I get the output using criteria query
So I think what you're trying to get can be achievedd by the following sql:
SELECT TestId, MAX(answer) WHERE questionId = 1 GROUP BY TestId;
You should be able to achieve this with the following Hibernate:
sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList()
.add(Projections.property("TestId"), "TestId")
.add(Projections.groupProperty("TestId"))
.add(Projections.max("answer")));

getting Hibernate error when using DBMS_RANDOM.value

I would like to achieve same result as the below query using Hibernate SQL, i.e., I would like to get two random records from the table whose ID is not equal to 300. I am using Hibernate 4.1 and Oracle 11g. I ran the below query on Toad and it gives 2 random records. But, when I try to run the HQL, there is error to do with the usage of "DBMS_RANDOM.value".
SELECT * FROM
( SELECT *
FROM table
where ID != '300'
AND q_ID=125
ORDER BY DBMS_RANDOM.value
)WHERE rownum < 3
;
I tried creating criteria and query, but both give Hibernate errors:
Hibernate Message: Invalid path: 'DBMS_RANDOM.RANDOM' [from com.model.table tab where tab.ID != '33092' ORDER BY DBMS_RANDOM.RANDOM]
and my actual hibernate query is:
Query query = session.createQuery("from table tab where tab.ID != '" +agrmId+"' ORDER BY DBMS_RANDOM.RANDOM").setMaxResults(2);
I also tried ORDER BY rand() and that gives an Oracle error.
Thank you for any help.
I solved the problem by adding a property tag in the hibernate mapping file:
<property name="constVal" formula="DBMS_RANDOM.RANDOM" type="long"/>
and then, in the POJO class, I added a variable with getter and setter methods:
private long constVal;
then, in the DAO class, I added the following query:
Criteria crit1 = session.createCriteria(table.class);
crit1.add(Restrictions.ne("id",300));
crit1.add(Restrictions.eq("quesId",125));
crit1.addOrder(Order.asc("constVal"));
crit1.setMaxResults(2);
and that solved it.

Resources