SQL Error: 0, SQLState: 42703 with message "The column name str_id was not found in this ResultSet" - jpa

this is my first question ever in StackOverflow and as suggested, I have looked at other similar questions and attempted to use their responses for my problem. So far, no luck.
The situation is as follows:
I have a custom query in JPA.
#Query(value="SELECT u.str_id,u.str_exercise_name, u.str_target_body_part,u.char_effect FROM training_schema.exercise_entity u WHERE u.str_exercise_name = ?1 and u.str_target_body_part= ?2", nativeQuery=true)
ExerciseEntity findExerciseEntityByNameAndTargetBodyPart(String str_exercise_name,String str_target_body_part);
If I remove the name of the columns (u.str_id, u.str_exercise_name, u.str_target_body_part, u.char_effect) and replace the query with:
#Query(value="SELECT u FROM training_schema.exercise_entity u WHERE u.str_exercise_name = ?1 and u.str_target_body_part= ?2", nativeQuery=true)
ExerciseEntity findExerciseEntityByNameAndTargetBodyPart(String str_exercise_name,String str_target_body_part);
I get the following error:
"The column name str_id was not found in this ResultSet"
The fact that the error doesn't come when I mention all the columns and is generated when I use alias 'u' doesn't make sense because this would mean that if I ever had to work with a larger table with, say, 10 columns, I would have to write them all out.
One more piece of information that hopefully helps: With the version of the query where I am using 'u' instead of the column names, the error is ONLY generated when a matching record is found. For a null return from the database, there is no problem.
Using Java Spring and PostgresSQL.

I was able to figure out the problem.
In the query where I am using the alias 'u' ALONE, I had to make a slight change. Instead of just saying 'u', I changed it to:
#Query(value="SELECT u.* FROM training_schema.exercise_entity u WHERE u.str_exercise_name = ?1 and u.str_target_body_part= ?2", nativeQuery=true)
ExerciseEntity findExerciseEntityByNameAndTargetBodyPart(String str_exercise_name,String str_target_body_part);
Using only 'u', was returning a record set WITHOUT any headers. Adding the '*' caused the query to return a resultset with column names which made the error go away.

Related

coumn reference is ambiguous

Im getting error
"org.postgresql.util.PSQLException: ERROR: column reference "date_created" is ambiguous"
I have a Base class that defines the date_created field and then all the other classes extend it.
Im makeing a set of REST controllers. All of them use
"sqlRestriction("GREATEST(date_created, last_updated) >= ?", [fromLastUpdated])"
All of them use the same piece of code. All the other 10 cases it works, but with the 11th case it does not work. I dont get why. Ist nearly identical to all the other cases(difference is the other columns).
Where can this issue come from?
SOLUTION
Grails domain classes allow you to have refrences to other Tables
like
Table2 table
within your domain class.
This causes the hilbernate to create a join clause between table1 and table2.
So printed out the criteria created and made small modifications to fix the issue with ambiguiti
"sqlRestriction("GREATEST(this_.date_created, this_.last_updated) >= ?", [fromLastUpdated])"
this_ is the alias given to the domain on whitch you create the criteria.

Query annotation not working for max(id)

I have a domain object GenJournal and it has an "id" member (Long) that's auto-generated. I also have a JPA repository that I've added ...
#Query("select coalesce(max(u.id), 0) from GenJournal u")
Long getMaxId();
The method getMaxId() returns zero or null before I added coalesce. I have two rows in my database with ids 1 and 2. Can anyone help me determine why this doesn't work?
I'm trying to get the latest or max id so that I can use the find method after to return the most recent GenJournal object in my service class.
I'm stumped and really need some options or strategy to determine why this doesn't work.
You could use "Native Query" feature by passing nativeQuery = true param into #Query annotation like this
#Query("select coalesce(max(u.id), 0) from Gen_Journal_Table u", NativeQuery = true)
Long getMaxId();
My issue was two-fold. First I was getting null without the use of "coalesce". That caused me to think that this didn't work. When I adopted the use of "coalesce" I didn't realize that my table had no records and was returning the zero (0). My table in the production profile did have two records and I was expecting an id of 2.
I was manually checking the wrong database and setting expectations that were incorrect.

In orientdb what is difference between in('edge_type') and in(edge_type)

Trying this query in Grateful dead database provided in orientdb gives 146 records:
select expand(in('sung_by')) from V where name = 'Garcia'
But when we try the similar version of below query: select expand(in(sung_by)) from V where name = 'Garcia', 150 records are returned
Is it a bug?? Just trying orientdb from past week, followed tutorial from this website and this was second issue found.
By using select expand(in(sung_by)), the value of the field sung_by is resolved at query execution, but there is no field called sung_by, so it's null.
For this reason, it's like executing select expand(in()) in that case. By using 'sung_by', instead, only the edges with label sung_by will be traversed.
So, put always " or ' around edge's class/label to traverse.

SQL query column does not exist error

I'm totally new in this area please tell me how to fix my problem.
when I write this query "SELECT * FROM places" in my database everything is okay.
However when I change it to "SELECT * FROM places WHERE eventId=2", I get error. Please look at this image.
as you can see, eventId column is exist. Why my query throws error?
You've almost certainly added the column names in a case-sensitive environment. (PgAdmin comes to mind.) Lowercase them in that same environment to avoid the need to quote fields.
Or change your query to:
select * from places where "eventId" = 2

ERROR: operator does not exist: character varying = numeric

So I keep getting the title error. The string I am using to create the query is,
select p from Product p where p.productType.productTypeId in (:productTypeIds)
And here is a clip of the java
List<Long>partTerminologyIds = getProducTypeds(partTerminologys);
..........................................................................
query.setParameter("partTerminologyIds", productTypeIds);
I have no idea why I am getting this error, ane yes partTerminolgyId in my database is a numeric 18.
Any ideas???
So in the end it ended up being that a "foreign key" was a string in one table and an numeric in the other. I rebuilt the database with new scripts and did not reverse engineer the new database.
This query is invalid:
select p from Product p where p.productType.productTypeId in (:productTypeIds)
Do you mean:
SELECT p FROM product p WHERE p.productTypeId IN (:productTypeIds)
Or rather:
SELECT * FROM product p WHERE p.productTypeId IN (:productTypeIds)
And if so, what is the data type of productTypeId in your query. Please clarify.
This looks like a Hibernate query. You need to do query.setParameterList to specify a collection value, otherwise Hibernate won't know to expand out :productTypeIds to a list of placeholders instead of simply binding the list as a serializable blob (which I think is an awful default).