2

How to create jaxb Object to Clob. When i tried the following not serializable error coming.

public static void createClob(TestTo testTo){
        PreparedStatement pst  = null;
        Connection con = null;
        //Clob studentListClob = null;                  

        try {
            con = openOASDBcon(false);
            pst  = con.prepareCall(INSERT_Clob);
            pst.setBytes(1, getByteArrayObject(testTo));
            pst.setString(2, "");
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(con, pst);
        }
    }


private static byte[] getByteArrayObject(TestTo testTo){

           byte[] byteArrayObject = null;
           try {

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(testTo);

                oos.close();
                bos.close();
                byteArrayObject = bos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
                return byteArrayObject;
            }
            return byteArrayObject;
        }

It's not possible to implement serializable. Is there any best way to implement jaxb object to clob.

jackyesind
  • 3,343
  • 14
  • 47
  • 74
  • It seems like you may be asking how to get JAXB to create Serializable classes. If this is the case, see http://stackoverflow.com/questions/1513972/how-to-generate-a-java-class-which-implements-serializable-interface-from-xsd-us – Rob Feb 17 '14 at 15:15
  • Not only serializable. I want to convert the jaxb object to clob. Is any way to do? – jackyesind Feb 17 '14 at 17:33
  • @Rob. Is there any way to do – jackyesind Feb 18 '14 at 03:45

1 Answers1

0

There are a couple of ways to approach sticking a JAXB object into a CLOB (or BLOB) column in a database. I think that you will have to do some custom insert and select logic to put the JAXB object into a column - the normal ORM model would be a row per object, with a column per field of the object (e.g., if you were using hibernate).

Option 1: Configure xjc to generate Serializable JAXB classes (How to generate a Java class which implements Serializable interface from xsd using JAXB?). Then use the Serializable interface to read/write the object from the clob/blob's input/output streams. This stores the Java object representation in the database.

Option 2: Use the JAXB-supplied XML marshal/unmarshal path to read/write the state of the object as XML text. This stores the XML representation of the object in the database.

I personally would go with Option 2 - the XML representation. I think it is easier to manage changes in the XSD and make sure you can read old objects, rather than having to deal with Java's serial version ID. In addition, you might want to consider compressing the XML before putting it in the CLOB (see Compressing and decompressing streams).

Community
  • 1
  • 1
Rob
  • 6,247
  • 2
  • 25
  • 33