Mapping POJO for MongoDB has not been easier. Thanks to the new codecs feature in MongoDB Java 3.0 driver.
Simply mark your entities with annotation, create EntityCodec
and that's it! Then use standard methods for storing and accessing data from MongoDB.
- Simple and easy to use.
- Use standard (MongoDB) way for object manipulation.
- Works for synchronous as well as asynchronous version of MongoDB Java Driver.
- You can extend with your own custom codecs.
- It's fast and small - only 13kB dependency covered by unit and integration tests.
Mongo mapper is on Maven Central. Add following into your pom.xml
.
<dependency>
<groupId>eu.dozd</groupId>
<artifactId>mongo-mapper</artifactId>
<version>1.x.x</version>
</dependency>
compile 'eu.dozd:mongo-mapper:1.x.x'
-
Annotate your entities with
Entity
. Make sure every entity has exactly one String annotated withId
. All properties must have correct getter and setter methods according Java Bean specification.import eu.dozd.mongo.annotation.Entity; import eu.dozd.mongo.annotation.Id; @Entity public class Person { @Id String id; String name; int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
-
Create mapper
CodecProvider
by callingMongoMapper.getProviders
.CodecRegistry codecRegistry = CodecRegistries.fromProviders(MongoMapper.getProviders());
-
Usage for standard driver:
MongoClientOptions settings = MongoClientOptions.builder().codecRegistry(codecRegistry).build(); MongoClient client = new MongoClient(new ServerAddress("localhost", 27017), settings);
-
Usage for asynchronous driver:
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(new ServerAddress("localhost", 27017))).build(); MongoClientSettings settings = MongoClientSettings.builder().codecRegistry(codecRegistry) .clusterSettings(clusterSettings).build(); MongoClient client = MongoClients.create(settings);
-
-
Access and store data like normal POJO.
MongoCollection<Person> collection = db.getCollection("persons", Person.class); Person person = new Person(); person.setName("Foo Bar"); // Store person normally. collection.insertOne(person); // Access data. Person person2 = collection.find.first()
- Entity reference - make sure all entities classes are annotated with
Entity
. - Embedded entities - entities annotated with
Embedded
does not need to have an ID. - @java.beans.Transient - annotated getter with it.
- Feel free to create issue or pull request if you missing some functionality.
- You can create other Codecs for you special classes.
- Added
BigDecimalCodec
andBigDecimalCodecProvider
as an example. - Don't forget to call
MongoMapper.addProvider(yourCustomCodecProvider)
.
Eclipse uses its own Java compiler which is not strictly standard compliant and requires extra configuration. In Java Compiler -> Annotation Processing -> Factory Path you need to add ClassIndex jar file. See the screenshot.
Copyright 2016 Zdenek Dolezal
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.