Problems persisting and retrieving data from MongoDB.
odilonjk opened this issue · comments
Repository: https://github.com/odilonjk/jakartaee_store
Hello! I'm sorry if the following questions are to noob, but I'm struggling with these two points:
1 - I'm trying to persist an entity into MongoDB. In part it's persisted but the _id
is always 0. I didn't found in any JNoSQL example what am I forgetting to implement to do it work correctly.
So, for every POST I make to my API, the new entity overrides the old one with ID zero.
2 - When I try to retrieve the entity, it works perfectly on the service layer. I can put a breakpoint and the value received from MongoDB is correct. The problem is that my HTTP response always shows me an empty response: {}
. I think that's some issue on my resource layer, when it tries to return Response.ok(store).build();
. Am I forgetting some annotation somewhere or do I perhaps need to implement some serializer?
Thanks! :)
Hey, thank you for your question, I'm checking it.
Hey @odilonjk , please, check your Store
it ID is a primitive long, that means,
the default value zero, thus every time that you create an entity, it will have 0
as ID value:
- https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5
- https://github.com/odilonjk/jakartaee_store/blob/master/src/main/java/com/odilonjk/stores/entities/Store.java#L12-L13
- https://github.com/odilonjk/jakartaee_store/blob/master/src/main/java/com/odilonjk/stores/services/StoreService.java#L25-L26
You can either set a new value to each unique ID or lets MongoDB generate an ID for you.
ps: The ID that MongoDB generates is not an auto-increment ID such as a relational database does. I'll open a PR to show you soon.
Yeah it really makes sense. With your help I got it. :)
BTW the PR worked like a charm!
Thank you @otaviojava
You welcome, if you have any questions, please, let me know.