NullPointer on ObjectIdGenerator

116 прегледа
Пређи на прву непрочитану поруку

Stephan Gerth

непрочитано,
19. 3. 2015. 12:28:3019.3.15.
– dropwiz...@googlegroups.com
Hi all,

It seems that i have now solved my problems with JsonBackReference and @JsonManagedReference by not using it. 
Instead i use the

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=OxSecRight.class)

on top of my entity class.

But now i have the problem to save a new Entity where the 'id' field is per default NULL. Hibernate handles this.
Must i re-write my entity and save methods that a @POST can pass the JsonProcessor?

thanks
Stephan

Tatu Saloranta

непрочитано,
19. 3. 2015. 12:53:4119.3.15.
– dropwiz...@googlegroups.com
This is more a question for Jackson user/dev list. But what is the exception you get? While missing Object Ids are not currently supported, perhaps it would be possible to allow usage for serialization.
There are a few question about semantics -- for example, would this simply mean that these are consider "anonymous" Objects, which can not be serialized using id? -- but it might be doable.

for this.

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephan Gerth

непрочитано,
20. 3. 2015. 05:33:1820.3.15.
– dropwiz...@googlegroups.com
Hmmm, i hope i'm not alone with this if other users come from the hibernate side.

I will store a new entity where the id is 'null' per default. Hibernate will handle this.

@id 
 private Long id;
 private String loginname;

OxSecUser obj = new OxSecUser();
obj.setLoginname("test_loginname");
 . . .
response = invocationBuilder.post(Entity.entity(obj, MediaType.APPLICATION_JSON));

This generates a Json:

{"id":null,"version":0,"loginname":"test_loginname","password": . . .

On the server side by the POST method it throws an error:

! Causing: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.oxitec.zkboost.backend.model.OxSecUser["id"])
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1446) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserializeFromObject(SuperSonicBeanDeserializer.java:225) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1045) ~[jackson-databind-2.5.1.jar:2.5.1]

So, my question is: must i modify the model classes so that a new entity has a default id value other than NULL i.e.

@Id
private Long id = Long.MIN_VALUE;

where i evaluate in the save method the value as a new entity?


Thanks all for informations about that
Stephan

Tatu Saloranta

непрочитано,
30. 3. 2015. 01:06:4230.3.15.
– dropwiz...@googlegroups.com
On Fri, Mar 20, 2015 at 2:33 AM, Stephan Gerth <terryt...@gmail.com> wrote:
Hmmm, i hope i'm not alone with this if other users come from the hibernate side.


Probably not, it sounds reasonable enough to me -- it's just happens to be bit more involved than vanilla handling by Jackson itself, where either Jackson generates the id, or it already exists. But in this case, it sometimes exists, other times not.

One potential problem is the case where id does not yet exist, but multiple references might exist within document. I don't know how those should be handled.
 
I will store a new entity where the id is 'null' per default. Hibernate will handle this.

@id 
 private Long id;
 private String loginname;

OxSecUser obj = new OxSecUser();
obj.setLoginname("test_loginname");
 . . .
response = invocationBuilder.post(Entity.entity(obj, MediaType.APPLICATION_JSON));

This generates a Json:

{"id":null,"version":0,"loginname":"test_loginname","password": . . .

On the server side by the POST method it throws an error:

! Causing: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.oxitec.zkboost.backend.model.OxSecUser["id"])
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1446) ~[jackson-databind-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserializeFromObject(SuperSonicBeanDeserializer.java:225) ~[jackson-module-afterburner-2.5.1.jar:2.5.1]
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1045) ~[jackson-databind-2.5.1.jar:2.5.1]

So, my question is: must i modify the model classes so that a new entity has a default id value other than NULL i.e.

@Id
private Long id = Long.MIN_VALUE;

where i evaluate in the save method the value as a new entity?

Ideally you shouldn't have to. As a work-around for now it may be necessary however.

I think it would be reasonable to allow deserialization with null id, although there is the potential concern that doing this could hide some actual problems (cases where id somehow mismapped).

But as a starting point, I will file an issue to allow handling of missing/null id for deserialization, for Jackson 2.6.0.

-+ Tatu +-

 


Thanks all for informations about that
Stephan


On Thursday, March 19, 2015 at 5:28:30 PM UTC+1, Stephan Gerth wrote:
Hi all,

It seems that i have now solved my problems with JsonBackReference and @JsonManagedReference by not using it. 
Instead i use the

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=OxSecRight.class)

on top of my entity class.

But now i have the problem to save a new Entity where the 'id' field is per default NULL. Hibernate handles this.
Must i re-write my entity and save methods that a @POST can pass the JsonProcessor?

thanks
Stephan

Одговори свима
Одговори аутору
Проследи
0 нових порука