From: rakesh at rakeshv.org (Rakesh Vidyadharan) Date: Mon, 17 Oct 2005 22:05:30 -0500 Subject: [Pljava-dev] Passing custom java object to PL/Java In-Reply-To: <43541B61.8040400@click.cz> References: <1B4E0340-B59B-4152-B6AF-3862B5EF1845@rakeshv.org> <43541B61.8040400@click.cz> Message-ID: <127953D5-2CBD-4DFE-85CB-DEF2B131900B@rakeshv.org> On 17 Oct 2005, at 16:45, Petr Mich?lek wrote: > Hi! > > I don't know, if I'm right, but try to serialize object Recurrence > like this: > > CLIENT SIDE: > ============ > class Recurrence implements Serializable //...should be enough > { > ... > } > > Recurrence rec = ....; > ByteArrayOutputStream baos = new ...; > ObjectOutputStream oos = new ObjectOutputStream( baos ); > oos.writeObject( rec ); > oos.close(); > > byte[] value = baos.toByteArray(); > statement.setBinaryStream( i++, new ByteArrayInputStream > (value),value.length); // Try use type BLOB! > // I don't know if this is most effective methond, how to pass BLOB > argument to JDBC statement... > > > SERVER SIDE: > ============ > .... > Recurrence rec = (Recurrence)new ObjectInputStream > ( byteArrayInputStream ).readObject(); > > > Try it and tell us if such method works! > > If it throws SerializebleException, try to pass simpler argument > like string and so on. > > Regards > Petr Michalek Thanks Petr for your suggestion. It turned out to be unnecessary. I was indeed thinking of trying raw serialization or using something like XStream to convert the object into an XML representation and parsing it back on the server (less efficient but easier to debug). It turns out that the first problem with my test program was that I was specifying the type as "testType" and not "testtype", to which Postgres converted the name in the typname column of pg_catalog.pg_type. Once I fixed that issue, I was able to send the object through to the server. The server "nicely" returned an error message saying it could not find the appropriate method: java.sql.SQLException: ERROR: Unable to find static method RecurrenceTest.test with signature (Ljava/sql/ResultSet;)I That was all the information I needed :-) I just updated my static method to take as parameter a ResultSet, and then just walked the ResultSet to get the values I had set. Ability to use ResultSetMetaData would have been nice, but since I know the structure of the data I am passing I can easily work without it. I have updated the source files at the URL's I posted initially with the working code in case anyone is interested. I only wish it had been this easy to process the result of a setof returned by a PL/Java function, instead of having to parse the delimited fields out of a PGObject.getValue() string. Thanks Thomas and Petr once again for your responses. Rakesh