MongoDB Java Object Builder 1.1

Utility class to directly load and save Java Objects from/to a MongoDB Database

  1. JoBa97

    First of all: The ObjectBuilder is a tool for developers. If you aren't a developer this is probably not interesting for you.

    • Saving custom objects to a MongoDB database
    • Loading custom objects from a MongoDB database
    • Compatible with pretty much every custom datastructure you can imagine
    1. Clone (git):
    2. Compile it using maven
    3. Add dependency to your maven project
    Code (Text):
    API Usage
    Saving Objects
    Code (Text):, customPlayerCollection);
    Loading Objects
    Code (Text):
    //Requires the DBObject (MongoDB) of the object you want to load
    CustomPlayer cp = ObjectBuilder.load(customPlayerdbo, CustomPlayer.class);
    Creating custom objects
    • Every custom object which is going to be loaded or saved to/from the DB needs to extend DBEntity
    • Objects which are going to be loaded from the DB need to implement DBLoadable, objects which are saved to the DB need to extend DBSaveable
    The next part is easier to explain with an example class.
    Code (Text):
    public class CustomPlayer extends DBEntity implements DBLoadable, DBSaveable {
        @DBLoad(fieldName = "username", priority = 10)
        private String name;
        @DBLoad(fieldName = "uuid", typeConverter = TypeConverter.UUIDStringConverter.class, priority = 10)
        private UUID uuid;
        @DBLoad(fieldName = "friends", typeConverter = TypeConverter.UUIDStringConverter.class)
        private Collection<UUID> friends;
        private Rank rank;//Rank is an enu
        @DBLoad(fieldName = "goals", typeConverter = TypeConverter.LocationConverter.class)
        private Location[] goals;

        public String getName() {
            return name;
        public UUID getUUID() {
            return uuid;
        @DBSave(fieldName = "rank")
        public Rank getRank() {
            return rank;
        @DBLoad(fieldName = "rank")
        public void setRank(Rank rank) {
            this.rank = rank;
        public Location[] getGoals() {
            return goals;
    • If a value should be saved to the DB either the field or a method returning the value needs a @DBSave annotation.
    • If a value should be loaded from the DB either the field or a method setting the value needs a @DBLoad annotation.
    Options in the annotations
    • (required) fieldName: The name of the field which holds the value in the Document
    • (situational) typeConverter:
      • Used to load values which are not
        • primitive (int, boolean, double, ...)
        • Strings
        • DBLoadable
        • One dimensional Array of the above
        • (Possibly nested) Collection of the above
      • Good to load Bukkit classes like Location directly without a Wrapper
      • For detailed instructions take a look into the TypeConverter class
    • (optional) priority: higher value -> loaded earlier

Recent Updates

  1. MongoDB 3.0