2 Replies Latest reply: May 5, 2010 7:56 AM by 843790 RSS

    How to write a  java.beans.PersistenceDelegate?

      I haven't found any Information about this topic, also [Using XMLEncoder|http://java.sun.com/products/jfc/tsc/articles/persistence4/] doesn't explains it. It seems that some Information was added, since I had last read it.

      The api-doc isn't very helpful too. Also the source-code of DefaultPersistenceDelegate and PersistenceDelegate wasn't quite useful. Also the implemation of MetaData didn't helped much.

      I have three reason why I would like to know how to do this.

      1) The database id attribute isn't not stored, even getter and setters are in place and different from the default value of null.
      Is "id" a reserved value? One of 30 classes does it, so normally it can be. Burt I was unable to figure out, what kind of difference caused this different behaviour.

      So perhaps writing a PD could solve this or at least it would gives me a hint, what's wrong. OK, I could add some debug infos to the DPD and repack it to to rt.jar, but that's quite time consuming.

      2) The Hibernate implementations of List,Map, Set doesn't work with XML-Encoder. Also EnumMap dont works. Even setting the ListPD for that subclasses don't work. So I have to copy the content into subclasses of java.util. Works,but is quite complicated and error prone.

      3) I want to change the way of writing byte arrays. E. g. below.
      At the moment I write the files per hand.
      Which is also is quite complicated and error prone, cause I have to remove them form the component tree and have to restore the refs at the time I'm loading the file. The default method causes an OutOfMemory-Exception, cause of writing every single byte to xml.

      So I need more information on how to implement:
      protected void initialize(Class type,
                                Object oldInstance,
                                Object newInstance,
                                Encoder out)
      protected abstract Expression instantiate(Object oldInstance,
                                                Encoder out)
      As far as I have understood. "initialize" is called if mutatesTo returns false. Otherwise "instantiate" is called.
      It seems that I have to use java.beans.Expression, but how?

      Class for PDF files.
      public class PdfFile implements SupportsDatabaseId, MixInCloneable<PdfFile> {
          private Long id;
          private String filename;
          private String comment;
          private byte[] pdf;
      Current Workaround
         final BASE64Encoder base64enc = new sun.misc.BASE64Encoder();
      I would be quite lucky, if you can give an example how to do that. Especially for the PdfFile would be nice.

      Thanks a lot.

      Greetings Michael

      Edited by: Urmech on May 5, 2010 4:58 AM

      Edited by: Urmech on May 5, 2010 5:00 AM

      Edited by: Urmech on May 5, 2010 5:05 AM
        • 1. Re: How to write a  java.beans.PersistenceDelegate?
          You probably don't need to write a PersistenceDelegate. Just provide a no-args constructor, and getters and setters for all the attributes you want to serialize. Or else extend DefaultPersistenceDelegate, provide only getters, and a constructor that takes all those values as arguments.
          • 2. Re: How to write a  java.beans.PersistenceDelegate?
            H thanks, for the reply, but I using XMLEncoder for a long time and I'm quite familiar with these assumptions.

            I already checked, everything for problem 1. But even if this problem could be solved without a own PD, the other two problems still needs one. For classes without Default-constructor I have own PD (see "Constructors whose arguments are properties").

            For the IDs I'm using this interface implemented by all classes which are using an id. Also the one class, which writes the ID.
            public interface SupportsDatabaseId extends Serializable {
                public Serializable getId();
            Cause of the id problem I tried.
            public interface SupportsDatabaseLongId extends SupportsDatabaseId {
                public Long getId();
                public void setId(final Long id);
            but I'm getting still the same effect. (Two classes are having Natural ID's, That's the Reason which the first interface looks so. All classes are implementing the setId method, regardless of the type of interface.

            For Problem 3 I don't know how XMLEncoder can decode a Base64 encoded String to a byte-Array, unless the PD uses any special tags.Converting it by the get and set method isn't possible, cause raw data should be store in the database.

            Especially "Initialization without assumptions" would be interesting, cause not writing the default values is a big source for errors.


            Greetings Michael

            Edited by: Urmech on May 5, 2010 5:53 AM

            Edited by: Urmech on May 5, 2010 5:56 AM