I have this query that I'm running in Java.
select book from com where genre=?;
I set the parameter for genre dynamically. Is there a way to sometimes set the parameter so that all data is selected?
The usual trick is to set a separate parameter for selecting everything:
SELECT book FROM com WHERE genre=? OR 1=?
When you set the second parameter to 0, filtering by genre is used, but when you set it to 1, everything is returned.
If you are willing to switch to using named JDBC parameters, you could rewrite with one parameter, and use null to mean "select everything":
SELECT book FROM com WHERE genre=:genre_param OR :genre_param is null
Do you mean you want to be able to use the same script and pass in a parameter that ignores your "Where"? Use this and pass in the string "AllData" to the second paramter to include all results and ignore your first parameter:
select book from com where (genre=? OR "AllData"=?);
Create a different query:
select book from com;
That will get the "book" column from all rows in "com".
Just use a wildcard.
Ex. SELECT book FROM com WHERE genre LIKE "%"+seachString+"%";
You can use CASE expression (SQL SQERVER):
select book from com where genre=CASE
WHEN ? IS NOT NULL THEN ?
ELSE genre
END
Related
I created a page to insert and modify data of an existing mysql- table.
But based on my requirements and the structure of the table I have to modify the sql for inserting data.
Because I am completly new on rapidclipse and java I need some hints/ examples how and where to modify this.
Looking all rapidclipse videos did not give the right hint.
I would like to insert three fields into a mysql-table
One of the fields I have to edit manualy.
The second field contains always the same value.
The third field contains a calculated value, which I have to fetch while runtime from the database.
As sql I would use following code:
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01)
VALUES ('T_supplier', (select * from (select max(cast(DMV_COL00 as
Integer)) +1 from OKM_DB_METADATA_VALUE as t2 where DMV_TABLE =
'T_supplier') as t3 ) , 'new suppliername');
The value for field DMV_Table will be always 'T_supplier'
The value for field DMV_COL00 is always the highest value in the col +1
The value for field DMV_COL01 will be always entered manually
(I am not able/ I don't want to modify/ use table form, -design and trigger, because it is a original table of OpenKM)
Thank you in advance!
best regards
OpaHeinz
Just a suggestion for sql code .. Your code could be refactored in a more SQL like code .. You could avoid the innner subquery .. and use a normal insert select
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01)
select 'T_supplier', max(cast(DMV_COL00 asInteger)) +1 , 'new suppliername'
from OKM_DB_METADATA_VALUE
where DMV_TABLE ='T_supplier'
The first step to solution
In the buttonClick event of save function
I set the value of DMV_Table field with:
... this.txtDmvTable.setValue("T_supplier");
The second step;
I created a view in the database wich delivers only the expected value:
`CREATE
OR REPLACE
VIEW `okmdb`.`V_suppliers_newID` AS
select
1 as "id",
max(cast(DMV_COL00 as Integer)) +1 as "newSupId"
from OKM_DB_METADATA_VALUE
where DMV_TABLE = 'T_supplier'; `
After that I created an entity in rapidclipse, read the value out of the view and assigned it to the other field DMV_COL00.
This was all.
I'm trying to create a query using CriteriaBuilder where I need to have a predicate where the value of the predicate is like the value in the database.
Basically, I need to be able to do the following:
WHERE myTestValue LIKE columnValue
In native queries, it is an option to do that, but using the CriteriaBuilder or NamedQueries, it does not seem to work.
String myValue = "foo#bar.com";
cb.where(cb.like(myValue, root.get(Entity_.email));
Is there an option in JPA to do it like this? Or should I fall back to native queries?
EDIT
I need to be able to check if a given value matches a wildcard entry in database. So the database has a record %#bar.com%, and I need to check if my given value foo#bar.com matches to that record.
I think your params should be other way round:
cb.where(cb.like(root.get(Entity_.email),myValue);
Aditionally you may need to use add this to the second param:
cb.where(cb.like(root.get(Entity_.email),"%"+myValue+"%");
Chris found the answer. First I need to "generate" a parameter.
ParameterExpression<String> senderEmailParameter = cb.parameter(String.class, "senderEmailParameter");
Path<String> senderEmailPath = root.get(Entity_.senderEmail);
Predicate predEmail = cb.like(senderEmailParameter, senderEmailPath);
And then I need to fill the parameter in the query.
q.where(predEmail);
return em.createQuery(q).setParameter("senderEmailParameter", senderEmail).getSingleResult();
This works! Thanks Chris!
Quick question... so in Hybris, I have a query similar to this:
"SELECT {CPR:pk} FROM {CategoryProductRelation as CPR}, ...."
Basically, I need to extract the Product Code and Category Code from Java which I think are available as source / target respectively but my question is, just like there's ProductModel, CategoryModel, etc. is there anything like that for CategoryProductRelation?, probably something like a generic RelationModel to simply extract source / target and go from there?
You'll need to JOIN in the entities like this
SELECT {CPR:pk}, {c.code} FROM {CategoryProductRelation as CPR
JOIN Category AS c on {CPR.source} = {c.PK} } WHERE ...
Also, you can do that in the Service Layer by simply calling your query and accessing the properties right from the relation type:
..
CategoryProductRelationModel model = result.get(0)
String categoryCode = ((CategoryModel)model.getSource()).getCode()
Depending on your amount of data, this could be pretty ineffecient.
I want to write a totally dynamic query method, which gets dinamically column names as parameters. Column names i.e : id, age, name, etc. I'm going to use criteria query, however I don't know how it's done exactly.
Some example says:
"Path<Long> idPath = personRoot.get( Person_.id );
Path<Integer> agePath = personRoot.get( Person_.age );
criteria.select( builder.array( idPath, agePath ) );"
My problem is the usage of "builder.array" part. How can I put together my (i.e:) path elements into a "Selection... selections" parameter in order to the "select" accept it and be valid my dynamic query?
Is there any possibility to write multiple group by according to my "Selection... selections" parameter in the "select" part?
Any hints appreciated, thank You in advance.
Selection... selections is just syntactic sugra for Selection[] selections. So you just need to create an array of selections, and pas this array as argument to the builder.array() method.
I am new to hibernate and still learning the basics. I'd appreciate if someone can point me in the right direction.
I have a class:
Destination
id
name
longitude
latitude
I can read destinations based on id with something like this:
List result = session.createQuery("from Destination as d where d.id=2").list();
However, I want to read destinations from database using name. I can perhaps write something like this as a query:
String name; // name set somewhere else, say a function argument
List result = session.createQuery("from Destination as d where d.name LIKE %"+name).list();
I believe this will yield all destinations with names similar to (variable) name.
Is there something inbuilt in hibernate for such use cases or is there a better way to handle this ?
EDIT:
One thing that follows from my thought process is: name column on destination db table will have an index setup. Can I map this index in some way to the Destination class using hibernate ?
You could build your query by concatenating strings. A more elegant solution would be to use the Hibernate Criteria API.
You query would then look something like:
List result = session.createCriteria(Destination.class)
.add(Restrictions.like("name", "%" + name)
.list();