Hashmap in persistence manager

so I'm trying to create a google engine using servlets, filters, etc. I have a java file that looks something like this:

public class Idea implements Comparator<Idea> { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key key; @Persistent private User author; @Persistent private String content; @Persistent private Date date; @Persistent private Map<User, Boolean> positiveVotes ; @Persistent private Map<User, Boolean> negativeVotes; public Idea(User author, String content, Date date) { this.author = author; this.content = content; this.date = date; this.positiveVotes = new HashMap<User, Boolean>(); this.negativeVotes = new HashMap<User, Boolean>(); } 

but when I try to run my program, I get an exception stack starting with:

 Feb 13, 2010 5:01:23 PM com.google.apphosting.utils.jetty.JettyLogger warn WARNING: /sign java.lang.IllegalArgumentException: positiveVotes: java.util.HashMap is not a supported property type. at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:145) at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:127) at com.google.appengine.api.datastore.Entity.setProperty(Entity.java:280) 

So my question is why it complains that java.util.HashMap is not a supported property type, and also what can I do to get around it. Thank you hope someone answers soon.

+6
java google-app-engine persistence
source share
3 answers

This is not a supported type for serialization. You can view a list of supported types and consider alternatives. I might be missing out on something, but can you just save the sets of users that support or oppose the idea? What is the purpose of the logical? Note that a HashSet is a supported type.

+2
source share

You can ask GAE to save the HashMap as a Blob value by adding a JDO annotation to mark this field as a saved serialized:

@Persistent (serialize = true)

https://code.google.com/intl/pl/appengine/docs/java/datastore/dataclasses.html#Serializable_Objects

+5
source share

private positiveVotes card;

Saving a hash map to see if a user has voted or not can become extremely ineffective if the number of users becomes large.

why don't you keep the class like this:

 class Votes { private Key key; private Key ideaId; private User voter; private Boolean positive; // true is positive, false is negative } 

and for each idea, just query the Votes table to see if the user has voted for this idea. if he was positive or negative.

 Query query = pm.newQuery(Votes.class); query.setFilter("ideaId == :ideaIdParam && user == :userParam"); List<Votes> userVotes = query.execute(ideaId, user); if(userVotes != null && !userVotes.isEmpty()){ return userVotes.get(0).getPositive(); // this gives the users reaction } else { return null; // this means no reaction } 

Now you can use this to get a specific user response or a loop over the list and get all user reactions by removing userParam from the request.

0
source share

All Articles