I am attempting to insert a record in an Oracle table with a Function, which would be called through iBatis.NET. Function works as expected in Oracle when called directly.
I have tried using <statement>
and <insert>
SqlMap but I can't get iBatis.NET to call the function, and Oracle doesn't support returning anything from Stored Procedure.
I would need to pass properties of my object as parameters to a function/sproc and get back the ID of this new record.
What would be a good combination of iBatis.NET call / SQLMap / Sproc or Function signature in Oracle?
The documentation only has examples of in-line SQL and I can only use sprocs.
Due to the number of properties in real objects, the hash-map and number of parameters is in the 30+.
Ideally I would be able to do this (doesn't work):
<procedure id="InsertPerson" parameterClass="BOM.Person">
TestDB.PERSON_PKG.InsertPerson(#Nam开发者_StackOverflow中文版e#, #Age#)
</procedure>
Domain object:
public class Person
{
int ID { get; set; }
string Name { get; set; }
decimal Age { get; set; }
}
iBatis.NET call:
int personID = mapper.Insert("InsertPerson", person);
Oracle Stored Procedure:
FUNCTION InsertPerson(
p_Name IN Persons.Name%TYPE,
p_Age IN Persons.Age%TYPE,
) RETURN NUMBER
IS
NEW_ID Persons.ID%TYPE;
BEGIN
SELECT Persons_SEQ.NEXTVAL INTO NEW_ID FROM DUAL; /* Get new ID*/
INSERT INTO Persons(ID, Name, Age)
SELECT NEW_ID, p_Name, p_Age from dual; /* Insert record */
COMMIT;
RETURN NEW_ID;
END;
In case this helps someone else, I was unable to find a workaround to my problem.
I ended up implementing this as a Stored Procedure which takes input parameters for all fields to be inserted in to a table, and one output parameter which returns the unique ID generated by sequence.
After executing mapper.Insert(...)
I simply read the output parameter and return it.
C#:
mapper.BeginTransaction(System.Data.IsolationLevel.Serializable);
// Add new Record
Hashtable param = new Hashtable();
param.Add("ID", user.ID); // Output
param.Add("DeptID", user.DeptID);
param.Add("RightID", user.RightID);
mapper.Insert("AddUserRight", param);
user.ID = Convert.ToInt32(param["ID"]);
MyBATIS map:
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CCP" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<statements>
<procedure id="AddUserRight" parameterMap="AddUserRight-param">
Database.USER_PKG.ADDUSERRIGHT
</procedure>
</statements>
<parameterMaps>
<parameterMap id="AddUserRight-param">
<parameter property="ID" column="ID" direction="Output" />
<parameter property="DeptID" column="DeptID" direction="Input" />
<parameter property="RightID" column="RightID" direction="Input" />
</parameterMap>
</parameterMaps>
</sqlMap>
Sproc (Oracle):
PROCEDURE AddUserRight(
ID OUT USERRIGHTS.USERID%TYPE,
DEPTID IN USERRIGHTS.DEPTID%TYPE,
RIGHTID IN USERRIGHTS.RIGHTID%TYPE)
IS
BEGIN
SELECT USERRIGHTS_UNQ_SEQ.NEXTVAL INTO ID FROM DUAL;
INSERT INTO USERRIGHTS(ID, DEPTID, RIGHTID)
VALUES (ID, DEPTID, RIGHTID);
END;
精彩评论