Function inside Hibernate Criteria - java

I'm trying to create a Hibernate criteria filter that uses a database (Oracle) function as part of the Where.
So far, I have it working with sqlRestriction...but I can't seem to ge the {alias} to work (I'm trying to feed in the "id" of the current row as a function argument).
e.g., my function takes 2 arguments:
def query = {
and {
sqlRestriction("F_FGAC('SomeText',{alias}.id) = 'Y'")
}
}
The error I get is:
No signature of method: basecas_05.EmplController.sqlRestriction() is applicable for argument types: (java.lang.String, java.lang.String) values: [F_FGAC('SomeText',{alias}.id) = 'Y'...].
Any idea what the problem is here...?

I see the problem...it's not with the alias, it's that I was not putting the right arguments into the method...this works:
sqlRestriction("F_FGAC('SomeText',{alias}.id) = 'Y'")

Related

How to pass Runtime query to SqlTransform in apache beam?

I want to pass the Query dynamically while running the Dataflow job. I am using SQLTransform which works fine when I pass Query within code.
My use case requires passing the Query at Runtime, is it possible with SqlTransform in Apache Beam?
This works if I hard-code it in code.
String PQuery = "SELECT col1, max(col2) as max_watermark FROM PCOLLECTION GROUP BY col1";
PCollection<Row> rows1 = rows.apply(SqlTransform.query(PQuery));
But with valueProvider input, it gives compile time error.
PCollection<Row> rows1 = rows.apply(SqlTransform.query(options.getQuery()))
Error
The method query(String) in the type SqlTransform is not applicable for the arguments (ValueProvider<String>)
To solve your issue, you need to get the value inside the ValueProvider :
PCollection<Row> rows1 = rows.apply(SqlTransform.query(options.getQuery().get()))
The query method takes a String as parameter, that's why you need to get the String value of the ValueProvider option.
You should use FlexTemplates which allow dynamic graph construction (such as SqlTransform) uses based on template parameters.

CrudRepository - Stored Procedure call is not working because of type/number of argument issue

I have JPA entity as
And then Repository as
Now, when I run it then I get following exception:
And stored procedure is:
It is running against Oracle database. Can someone please help me understand even though I have correct parameter numbers and type, still why I am getting this exception.
Please note: I do not have local environment so I cannot put a sample code, and please do not worry about class/method name, I tried to camouflage them so they may be inconsistent.
There is one more question that suppose I have 2 OUT parameters then how you I create my entity class, with one output parameter I know I can return String (or appropriate return type) but in case of 2 OUT parameters I do no know how to do it? I have read this article but it is only with 1 OUT parameter, and I couldn't find any article or post which explains for 2 OUT parameter. If someone has a code with 2 OUT parameter then it would be helpful.
Please try:
use the exact (db) names of procedure parameters:
#StoredProcedureParameter(name = "tbl_name" ...
#StoredProcedureParameter(name = "p_date" ...
#StoredProcedureParameter(name = "p_message" ...
or (alternatively) omit names completely (rely on position).
From StoredProcedureParameter javadoc:
The name of the parameter as defined by the stored procedure in the database. If a name is not specified, it is assumed that the stored procedure uses positional parameters.
Currently you can't have multiple OUT-parameters using spring-data, but (should be) no problem with standard JPA:
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("my_proc");
proc.setParameter("p_in", 1);
proc.execute();
Integer res1 = (Integer) proc.getOutputParameterValue("out1");
Integer res2 = (Integer) proc.getOutputParameterValue("out2");
see also: Spring Data JPA NamedStoredProcedureQuery Multiple Out Parameters

JPA calling stored procedure with argument

I have this procedure in my postgreSQL database:
CREATE OR REPLACE FUNCTION getItemsForCategory(categoryId integer)
RETURNS SETOF ITEM AS $_$
DECLARE
result ITEM;
BEGIN
FOR result IN SELECT *
FROM item it
JOIN item_category itcat ON it.id = itcat.item_id WHERE itcat.category_id = categoryId LOOP
RETURN NEXT result;
END LOOP;
END; $_$ LANGUAGE 'plpgsql';
which works excellent using terminal, but I have trouble with calling it using JPA. Here is my code snippet(4 is value of argument cateforyId):
transactions.begin();
final StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("getItemsForCategory");
storedProcedureQuery.setParameter(1,4).execute();
final List<ItemEntity> itemEntityList = (List<ItemEntity>) storedProcedureQuery.getResultList();
transactions.commit();
after running code above I receive this error:
Exception in thread "main" java.lang.IllegalArgumentException:
You have attempted to set a parameter at position 1 which does not exist in this query string getItemsForCategory
Has anyone some idea how to set the value of argument correctly? I've also tried to set parameter using 0 instead of 1, calling setParameter with others datatypes of arguments (String,Object) but everytime I am receiving familiar kind of error like the one, which is shown there. Thank you very much
You need to register your parameters, before setting values.
spq.registerStoredProcedureParameter("categoryId", int.class, ParameterMode.IN);
See this link.

Playframework get List of Objects as Options in select

I am using Playframework 2.1.1 and Java. I am trying to fill a selectbox with data I retrieve from a database using the Play formhelpers
Here is some code if the view:
#helper.form(action = routes.Admin.submitUnit) {
#helper.select(
field = unitForm("metaunit"),
options = options(Metaunit.find)
)
...
}
And the Method to retrieve Metaunits from db:
public static List<Metaunit> find(){
Query query = JPA.em().createQuery("SELECT e FROM Metaunit e");
return (List<Metaunit>)query.getResultList();
}
When I try to compile it, I get the following Error-Message:
Overloaded method value [apply] cannot be applied to (java.util.List[models.Metaunit])
Any help is appreciated! Thanks
Take a look into computer-database-jpa sample ie. options() method in Company model in general it returns Map<String, String>.
As you can see in the editForm view, usage is pretty similar to your.
Note: probably you Metaunit is connected with some M-M relation, in that case most probably you will need to use unitForm("metaunit.id") as a field's value

Querydsl need to query where integer column is less than value

I am having trouble writing a JPAQuery that will execute a simple less than or equal to comparison on an integer column. I have QCommand generated querydsl object which I am trying to use with a JPAquery to execute this very simple query. The query I want to execute would look like this using SQL:
select * from command where retry_count <= 10;
The generated QCommand object defines the retryCount parameter as:
public final SimplePath<Integer> retryCount = createSimple("retryCount", Integer.class);
I have noticed that because it is a SimplePath, when I try to write the JPAQuery, the retryCount variable does not have comparison operators like loe or goe. That column/variable only allows me to do a isNull, isNotNull, eq, and other very simple operators, but no comparison operators. This would look like below, however, obviously not execute the comparison I want, but it works without error:
JPAQuery query = new JPAQuery(entityManager);
query.from(command).where(command.retryCount.isNotNull()).list(command);
In looking around I saw there is a way to create a NumberPath, which seems to be what I want, however, when I attempt the below code I get an exception "java.lang.IllegalArgumentException: Undeclared path 'retryCount'. Add this path as a source to the query to be able to reference it."
JPAQuery query = new JPAQuery(entityManager);
NumberPath<Integer> retryCount = new NumberPath<Integer>(Integer.class, "retryCount");
return query.from(command).where(retryCount.loe(10)).list(command);
So, how can I "add" this path to the source. I have looked around and I cannot find an example that does this nor an explanation as to how this is supposed to be done. The javadocs are not very helpful in this situation. I had been moving along nicely with querydsl, but this is the first roadblock and I'm stumped on writing one of the most simple queries. Any assistance or advice would be greatly appreciated.
The query with manual paths works like this
JPAQuery query = new JPAQuery(entityManager);
NumberPath<Integer> retryCount = new NumberPath<Integer>(Integer.class, command, "retryCount");
return query.from(command).where(retryCount.loe(10)).list(command);
The difference is that you create the retryCount path as a top level path, where as it should be created as a property path with a parent.
How does the Command class look like? There might be some issues in the code generation.

Categories

Resources