问题描述:

I'm getting a ClassCastException (java.lang.Double cannot be cast to java.lang.Long) when retrieving multiple instances of a class in the datastore. The class has many values that are doubles and many that are longs. I'd like to view what is in the datastore and compare with the class properties to see if there is a mismatch. I tried the representationsOfProperty method found near the bottom of https://cloud.google.com/appengine/docs/java/datastore/metadataqueries#Java_Kind_queries, but my queries return null.

I have a class defined similar to the following:

@PersistenceCapable(identityType = IdentityType.APPLICATION)

public class Container

{

@PrimaryKey

@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)

private Long containerID = null;

@Persistent

private Long extensionID = null;

@Persistent

private String homeUrl;

@Persistent

private Double containerScore;

}

I copied the code from the GAE page linked above. The only change I made was to convert 'key' to "key" since a string is requested and what is shown in the example isn't a character.

Collection<String> representationsOfProperty(DatastoreService ds,

String kind,

String property)

{

// Start with unrestricted non-keys-only property query

Query q = new Query(Entities.PROPERTY_METADATA_KIND);

// Limit to specified kind and property

q.setFilter(new FilterPredicate("__key__", Query.FilterOperator.EQUAL, Entities.createPropertyKey(kind, property)));

// Get query result

PreparedQuery pq = ds.prepare(q);

Entity propInfo = pq.asSingleEntity();

if( null == propInfo )

{

Collection<String> strs = new ArrayList<String>();

strs.add( "[ERROR: Invalid Query: " + pq.toString() + "]" );

return strs;

}

// Return collection of property representations

return (Collection<String>) propInfo.getProperty("property_representation");

}

I call that method with the following code:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

String prop = "containerID";

Collection<String> reps = representationsOfProperty( datastore, Container.class.toString(), prop );

Unfortunately, propInfo is always null. Any ideas on what I could try? Am I doing something wrong?

网友答案:

I just created a sample project to reproduce your issue. JDO is saving the entities in the Datastore, but not naming the primary key property according to the member variables of the PersistenceCapable class. Check your datastore viewer and grab a random Container entity to see what's happening.

If you need to do metadata queries on the id property in this manner, you should be aware that the ID/Name property is part of the key. Check this interesting talk to learn more about how the Datastore internals work (it's based on BigTable).

相关阅读:
Top