How to run the update query in JPA - jpa

I am playframework application developer. I am using createNativeQuery method in jpa to extracting values from tables through select query. I need to use update query. What i have to do and what will be the return type of that method. Please anyone help me. Thanks in advance..
if i use like this it showing error..
Query query=JPA.em().createNativeQuery("update truck set flag='YES' where shipment_upc=:EAN_code");
query.setParameter("EAN_code", EAN_code);
System.out.println(query.getSingleResult());

Use createNativeQuery with your update- query and you will get back a Query- Object.
On it use executeUpdate and you get back the number of updated datas.

Related

Increment embedded entity property using function style value using Typeorm #createQueryBuilder

Here is my entity definition
What I want to achieve is increment
status.sentCount
on every api call made.
I have gone through documentation and tried to implement the solution for embedded entity
Although it work for direct field like "age" but doesn't seem to be working for simple-json object "status".
Thank you. Any help is appreciated.
You're dealing with jsonb type data, which you cannot update directly. You can use jsonb_set function provided by Postgres.
set(`jsonb_set(status, '{sentCount}', 'new_value')`)
I didn't try it but I hope it will work for you.
You can find more info here https://aaronbos.dev/posts/update-json-postgresql
Edited:
Use following query
UPDATE your_table SET status = jsonb_set(status, '{sentCount}', (COALESCE(status->>'sentCount','0')::int + 1)::text::jsonb) WHERE id = 1;

Talend- get results from tdb2Row component and insert into another table

I'm running a select count query in a tDB2Row component. I need to get the value found and insert it into another table.
I've tried using the propagate query's recordset, but it doesn't make sense to me.
Also what component would i use next?
Thanks in advance!
Please use tDB2Input component for select query and give schema to it and add tLogRow to it to see the result.
tDB2Row is for dml query like update and delete.
Hope this helps...

Zend framework db update not working

Am developing an app using zend framework. I want to update some columns in a table using zend db update but its not working. My code is something like this.
$where=$table->getAdepter()->quoteInto('from=?',$user_id);
$numrows=$table->update(array('read'=>1),$where);
But as I told this is not working. I tried passing where clause as array like this:
$table->update(array('read'=>1),array('from'=>$user_id));
I also tried passing where as string:
$table->update(array('read'=>1),'"from"='.$user_id);
But none of these are working. Can anyone help.
Got the answer. The problem was about keywords. to and from keywords are reserved in SQL and that was creating problem. I changed the column names and it worked. Thanks all.

Writing a query with ORMLite

How can I write a query with ormlite instead of using .create or any other thing like that? Can you please show me how for this simple example :
SELECT name FROM client
EDIT since I can't answer myself :
I guess I had to search a little more , anyway I found how to do it with the QueryBuilder like this :
newDao.query(newDao.queryBuilder().where.eq("name",valueofname)
If someone knows how to write the full query that would be great , otherwise , I'll stick with this solution
How can I write a query with ormlite instead of using .create or any other thing like that?
Goodness, there are tons of documentation about how to do this on the ORMLite site. Here's the section on the query builder.
I'm not sure what you mean by "full query" but your example will work with some tweaks:
List<...> results = newDao.queryBuilder().where().eq("name",valueofname).query();
It does not make sense to just return the name since the Dao hierarchy is designed to return the specific Client object. If you just want the name the you can specify the name column only to return:
... clientDao.queryBuilder().selectColumns("name").where()...
That will return a list of Client objects with just the name field (and the id field if it exists) extracted from the database.
If you just want the name strings then you can use the RawResults feature.

SqlDataAdapter Update

Can any one help me why this error occurs when i update using sqlDataadapter with join query
Dynamic SQL generation is not supported against multiple base tables.
You have a "join" in your main query for your dataset (The first one in the TableAdapter with a check by it). You can't automatically generate insert/update/delete logic for a TableAdapter when the main query has multiple tables referenced in the query via a join. The designer isn't smart enough to figure out which table you want to send updates to in that case, that is why you get the error message.
Solution. Ensure that your main query only references the table you want the designer to write insert/update/delete code for. Your secondary queries may reference as many tables as you want.
It was in the case that i was trying to set value for identity column in my datarow. Simply i deleted the code to set value for identity column and it will work.
My Scenario:
Database:
uin [primary, identity]
name
address
Whenever i tried to set the datarow("uin") the error occurs. But works fine with datarow("name") and datarow("address").
Hope it works for you too