I have the following pl/sql query,
INSERT INTO Table(ID,..................)
VALUES(SEQ.nextval,....................);
SELECT SEQ.currval ID FROM DUAL;
I need to get ID using hibernate. I am using the following query which showing error,
.....getDataSession().createSQLQuery(hQuery).list()
Any one help me.
Create new Object and save it using session.save() method it will return this object id.
Object object = new Object();
//add object properties
object.setXXX(value);
//now save the object
String id =(String)getDataSession().save(object);
Hope it helps.
Related
I am trying to execute a Stored Procedure which updates a column and retrieves the filename from the same table after updating
StoredProcedure:
CREATE DEFINER=`test`#`%` PROCEDURE `update_count`(
IN in_testID VARCHAR(64),
OUT out_FileName VARCHAR(100),
OUT out_Message VARCHAR(100))
BEGIN
UPDATE files SET count=count+1 WHERE testID=in_testID;
SELECT FileName INTO out_FileName FROM files WHERE testID = in_testID;
SET out_Message = 'File updated uccessfully';
END
JavaCode to execute this StoredProcedure:
Query query = session.createSQLQuery("CALL update_count(:in_testID, #out_FileName, #out_Message)").addEntity(FilesBean.class)
.setParameter("in_testID",body.getTestId());
query.executeUpdate();
Updated the query.executeUpdate() with query.list(). But the line returning a error ResultSet is from UPDATE. No Data
I need to fix this with using the createSQLQuery
The easiest way to do that is return the out parameter as part of the returning parameters (relevant only if you have access to the store procedures).
just add a store procedure like the following one
create procedure myProcedure_only_in_prams (
in in_Id int)
begin
call myProcedure(in_id,#out_Id) ;
select #out_id
END;
after done that it quite simple to use it with Hibernate in the following way
Query query = session.createSQLQuery(
"CALL myProcedure_only_in_parms (:in_Id)")
.setParameter("in_id", 123);
List result = query.list();
The result contains the out parameter, if you want return multiply parameters you can add it by doing select #parm1,#parm2,... ,#parmn
Hope it helped
Is any possibility to execute N1QL update query using spring-data?
I.e. I have following update query:
"UPDATE USERS USE KEYS $id SET location = $location"
I tried to use couchbaseTemplate.queryN1QL method, but it doesn't work.
Is there any solution of this problem using spring data or even native couchbase java SDK?
the CouchbaseTemplate is more tailored towards Spring Data document's use case, so it expects to run queries that return specific elements and will end up unmarshalled into entities (eg. a User object).
if you want to run a more freeform query, like your update, you can always access the native SDK from the template. In your case:
String paStatement = "UPDATE USERS USE KEYS $id SET location = $location";
JsonObject paramValues = JsonObject.create().put("id", theId).put("location", "theLocation");
N1qlQuery query = N1qlQuery.parametrized(paStatement, paramValues);
template.getCouchbaseBucket().query(query);
I think you can add RETURNING * at the end of your UPDATE statement. It would make the statement returns something. It works for me.
I have a hibernate code which insert a new role to the table as follows:
Staff staff = new Staff(staffDTO);
Session session = sessionManager.getSession();
session.beginTransaction();
session.save(staff);
session.getTransaction().commit();
Staff is defined as entity.
My question is that how can I get the newly generated row id by the database?
Many thanks.
Hibernate is smart enough :).
After you save the Object in database If you see ,the object have the generated id. Check it.
After save done, just inspect the object and see.
How to get which properties were updated after hibernate update?
For example if I got
SomeEntity se = new SomeEntity();
getHibernateTemplate().save(se);
//then in some other method
se.setProp1("some new value");
//then in 3th method
getHibernateTemplate().update(se);
If you tell hibernate to do dynamic update it will know witch properties were changed and update only them. Is there a way to get the ones that were changed or to check is specific property was changed?
Ended up doing native sql query to compare the state in the db with the state in the entity before flush the session.
Query query = session.createSQLQuery(
"select t.someProp1 from someTable t where t.id = :entityId")
.setParameter("entityId", entity.getId());
List result = query.list();
I'm using JasperReports engine, and one of the reports gets data from database executing SQL statement. Is there a way to pass parameters to that query?
Thanks in advance!
First, create a new parameter in your report. Then insert the parameter in your query, for example:
SELECT name, department FROM employees WHERE employee_id = $P{employeeId}
Make sure your parameter types matches the data type of the columns in your database. Finally, simply pass your parameters to the JasperReports engine. An example would be:
parameters.put("employeeId", Long.valueOf(14309));
JasperRunManager.runReportToPdf(reportFile, parameters, connection);