ObjectDB Select query with too few parameters - java

I'm trying to populate a tsp page based on what parameter was passed to it. Currently I'm using the title of the blog post, I don't like this but I wanted proof of concept and had difficulties obtaining the object id. So, I'm temporarily passing in the title string with a view to change it later.
My query is throwing a too few arguments exception, none of the examples I've come across have lead me to believe that there is something missing so I'm slightly confused. The call is:
#Transactional
public objectName getObjectByTitle(String title){
TypedQuery< objectName > query = em.createQuery(
"SELECT b FROM tableName AS b WHERE b.title = :title",
objectName.class);
return query.getSingleResult();
}
with the error being:
com.objectdb.o._PersistenceException: Attempt to execute a query with too few arguments
It might be one of those cases where I'm looking too hard but I've scoured the objected site and cannot find a solution. Any help is appreciated.

You defined a named parameter in the query, and didn't bother setting its value (using setParameter), even though you went to the trouble of passing "title" in to the method ...

Related

Access user defined method properties on query - Esper

I implemented a query where i mined data from a database but i have to change it so i mine my data from a custom function in my code. I read the documentation and added the annotation import on the configuration. The query throws that error:
Failed to resolve event type, named window or table by name 'path.to.my.class.customfunction'
I don't know the type that my function have to return but i tried Arraylist and Hashmaps with key an integer and value a custom class and didn't work.
My final query want to look like this :
select * from LocationEvent as loc,
***CustomFuntion()*** as product
where loc.id=product.id ;
I kept the structure i used for database connection. I don't know if there is another way to solve this. Thanks.
EDIT: I managed to make call the custom function with that query :
select path.to.class.getProducts() as product from pattern[every timer:interval(3 sec)]
My function right now return an ArrayList and the query returns this:
[Product{ProductID=124,.....,},Product{...}]
So now my problem is that i can't access properties of Product on the query like products.ProductID
If you want to have a custom function in the from-clause you can use the "method:". The docs have this described here: Accessing Non-Relational Data via Method. The Esper runtime then calls your method to get events/rows.

#Query annotation with custom Class Spring Boot Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
question for JPA-Users, is the following somehow possible?
#Query(value = "SELECT * FROM Users u WHERE u.status = :sampleClass.status",
nativeQuery = true)
List<SampleClass> findBySampleClass(SampleClass sampleClass);
Notice the way I am trying to access SampleClass in the #Query annotation. Wasn't able to get this going, instead went for Criteria and constructed my query old-school.
There should not be any space in between = and : so change the query to below one.
SELECT * FROM Users u WHERE u.status =: sampleClass.status
Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??
see this for much clarity.
You can use like below also by indexing parameters like ?1.
#Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);
Refer : Quote from Spring Data JPA reference docs.
Also, see this section on how to do it with a named native query.
What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).
But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.
You could use the approach as mentioned in the JPA docs:
#Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
nativeQuery = true)
List<SampleClass> findBySampleClass(String status);
Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries
Not sure why you are trying to pass the whole object though.Would you care to explain?

Cannot get update query to work with Hibernate

I have a simple update query just to check if update query works:
this.openDBTransaction();
Query updateQuery = session.createQuery(
"UPDATE User AS usr SET usr.failedLogins=666 WHERE usr.id='USER_3000'"
);
int result = updateQuery.executeUpdate();
this.closeDBTransaction();
but somehow DB is not update with desired value. result came as 1 so something took place but for sure not update query.
Any clue what is going on?
You should use #Transactional annotation so that the compiler knows that the transaction is manipulating the database, thus permits to perform Data Manipulation queries or it will simply execute it as a Data Definition Language query.
Look at the code snippet below, for example,
#Transactional
public Employee editEmployee(Employee employee) { //employee is the data you got through post
return entityManager.merge(e1);
}
Also, the best practice is to always implement Data Access Object Interface and its implementation and define your queries in the implementation.
I hope this helps.

getObjectById throws ClassNotResolvedException

We are using mySQL + JDO + Datanucleus.
When I call pm.getObjectById(type,id); it throws the exception
javax.jdo.JDOUserException: Failed to find the object of with id "org.geppetto.persistence.db.model.GeppettoProject:1"
NestedThrowables:
org.datanucleus.exceptions.ClassNotResolvedException: Class "org.geppetto.persistence.db.model.GeppettoProject" was not found in the CLASSPATH. Please check your specification and your CLASSPATH.
However if before calling it I query the database with something like the following
Query query = pm.newQuery(GeppettoProject.class);
query.execute();
then from that moment on getObjectById starts returning the object. Is this behaviour correct? getObjectById seems to only return things once they are cached, i.e. only after a query was executed.
In general I wouldn't like to have to bother with this, if things have to be fetched for the first time then let it be, if they were already fetched give me whatever is cached but I wouldn't expect to have to know about it. Am I missing something?

java.sql.SQLException: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY

I have a method in Dao Class that returns List<Object[]> back and I am using named Query
public List<Object[]> getListByCustomer(Session session, int customerId, List<Integer> strIds) {
Query namedQuery = session.createSQLQuery(QueryConstants.EXPORT);
namedQuery.setParameter("customer", customerId);
namedQuery.setParameter("stringId", strIds);
List<Object[]> objects = namedQuery.list();
return objects;
}
I want to pass List<Integer> strIds in stringId into the named query as follows :
public class QueryConstants {
public static final String EXPORT =
"SELECT sv.NAME, sv.TYPE, sv.CLIENT_ADDRESS, sv.NAME_REDUNDANT, sv.DEPARTURE_DATE, s1.CODE,sv.STATE, sv.CODE "
+ "FROM VIEW sv, PROCESS p1, SET s1 "
+ "WHERE sv.R_ID = p1.R_ID and p1.ISSUER_ID = s1.USER_ID and sv.CUSTOMER_ID = :customer and sv.R_ID IN (:stringId)";
}
But I get ORA-00932: inconsistent datatypes: expected NUMBER got BINARY.
Also when I remove sv.R_ID IN (:stringId) from the query it works fine and
when I pass Integer(strIds) instead of List<Integer> strIds into the query it works fine.
I'm using Oracle 10g.
This is a very misleading error, and may root from different causes, for me I was setting a parameter that it was supposedly a number but at runtime it was setting null, hence it was binary. On a separate occasion got this error due to bean creation error in spring and was not setting the parameter correctly as well.
I think you just need to use
IN :stringId
instead of
IN (:stringId)
For JPA
namedQuery.setParameter("stringId", strIds);
is correct, but for Hibernate you should use
namedQuery.setParameterList("stringId", strIds);
I encountered this same exception and found the below reason for that -
In my entity, a field was mapped to a custom object (Parent child relationship - #ManyToOne). Later, the relationship annotation was removed by developer but the datatype was not changed.
After removing the #ManyToOne annotation, the #Column annotation should have been used with appropriate data type (Integer).
Case your param is list. If list is empty then raise error, you must check that list not empty to avoid error.
Case your param is single value. Let use TO_NUMBER(:your_param) to avoid error.
It working on me.
In my case, I was using HQL (in the repository of spring data) with an Entity mapped with #Enumerated (ORDINAL). I was trying to use the the enum object directly in the where clausule. The solution was to use TO_NUMBER(:your_param) as mentioned by the member above.
I got the same error but for a different reason. In my case it was due to the order the parameters were supplied being different from the order defined in the query. I assumed (wrongly) that because the parameters were named the order didn't matter. Sadly, it seems like it does.

Categories