Skizit
Skizit

Reputation: 44862

Play-framework: Hashtable in a model?

I'm trying to create a model which is a hashtable with a few accessor methods. The model looks like so...

public class MyHash(){
    Hashtable<Integer, CustomObject> hash = new Hashtable<Integer,CustomObject>();
    //few accessor methods here...
}

This class is then used inside another model. When the other mode initiates its variable like so...

this.hash = new MyHash();

the following exception occurs...

Internal Server Error (500) for request POST /login

Execution exception (In /app/controllers/Security.java around line 70)
IllegalArgumentException occured : No serializer found for class models.MyHash and no properties discovered to create BeanSerializer (to avoid exception, disable   SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:  User["hash"])

play.exceptions.JavaExecutionException: No serializer found for class models.MyHash and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: User["hash"])
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: No serializer found for class models.ListOfFilters and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: User["hash"])
at org.codehaus.jackson.map.ObjectMapper._convert(ObjectMapper.java:1784)
at org.codehaus.jackson.map.ObjectMapper.convertValue(ObjectMapper.java:1750)
at play.modules.mongo.MongoMapper.convertValue(MongoMapper.java:12)
at play.modules.mongo.MongoDB.save(MongoDB.java:239)
at models.User.save(User.java)
at controllers.Security.createNewUser(Security.java:70)
at controllers.Security.authenticate(Security.java:36)
at play.utils.Java.invokeStaticOrParent(Java.java:161)
at play.utils.Java.invokeChildOrStatic(Java.java:183)
at controllers.Secure$Security.invoke(Secure.java:187)
at controllers.Secure$Security.access$0(Secure.java:184)
at controllers.Secure.authenticate(Secure.java:64)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
... 1 more
Caused by: org.codehaus.jackson.map.JsonMappingException: No serializer found for class models.MyHash and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: User["hash"])
at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62)
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:268)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:160)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:131)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:297)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1030)
at org.codehaus.jackson.map.ObjectMapper._convert(ObjectMapper.java:1777)
... 17 more

Upvotes: 1

Views: 928

Answers (2)

mericano1
mericano1

Reputation: 2913

try adding @JsonAutoDetect to your MyHash class. Check the jackson docs for more info.

Also you need to map your hashmap properly as craigmj suggests.

Upvotes: 0

craigmj
craigmj

Reputation: 5077

I think this will get you halfway there: How do I use JPA to persist a Map (java.util.Map) object inside an entity and ensure the persistence cascades?

But you will still need to make the MyHash itself serializable, so that the framework knows how to store it into the db: it needs to become an @Entity itself.

Upvotes: 1

Related Questions