2

My Java bean looks like below public class AuditRptDTO {

private Date timestamp;
private String userId;
private String stName;
private String userType;
private String code;
private Clob oldRecord;
private Clob newRecord;
private String serverIp;

} here in place of oldRecord and newRecord i need to save another bean object. how can i achieve this in hibernate using an XML mapping.

2
  • There are two things here, saving days with Hibernate xml mappings and saving an object in a clob. Both are perfectly possible. Which one is causing you a problem? Commented Oct 30, 2017 at 8:34
  • how can i convert java object into a CLOB type ? Commented Oct 31, 2017 at 8:06

1 Answer 1

1

A Simple approach is:

serialize your Object:

ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ObjectOutput out  = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();

byte[] dataArray = bos.toByteArray();

Base 64 encode the data to get a String.

String dataString = Base64.encodeBase64String(dataArray);

Then all that s needed is to convert the String to an instance of the Clob interface, you could create your own or use an existing one.

Clob clobData = SerialClob(dataString.toCharArray());

There may be a simpler way to instantiate your Clob object, but you will definitely need to serialize your object first. I will leave t to you to see if you can improve on this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Martin for your comment on it.
During retrieval of Clob object from DB and then when we deserialize do we need to have the type of object(class).? or This approach doesn't require to worry about actual object type while getting the data back from DB.
You will need the type in scope, but you don't have to specify it while you deserialze it. It's probably worth reading up about java serialisation, there are a few things which you may need to be aware of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.