Skip to content

Updated Video.java and VideoRepository to use String for Id.#40

Open
rbramwell wants to merge 2 commits intojuleswhite:masterfrom
rbramwell:patch-1
Open

Updated Video.java and VideoRepository to use String for Id.#40
rbramwell wants to merge 2 commits intojuleswhite:masterfrom
rbramwell:patch-1

Conversation

@rbramwell
Copy link

'Id' needs to be String in order for Spring Data JPA to correctly assign an allow MongoDB to automatically generate an ObjectId('...') and return to Spring Data JPA.

MongoDB's auto generated ObjectId consists of alphanumeric characters and hyphens '-'. When a long is used as the datatype for the Id, Spring Data MongoDB will issue an update/overwrite a single document in MongoDB with '_Id': NumberLong(0).

Output from mongo shell (Id as long):

db.video.find().pretty()
{
"_id" : NumberLong(0),
"_class" : "org.magnum.mobilecloud.video.repository.Video",
"name" : "Video-3a3b3158-9517-48c4-bdbd-718374e296bd",
"url" : "http://coursera.org/some/video-3a3b3158-9517-48c4-bdbd-718374e296bd",
"duration" : NumberLong(2880000)
}
This differs from the desired outcome which is to create a new (additional) document on subsequent calls to src / test / java / org / magnum / mobilecloud / integration / test / VideoSvcClientApiTest.java

Output from mongo shell (Id as String in 2nd document):

db.video.find().pretty()
{
"_id" : NumberLong(0),
"_class" : "org.magnum.mobilecloud.video.repository.Video",
"name" : "Video-3a3b3158-9517-48c4-bdbd-718374e296bd",
"url" : "http://coursera.org/some/video-3a3b3158-9517-48c4-bdbd-718374e296bd",
"duration" : NumberLong(2880000)
}
{
"_id" : ObjectId("545cba6444ae69b880ba589b"),
"_class" : "org.magnum.mobilecloud.video.repository.Video",
"name" : "Video-a820232d-dca3-4754-8962-f5355e408d61",
"url" : "http://coursera.org/some/video-a820232d-dca3-4754-8962-f5355e408d61",
"duration" : NumberLong(60000)
}
Updated VideoRepository.java to extend MongoRepository<Video, String> instead of MongoRepository<Video, Long>. This is in line with change to Id as String insteaf og long.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments