How to call storedproc using openJPA and map to existing entity - stored-procedures

I am trying to figure out how to call a stored procedure using openJPA
How do I do that? I assume it is the same as calling a namedQuery, but I can't find anywhere online where to do this.
I can't find one tutorial.
Also, how do you map it to an existing entity? Just have a ("nameOfStoredProc", NameOfEntity.class)?

From the OpenJPA user manual.... Creating SQL Queries
In addition to SELECT statements, OpenJPA supports stored procedure
invocations as SQL queries. OpenJPA will assume any SQL that does not
begin with the SELECT keyword (ignoring case) is a stored procedure
call, and invoke it as such at the JDBC level.
EntityManager em = ...;
Query query = em.createNativeQuery("StoredProcName", Magazine.class);
processMagazines(query.getResultList());

Related

How to create a strong typed sql statement returning multiple result sets

Currently I am using a stored procedure returning multiple result sets. I set up a test and found that the result of the stored procedure can efficiently be read by using the "ExecuteReader" method of the DataConnection class and then Executing or Querying the DataReader.
The problem with this approach is that there is no tight coupling between Code and Stored Procedure. So I would like to replace the Stored Procedure by a Linq2db expression that produces a sql statement with multiple select statements. The ExecuteReader method is expecting a string sql parameter. How can the Sql string be constructed based on a DataConnection object that has knowledge of my Database schema. Is this feature somehow implemented in linq2Db.

FireDAC Add new field to existing SQL table

I am trying to create a new field in a FireDAC-compatible database with this PROCEDURE:
PROCEDURE CreateField(Connection : TFDConnection ; CONST TableName : STRING; F : TFieldDefinition);
VAR
Table : TFDTable;
BEGIN
Table:=TFDTable.Create(NIL);
TRY
Table.Connection:=Connection;
Table.TableName:=TableName;
Table.FieldDefs.Updated:=FALSE;
Table.FieldDefs.Update;
Table.FieldDefs.Add(F.FieldName,F.FieldType,F.MaxLen,NOT F.Nullable);
// Commit my changes to the database //
FINALLY
FreeAndNIL(Table)
END
END;
where
TYPE
TFieldDefinition = CLASS
PUBLIC
FieldName : STRING;
FieldType : TFieldType;
MaxLen : Integer;
Nullable : BOOLEAN;
END;
but I can't seem to "Commit" my changes back to the database (ie. end up executing an ALTER TABLE ADD [COLUMN] statement).
How do I commit my changes to the FieldDefs list of the table? Or is there some other way - using plain FireDAC - that I can use to create a new field in an existing table?
Note: There are already data in the table, so I can't simply "DROP" and then "CREATE" the table again.
FireDAC should understand the sytax used by the supported databases and use the appropriate syntax and decoration. See the documentation here: http://docwiki.embarcadero.com/RADStudio/Sydney/en/Preprocessing_Command_Text_(FireDAC)
TFieldDef is an internal descriptor of the fields in a TDataSet, it's not going to be used to update the table structure automatically. (Although you could write your own procedure that compares your TFieldDefs to the FireDAC MEta Data and creates the DDL (Data Definition Language) statements you need to execute in a TFDCommand ... )
To alter that table structure you will need to provide the DDL (SQL) statement that you execute with TFDCommand - the 'preprocessing' link above will explain how to write it in a dialect abstracted way.
If you use the appropriate FireDAC description it will automatically put in the appropriate SQL decoration for you so the syntax is valid. Depending on your SQL dialect and the FireDAC driver you may hit limitations. (For example using the ODBC driver FireDAC generally won't know the specific details of the underlying database - we had to implement a solution for SAP HANA which had exactly this challenge).
Bear in mind that some SQL dialects support features that others don't - so for example it's not safe to assume that you can position a column when you add it (which MySQL supports for example) as not all dialects allow that in an ALTER TABLE statement.

Avoiding hard coding Schema in DB2/400 Stored Procedure

I'm creating Stored Procedures to replace Legacy app programs for an IBM i. I'm calling the stored procedure from a Java Web App. I'm using the jt400 JDBC driver
My JDBC URL is jdbc:as400://myhost/;libraries=*LIBL,MYLIB;prompt=false
The stored procedures can call stored procedures
The initial stored procedure call completes normally if it does not make further stored procedure calls
If the stored procedure makes a call to other stored procedures it fails with
com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0204] MY_SP in MYLIB type *N not found.
If I hard code a schema in the stored procedure call statement, the call completes normally.
I want to have the called stored procedures use the same schema as the caller
You need to SET PATH = "MYLIB"
When I am using SQuirreL to call a stored procedure, I need to use the SET PATH statement to get it to find the stored procedure. I don't know if that is because my library list is bad or what, but the current schema is not used to find an unqualified stored procedure.
I actually had this same problem, the stored procedure uses your job description library list. You need to edit that you can use TAATOOL CHGLBLJOBD. I am not in front of an iSeries at the moment but I believe the command was either EDTJOBDLIB or EDTJOBDLIBL WRKJOBDLIBL. It is some variation of that.

derby procedure that does not modify

I'm trying to create a trigger on derby which simply calls a procedure. The stored procedure does not change anything and gets no parameters. It simply check that the time is within an interval (for example between 08:00 and 16:00). On creation of trigger i receive the following error:
"42Z9D: Procedures that modify SQL data are not allowed in BEFORE triggers."
But the procedure makes no changes.
When defining a procedure one should specify if the procedure modifies data or not. If it executes any sql or not. As mentioned in the link provided above by Bryan I should use one the options:
{ NO SQL | MODIFIES SQL DATA | CONTAINS SQL | READS SQL DATA }
If you dont use this options the default value will be assumed that is CONTAINS SQL.

Multiple select statements in a stored procedure for use in classic ASP

Currently I have a classic asp application, and one of the page has logic like this:
SELECT * FROM Table1 WHERE Condition
//DO stuff
SELECT * FROM Table2 WHERE Condition
//DO stuff
SELECT * FROM Table3 WHERE Condition
//Execute
I need to convert this into a stored procedure. Now I know how to use stored procedures in ASP generally, but I don't know how to do it with multiple selects. How can I generate three sets of data and use them properly?
Thanks.
Are you using ADO or ADO.Net ? Both have the capability to return multiple recordsets from a stored procedure. In Ado, if a stored procedure has multiple Selects in it, you can access the second, and subsequent Resultsets (ADO RecordSets) in your client code by calling Recordset.NextRecordSet.
In ADO.Net, When a Stored proc has multiple Selects, ADO.Net will put the results of each Select into a DataTable object, and put each DataTable into the Tables collection of the DataSet object which returned by the ADO.Net command.Execute() method.

Resources