Wednesday, 6 December 2023

java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected - got CLOB

Case:
Case 1:
i had this Error when executing a VO that have a CLOB column

Case 2:
i had another case where there was a CLOB column in the database and it was set in the Entity as oracle.jbo.domain.ClobDomain

i retrieved this column through a ROV and set it in a PageFlowScope Object and showed that PageFlowScope Object on the screen



Problem:

For Case 1:
i was getting ORA-00932 error when executing a Query that have a CLOB column

For Case 2:
the problem was that after running or deploying the application on the server, the CLOB field was showing correctly right after running or deployment, but after 2 or 3 hours, the same field showing the CLOB didnot show any data, but this case did not throw any exceptions in the log.


Solution:

the attribute in the entity/view should be defined this way:

<Attribute
Name="YOUR_ATTRIBUTE_NAME_IN_VO"
IsQueriable="false"
ColumnName="YOUR_COLUMN_NAME_IN_TABLE"
SQLType="CLOB"
Type="java.lang.String"
ColumnType="CLOB"
TableName="YOUR_TABLE_NAME"/>



and in the RowImpl the attribute should be a String


it should also be noted that if you know that your CLOB will hold characters more than 4000, then you will have to split your CLOB into parts to be able to show all data inside it as shown below;

we will use the DBMS_LOB.SUBSTR which is used for taking a part of the CLOB to avoid having more than 4000 characters.

dbms_lob.substr( clob_column, for_how_many_bytes, from_which_byte );

we alos use DBMS_LOB.GETLENGTH to get the length of the CLOB to make sure we get the data correct.

for more info about these functions check these links 

DBMS_LOB Oracle Doc


DBMS_LOB.SUBSTR (yout_column_name, 4000, 1) AS yout_column_name_pt1 

      ,CASE WHEN dbms_lob.getlength (yout_column_name) > 4000  THEN DBMS_LOB.SUBSTR (yout_column_name, 4000, 4001) END AS yout_column_name_pt2 

       ,CASE WHEN dbms_lob.getlength (yout_column_name) > 8000  THEN DBMS_LOB.SUBSTR (yout_column_name, 4000, 8001) END AS yout_column_name_pt3 

       ,CASE WHEN dbms_lob.getlength (yout_column_name) > 12000 THEN DBMS_LOB.SUBSTR (yout_column_name, 4000, 12001) END AS yout_column_name_pt4