Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified
Accepted Solution

Question about persistence

Hi.

 

I have a question about the Persistable interface. If I'm correct, inside a Persistable object all primitive types, arrays of primitive types and Vectors containing objects (implementing Persistable) are persisted. What about Vectors or arrays containing non Persistable objects? If these objects's attributes are primitive, will they be persisted or will be their references? And what happens also to objects contained into Persistable collections as BigVector?

I have been re-reading the docs for a while, and I couldn't find if Vector and Hashtable are Persistable by default.

 

Thanks in advance.

 

PD: It seems to me very little natural in Java the fact that an Object doesn't inherit the Persistable interface. Let's say then that we are programming with a modified Java language, don't you think so?

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Question about persistence

everything that should be persisted has to implement the interface. this includes objects that are part of collections.

some classes already implement persistable, see the API of the Persistable interface for a list.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Question about persistence

I've read the API documentation and the development guide, and I think there's nothing related to persistence on primitive types nor Vector or Hashtable. That list only includes RIM's classes, but not java.util. Also in the javadoc of Vector and Hashtable there's nothing  about Persistable because the doc page is the same that Sun's.

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Question about persistence

try it again:

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/util/Persistable.html

especially the part with "All Known Implementing Classes"

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 57
Registered: ‎05-25-2009
My Device: Not Specified

Re: Question about persistence

Give you an example

 

import net.rim.device.api.util.Persistable;


// the class of persistent data
public final class UserAccountStore implements Persistable {
    private long mobileID = -1;
    private String token;
    private long userID = -1;


    public long getUserID() {
        return userID;
    }

    public void setUserID(long userID) {
        this.userID = userID;
    }

    public long getMobileID() {
        return mobileID;
    }

    public void setMobileID(long mobileID) {
        this.mobileID = mobileID;
    }

    public String getToken() {
        return token;
    }

   

	

	public String getCarrierType() {
		return carrierType;
	}

	public void setCarrierType(String carrier) {
		carrierType = carrier;
	}
}

 

 

Developer
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Question about persistence

simon_hain: I've read again (and very carefully) the list of implementing classes and I can't see Vector or Hashtable anywhere.

 

kungfucop: thanks for the snippet, but the question is what happens if inside your Persistable class you have to include a collection. More exactly, if the objects must be stored on RIM's persistable collections (such as BigVector) or it is possible to use java.util's collections instead. In addition, I was wondering if classes like Boolean, Integer, ...can be stored in that kind of collections.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Question about persistence

You are right, Vector and HashTable (and a number of other classes) don't indicate they implement Persistable.  I have raised this using the Submit Feedback button.

 

However you can rest assured that they do.

 

The Persistable implementation also seems to missing from a number of the other standard Objects as well such as String.  In addition, I've never tried to Persist Integer or Boolean or similar objects, I suspect they will Persist just fine, but there doesn't seem to be any documentation of these either.

 

I suggest you use the Submit Feedback (on the OS 5.0 JavaDoc pages) on these to request documentation of this, as I have done.

 

I think Simon has summarized the basic rules though.  Every Object that is persisted must implement Persistable, otherwise an Exception will be thrown when you attempt to persist the Object, or collection containing the Object. 

 

In specific answer to two of your questions:

 

1) "If these object's attributes are primitive, will they be persisted or will be their references?"

I believe what happens is the a serialized for of the Object is persisted, but I fact, you don't really care.  As far as you are concerned, when you persist an Object, the Object and all its primitives, and all Objects it references and all their primitives and all Objects they reference and all their primitives.......... are persisted. 

 

2) "It seems to me very little natural in Java the fact that an Object doesn't inherit the Persistable interface. "

I think you are suggesting that if A is persistable, then an extension of A should also be persistable.  In normal circumstances I would agree, but for Persistable, I disagree.  The current situation, where, by default, extensions are not Persistable seems reasonable to me.  If persistable was inherited, then you would only ever be able to extend Objects in a way that retained this Persistable attribute.

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: Question about persistence

I have an application where I persisted String and Integer, but have found it less trouble to try and stick with storing primitives since no validation is usually needed to determine if an int is really an int for example.

 

 

Developer
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Question about persistence

[ Edited ]

Following your advice, I have raised this question too. Thanks for the explanation.

I had no time to make tests with Vector and Hashtable, but I also think they are persistable. Despite the lack of official info about this, I will accept your post as solution. Anyway, I will keep using RIM's collections (BigVector) instead where possible.

 

I'm not sure about the point that exceptions are always thrown. I remember a project where I tried to persist an array of non persistable objects, and only their references (int) where retrieved. But again, I'm not sure 100 % about this.

 

Point 2 was only a comment, but I think that in order to maintain the consistence of the language, interfaces should be inherited. If not, then this is not true Java (but this is only my humble opinion). There has to be some other mechanism to make this possible.

 

Regards.

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Question about persistence

if you don't see the text i am fine with quoting it:

 

All known implementing classes:

 

AESCryptoToken, AESKey, ARC4Key, ASN1BitSet, AssociatedData, BigIntVector, BigLongVector, BigVector, BiometricData, CAST128CryptoToken, CAST128Key, CertificateExtension, CertificateServerInfo, CertificateServers, CertificateStatus, CMSReceiptData, CodeSigningKey, ControlledAccess, DataBuffer, DESCryptoToken, DESKey, DHKeyPair, DHPrivateKey, DHPublicKey, DSAKeyPair, DSAPrivateKey, DSAPublicKey, ECCryptoSystem, ECCryptoToken, ECKeyPair, ECPrivateKey, ECPublicKey, FingerprintBiometricData, HMACCryptoToken, HMACKey, HttpHeaders, InitializationVector, IntVector, KEACryptoSystem, KEACryptoToken, KEAKeyPair, KEAPrivateKey, KEAPublicKey, KeyPair, LongHashtable, OID, PersistentObject, PersistentStore, RC2CryptoToken, RC2Key, RC5CryptoToken, RC5Key, RSACryptoSystem, RSACryptoToken, RSAKeyPair, RSAPrivateKey, RSAPublicKey, ServiceBook, ServiceBook.ServiceStatus, ServiceRecord, SkipjackCryptoToken, SkipjackKey, SmartCard, SmartCardDSACryptoToken, SmartCardID, SmartCardRSACryptoToken, SparseList, StringMatch, ToIntHashtable, ToLongHashtable, TripleDESCryptoToken, TripleDESKey, WTLSCertificate, WTLSDistinguishedName, X509Certificate, X509DistinguishedName, MultiMap, LongVector, LongStack, LongIntHashtable, IntStack, IntMultiMap, IntLongHashtable, IntIntHashtable, IntHashtable, ContentProtectedVector, ContentProtectedHashtable, ByteVector, HotspotCredentialsAgent

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter