Is it possible to convert CLOB data to String in Mybatis sql mapper xml? - spring

I want to receive CLOB data of CONTENT column as java.lang.String value with only sql mapper definition.
Select query is something like this.
<select id="selectById">
<![CDATA[
SELECT
ID -- // NUMBER
, TITLE -- // VARCHAR2(2000)
, CONTENT -- // CLOB
FROM FAQ
WHERE ID = #{id}
]]>
</select>
Is it possible? I've googled over an hour and realized <resultMap /> can be the solution.
But, I can't figure out any more. Please help me.

I'll show you two approaches.
The first solution is to use <resultMap />.
You just need to specify javaType for the CONTENT column.
<resultMap type="map" id="faqRM">
<id property="ID" column="ID" />
<result property="TITLE" column="TITLE" />
<result property="CONTENT" column="CONTENT" javaType="string" />
</resultMap>
and specify resultMap in the <select />.
<select id="selectById" resultMap="faqRM">
The second approach is to specify the type handler for CLOB vs Object conversion globally in the config.
<typeHandlers>
<typeHandler javaType="java.lang.Object" jdbcType="CLOB"
handler="org.apache.ibatis.type.StringTypeHandler" />
</typeHandlers>
Then you can simply specify resultType="map" instead of defining <resultMap />.
<select id="selectById" resultType="map">
I should also mention that java.lang.String has smaller capacity than CLOB (2GB - 1 vs 4GB - 1 last time I checked).

Related

segment terminator in BeanIO

I'm interested in the recordTerminator parser property of BeanIO. Does it apply to segments too, like "segmentTerminator"? Namely, I have a stream of fixedlength format, containing of a single record with repeatable segments, and all stream is a single line. Hence, I have set recordTerminator="", but it still gives me
==> Invalid 'state': Expected minimum 1 occurrences
==> Invalid 'city': Expected minimum 1 occurrences
==> Invalid 'street': Invalid field length, expected 35 characters
==> Invalid 'zip': Expected minimum 1 occurrences
It doesn't complain about fields that precede to repeatable segment, and complaints about the fields in a repeatable segment are out of order defined in mapping.xml, that looks like this:
<beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
<stream name="employeeFile" format="fixedlength">
<parser>
<property name="recordTerminator" value="" />
</parser>
<record name="employee" class="example.Employee">
<field name="firstName" length="35" />
<field name="lastName" length="35" />
<field name="title" length="35" />
<field name="salary" length="35" />
<segment name="addressList" collection="list" minOccurs="1" maxOccurs="unbounded" class="example.Address">
<field name="street" length="35" />
<field name="city" length="35" />
<field name="state" length="35" />
<field name="zip" length="10" />
</segment>
</record>
</stream>
</beanio>
Class implementations is like this:
package example;
public class Employee {
String firstName;
String lastName;
String title;
String salary;
List<Address> addressList;
// getters and setters not shown...
}
package example;
public class Address {
private String street;
private String city;
private String state;
private String zip;
// getters and setters not shown...
}
If I remove all preceding fields to repetitive segment both from mapping.xml and the input string, remaining string is properly unmarshalled, and marshalled to json afterwards, I even didn't change implementation of java classes, so the preceding fields stay uninitialized, as expected, but properly printed out after marshalling. Where did I go wrong?
OK, my camel code is in spring xml, looks like this:
<route id="simple-route">
<!-- from id="request-file" uri="file://C:/mqdocuments/?fileName=response464.txt"/-->
<from id="request-file" uri="file://C:/mqdocuments/?fileName=request464.txt"/>
<log id="route-log-request" message="request: ${body}"/>
<setHeader headerName="CamelJmsDestinationName" id="_setHeader1">
<constant>queue://QM_TEST/INPUTQ?targetClient=1</constant>
</setHeader>
<to id="_to1" pattern="InOut" uri="websphere:queue:SYSTEM.DEFAULT.LOCAL.QUEUE?useMessageIDAsCorrelationID=true&replyTo=REPLYQ"/>
<log id="route-log-response" message="response: ${body}"/>
<transform>
<simple>${body}\0</simple>
</transform>
<unmarshal ref="parseTransactions464"/>
<marshal ref="jack"/>
<log id="route-log-json" message="jackson: ${body}"/>
</route>
So basically, when I uncomment input from file, in which the reponse is saved, and place in comment mq to endpoint, unmarshalling is OK, but if I put a request to a queue, and get response, then I hope to rectify the problem by a transform that simply adds EOF character, because without it, it gives me error that I reported in the first place.
And transform doesn't help, because I don't know how to write EOF (ascii 26), but even if I figure that out, I'm not sure it will help.
I'm going to attempt this as an answer, unfortunately I can't test this, I have nothing setup for use with Camel. First I would not change the default recordTerminator value for BeanIO and leave it to use the default which is any of CR, LF or CRLF.
Then in the transformation of the message, I'll append a newline (\n) instead of the \0. I don't quite understand why you would want to use the EOF character if you have control over it. Instead of:
<transform>
<simple>${body}\0</simple>
</transform>
I would go for:
<transform>
<simple>${body}\n</simple>
</transform>
See the section on "Using New Lines or Tabs in XML DSLs" close to the bottom of the page:
Using New Lines or Tabs in XML DSLs
Available from Camel 2.9.3
From Camel 2.9.3: it is easier to specify new lines or tabs in XML
DSLs as you can escape the value now xml
<transform> <simple>The following text\nis on a new line</simple></transform>
I was floating around trying to identify the problem, but in the end, I realized I should have set the charset with the encoding attribute of beanio dataFormat, which I couldn't do because of this defect:
http://camel.465427.n5.nabble.com/Re-Exhausted-after-delivery-attempt-1-caught-java-lang-NullPointerException-charset-tc5817807.html
http://camel.465427.n5.nabble.com/Exhausted-after-delivery-attempt-1-caught-java-lang-NullPointerException-charset-tc5817815.html
https://issues.apache.org/jira/browse/CAMEL-12284
Finally, I was instructed by Claus Ibsen to use such workaround:
<bean class="org.apache.camel.dataformat.beanio.BeanIODataFormat"
id="some_bean_id">
<property name="encoding" value="UTF-8"/>
<property name="mapping" value="mapping.xml"/>
<property name="streamName" value="some_stream_name"/>
</bean>

AppleScript error for property, not for element

When I make the following AppleScript call to my app, I get a list of text objects:
tell "MyApp"
name of every myobject
end tell
But the following call returns an error:
tell "MyApp"
name of selected myobjects
end tell
Script Error
MyApp got an error: Can't make name of selected MyObjects into type specifier.
I turned on verbose logging with the -NSScriptingDebugLogLevel 1 argument to my app, which gave me this:
2015-01-27 14:33:53.650 MyApp[4848:2572448] Error converting apple event to script command: -1700
2015-01-27 14:33:53.650 MyApp[4848:2572448] Original event: <NSAppleEventDescriptor: 'core'\'getd'{ '----':'obj '{ 'form':'prop', 'want':'prop', 'seld':'pnam', 'from':'obj '{ 'form':'prop', 'want':'prop', 'seld':'MASM', 'from':null() } }, &'csig':65536 }>
2015-01-27 14:33:53.650 MyApp[4848:2572448] Offending object descriptor: <NSAppleEventDescriptor: 'obj '{ 'form':'prop', 'want':'prop', 'seld':'pnam', 'from':'obj '{ 'form':'prop', 'want':'prop', 'seld':'MASM', 'from':null() } }>
2015-01-27 14:33:53.651 MyApp[4848:2572448] Expected type descriptor: <NSAppleEventDescriptor: 'obj '>
This is the "application" section of my sdef file:
<class name="application" code="capp" plural="applications" inherits="application">
<cocoa class="MyApp.MAApplication" />
<element type="myobject" access="r">
<cocoa key="myobjects" />
</element>
<property name="selected myobjects" code="MASM">
<cocoa key="selectedMyObjects" />
<type type="myobject" list="yes" />
</property>
</class>
And the MyObject class:
<class name="myobject" code="MAMO" inherits="item" plural="myobjects">
<cocoa class="MyObject" />
<property name="id" code="ID " type="text" access="r">
<cocoa key="permanentID" />
</property>
<property name="name" code="pnam" type="text" access="r">
<cocoa key="displayName" />
</property>
</class>
When I just call selected myobjects, I get back a list of identifiers, so that part is working correctly. When can't I get the name or other properties back from that list, when I can with another list specified the exact same way (except as an element instead of property)?
Update
Still not having any luck so far. I noticed, with verbose logging, that when I call name of every MyObject, the command "Intrinsics.get" triggers with an NSPropertySpecifier "displayName of myobjects". Apparently that link doesn't get made when using a property rather than an element to return a list of items. I found a workaround, though, since I specify an is selected property on MyObject. You can call:
name of every myobject whose is selected = true
That works the way I expected my selected myobjects property to work, returning a list of text objects.
Update 2
I added a sample project to GitHub:
https://github.com/abbeycode/AppleScriptObjectList
That contains a sample script with the first two methods that behave as expected, followed by the one that triggers the error. Feel free to mess around if you'd like. I submitted a radar with this sample project.

Reusing MyBatis ResultMap in multiple mapper.xml

I would like to re-use a specific from different *Mapper.xml files which all somehow read same objects.
I have a Database table called Project, which I created the following resultMap for:
<resultMap id="ProjectMap" type="com.model.Project">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="client_prj_no" jdbcType="VARCHAR" property="clientPrjNo" />
<result column="notes" jdbcType="VARCHAR" property="notes" />
<result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
...
<resultMap>
It works great in the ProjectMapper.xml, however, now I want to create a ClientWithProjectsMapper.xml where I want to SELECT * FROM CLIENT, PROJECT where PROJECT.CLIENT_ID = CLIENT.ID and have a Client object return with a List objects. In other words, I want to get a ClientWithProjects with a single SQL.
In my mapping, I want to reuse the ProjectMap (without copy/paste) which I defined in the ProjectMapper.xml, but I am not sure how to accomplish this.
I could factor out the ProjectMap into a separate file, but I have not found any facilities in MyBatis to #include other files.
Any ideas on how this can be done? (I am using Maven, are there any plugins that would filter the files looking for #include or such, and include the contents of the file right into file being processed?).
Thanks.
-AP_
Once you import all the mapper xmls in mybatis-config.xml file you can refer ResultMaps configured in any of the mapper xmls using resultMap id along with namespace.
for Ex:
ProjectMapper.xml
<mapper namespace="com.mybatisapp.mappers.ProjectMapper">
<resultMap id="ProjectMap" type="com.model.Project">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<resultMap>
</mapper>
In ClientWithProjectsMapper.xml
<select id="selectProjectsByClient" parameterType="int"
resultMap="com.mybatisapp.mappers.ProjectMapper.ProjectMap">
select * from blahblah
</select>
Here you can reference resultMap in another Mapper xml files using fully qualified name as "com.mybatisapp.mappers.ProjectMapper.ProjectMap"

NHibernate 3.3: <sql-insert> executing an Oracle 11g stored procedure

I'm mapping an Oracle 11g stored procedure as command in a NHibernate's .hbm.xml mapping file, like this:
<class name="Person" table="PERSONS">
<id name="Id" column="COD_PERSON" />
<property name="Name" column="NAME" />
<property name="AuditField1" column="AUDITFIELD1" />
<property name="AuditField2" column="AUDITFIELD2" />
<property name="AuditField3" column="AUDITFIELD3" />
<sql-insert>exec PKG_PERSONS.insert_sp ?,?</sql-insert>
</class>
This is the stored procedure:
create or replace package body PKG_PERSONS is
procedure insert_sp(pcod_person persons.cod_person%type,
pname persons.name%type) is
begin
insert into persons(cod_person, name) values(pcod_person, pname);
end;
From this mapping, I'm expecting 'Id' and 'Name' properties would be send as parameters, but for sure, this isn't happening; I'm getting this error from Oracle: ORA-01036: illegal variable name/number. NHibernate shows some logs in Console window and it seems like NH tries to map all properties, included AuditFields, to invoke the stored procedure.
Maybe is this the source's error?
Is this the expected behaviour?
Is it possible to specific what properties send as parameter with NH3/Oracle?
Thanks in advance.
Confirmed, NH tries to send as parameter every entity's properties mapped to <sql-insert>/<sql-update> stored procedures, but you could make exceptions with insert/update=false.
Another issue was the right sintaxis to execute Oracle11g's stored procedure, this is the mapping that works for me with NH3.3 and Oracle.DataAccess 4.112.2.0:
<class name="Person" table="PERSONS">
<id name="Id" column="COD_PERSON" />
<property name="Name" column="NAME" />
<property name="AuditField1" column="AUDITFIELD1" insert="false" update="false" />
<property name="AuditField2" column="AUDITFIELD2" insert="false" update="false" />
<property name="AuditField3" column="AUDITFIELD3" insert="false" update="false" />
<sql-insert check="none">begin PKG_PERSONS.insert_sp(:p0,:p1); end;</sql-insert>
</class>
I had to redefine the parameter's order, because Id property is send at last position:
create or replace package body PKG_PERSONS is
procedure insert_sp(pname persons.name%type,
pcod_person persons.cod_person%type
) is
begin
insert into persons(cod_person, name) values(pcod_person, pname);
end;
Maybe this will help somebody else,
Thanks.

Missing schema in DBML if using LINQ to SP and Sp returning multiple record sets

while making of POC of LINQ to SQL and entities, i faced a problem stuck in a frozen dead end.
Problem is , am using LINQ to SP and every things was working fine and i made cool methods of editing, adding and deleting. Then some thing click in my mine that "what if i just return two record set from SP". i made a SP and returned two record set from it
SP look like this [Demo]
Create PROCEDURE [dbo].GetUserData
#UserId Bigint
AS
BEGIN
SET NOCOUNT ON;
-- Getting User
select * from [User] where id=#UserId
-- Getting User's role
select * from [Role] where userId=#UserId
end
then i droped that SP in my DBML (Linq to SQL classes) then here i noticed that only schema of one record set was created like this
<?xml version="1.0" encoding="utf-8"?>
<Database Name="MyInventory" Class="MyDBMLDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
<Connection Mode="AppSettings" ConnectionString="Data Source=MyDatabaseServer\;Initial Catalog=MyInventory;Integrated Security=True" SettingsObjectName="ConsoleApplication16.Properties.Settings" SettingsPropertyName="MyInventoryConnectionString" Provider="System.Data.SqlClient" />
<Function Name="dbo.GetUserData" Method="GetUserData">
<Parameter Name="UserId" Parameter="userId" Type="System.Int64" DbType="BigInt" />
<ElementType Name="GetUserDataResult">
<Column Name="Id" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
<Column Name="Name" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
<Column Name="Email" Type="System.String" DbType="NVarChar(50)" CanBeNull="true" />
<Column Name="IsDeleted" Type="System.Boolean" DbType="Bit NOT NULL" CanBeNull="false" />
<Column Name="HomePage" Type="System.String" DbType="VarChar(100)" CanBeNull="true" />
</ElementType>
</Function>
</Database>
i can clearly see that only one record set is created of Users records and it is missing Role schema :(.
Can any body tell me what and why is that so?
Thanks
Lura
I have had to deal with something similar in getting multiple data sets from a data base for a website. What we did was create an DatabaseExtensions.cs file to add the queries with multiple data sets.
So in the extensions file we would have something like this
public partial class DataBaseDataContext
{
[ResultType(typeof(FirstResult))]
[ResultType(typeof(SecondResult))]
[Function(Name = "dbo.StoredProc")]
public IMultipleResults StoredProc([global::System.Data.Linq.Mapping.ParameterAttribute(DbType = "Int")] System.Nullable<System.Int> ID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), ID);
return ((IMultipleResults)(result.ReturnValue));
}
}
public class FirstResult;
public class SecondResult;
Note: I changed some of the names of things in this code to make it easier to read, so it may not work as is.
FirstResult and SecondResult are the result type classes. I would usually copy them from the dbml's accompanying .cs file then rename it. I didn't include their code here because it can be rather long.
DataBaseDataContext dataCon = new DataBaseDataContext();
var results = dataCon.StoredProc(id);
var firstSet = results.GetResult<FirstResult>();
var secondSet = results.GetResult<SecondResult>();
//process data
It is important to get your results out in the same order they come out in your stored procedure. After you have gotten your results out, you can use LINQ or whatever to work with them.
My finding is that if you add a result set to your dbml manually you get the results that you want but LinqToSql doesn't offer you a simple way to have access to them
Here is a path to your results
((System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderSession
<System.Data.SqlClient.SqlDataReader>)
(((System.Data.Linq.SqlClient.SqlProvider.ExecuteResult)(result)).session)).buffer
you here have access to your result by index buffer[0] will return the results of your first select statement and buffer[1] returns the result of second select.
probably you can cast this two IEnumerables to typed IEnumerable<GetAllResult> and IEnumerable<GetAllResult1> although I didn't test that.
I build this sample dbml
<Function Name="dbo.GetAll" Method="GetAll">
<ElementType Name="GetAllResult">
<Column Name="ID" Type="System.Int32" DbType="Int" CanBeNull="true" />
<Column Name="Tagname" Type="System.String" DbType="NChar(10)" CanBeNull="true" />
</ElementType>
<ElementType Name="GetAllResult1">
<Column Name="Id" Type="System.Int32" DbType="Int" CanBeNull="true" />
<Column Name="TagId" Type="System.Int32" DbType="Int" CanBeNull="true" />
<Column Name="Name1" Type="System.String" DbType="NChar(10)" CanBeNull="true" />
</ElementType>
</Function>
and the generated cs file would be like this
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetAll")]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(GetAllResult))]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(GetAllResult1))]
public IMultipleResults GetAll()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((IMultipleResults)(result.ReturnValue));
}
Have you looked code generated for your model? Two resultsets will be here. Mapping can lie.
Ramesh

Resources