Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring Data MongoDB support enums?

For Java enum type, I learn that there are two solutions for MongoDB: serialization and using Jackson’s ObjectMapper. Can the MongoRepository work with an enum data type with either of those approaches or I have to write a customized repository?

like image 862
vic Avatar asked Mar 10 '15 19:03

vic


People also ask

How is enum stored in MongoDB?

Steps: Create and register a codec provider with Mongo Code Registry which Mongo uses to determine which Enum decoder to use a Java Enum value. Create and register Enum decoder for ProcessType. Create and register Enum with DB.

Does Spring support MongoDB?

The Spring framework provides powerful connectors to easily perform database operations with MongoDB. Data is stored as BSON objects in MongoDB making data retrieval easy. For this Spring Boot and MongoDB example tutorial, we are only concerned with the Persistence and Database layers.

What is Spring Data MongoDB?

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.

Is MongoDB good for spring boot?

Spring Boot is the easiest way to spin a spring project quickly and MongoDB is the most popular NoSQL database.


2 Answers

Yes, Spring Data MongoDB supports enums. Just use them in your domain model.

like image 103
Oliver Drotbohm Avatar answered Sep 19 '22 04:09

Oliver Drotbohm


Spring Data Mongodb can serialize enum into string using enum's name as value. Let's say, it uses the second approach from the article http://www.vineetmanohar.com/2010/01/3-ways-to-serialize-java-enums/. IMHO this can't be taken seriously, because the only correct way to store enums in the database is the approach #3 from the same article, let me cite it: "This approach involves assigning a an explicit user defined value to each enum constant and defining a toValue() and fromValue() methods on the enum to do the serialization and deserialization.". So, Spring Data Mongodb does not support enums.

like image 23
Igor Bljahhin Avatar answered Sep 22 '22 04:09

Igor Bljahhin