Thursday, January 21, 2010

Error Occurs in saving List of beans and Displaying in table

I got a task as follows:
  1. A jsp page with a like of Item Details populated in a table.
  2. User will modify any of the data in any of the column.
  3. After submitting the jsp the updated details needs to be saved in database
The scenario seems to be simple. But the problem is: If we iterate the list in a property of the form,
we are able to display in screen. But the List is set to null if we submit the same form again.
The list iteration is like below:

I have searched in net for the solutions. But I couldn't find any thing. Hence I have to go for a hard solution as below:
  1. Once after the list is displayed in Jsp, in the Onclick of Submit button, I have called a javascript:function.
  2. The list of data displayed is retrieved using the javascript by browsing through the tags inside the jsp.
  3. The retrieved data is converted into a string buffer with "," appended for each column values and ";" appended for each rows.
  4. After creating the string buffer, It is set to a variable in a form( say hiddedDataList).
  5. Finally, the variable with the string is set as hidden variable using html:hidden.
  6. We can again tokenize the string from the variable and create a list and save in the same list variable of form.
The sample code is as follow:

Monday, January 18, 2010

Persisting a BLOB in hibernate mapping

I have faced an error in saving a
record in a table
"ORA-01465: invalid hex number" While using BLOB in hibernate:

I have to convert a data object to a BLOB and save it in a data base.
I was facing an error called "ORA-01465: invalid hex number " whenever i tried to save the data row with the blob data in one of the column.

Error created code piece.:


try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
try {
message = oracle.sql.BLOB.createTemporary(null, false, oracle.sql.BLOB.DURATION_SESSION);
message.setBytes(1, ba.toByteArray());
System.out.println("This is the Blob message " + message);
} catch (SQLException e) {
LOG.error("Error while converting Byte array to BLOB ", e);
}



os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();

}
}

Later I found in one of the site that there is a class called BlobImpl in Hibernate. I used following code to resolve the issue.
Solution:
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(ba);
os.writeObject(object);
os.flush();
// message = oracle.sql.BLOB.empty_lob();
// message.setBytes(1, ba.toByteArray());
BlobImpl blobImpl = new BlobImpl( ba.toByteArray() );
message = blobImpl;
// System.out.println("This is the Blob message " + message);



os.close();
} catch (IOException ioe) {
// ioe.printStackTrace();

}