Giter Club home page Giter Club logo

play-morphia's Introduction

PlayMorphia Play 2.8.x Module

This is a Play 2.8.x Module for Morphia (a MongoDB Java driver wrapper).

Installation

Add the following to your build.sbt:

libraryDependencies ++= Seq(
    guice,
    "org.mongodb" % "mongo-java-driver" % "3.12.0",
    "dev.morphia.morphia" % "core" % "1.5.8",
    )

Create a lib folder in your project directory and copy play-morphia.jar inside.

You will need to specify your MongoDB configuration in the conf/application.conffile:

playmorphia {
    uri="mongodb://127.0.0.1:27017/"
    database="YourDB"
    models="models"
    mongoClientFactory="controllers.mongoConfiguration.MyMongoClientFactory"
}

MongoClient Factory

Not all MongoClient options are supported by the MongoClientURI. For full customization of the generated MongoClient, you can specify your own MongoClientFactory class in the playmorphia section of the conf/application.conf, like this:

mongoClientFactory="controllers.mongoConfiguration.MyMongoClientFactory"

(I created a package inside controllers named mongoConfiguration)

The value should be the name of a class that extends from it.unifi.cerm.playmorphia.MongoClientFactory and provide at least an empty constructor or a constructor that takes a play Configuration. For example:

package controllers.mongoConfiguration;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.typesafe.config.Config;
import it.unifi.cerm.playmorphia.MongoClientFactory;

import java.util.Arrays;

public class MyMongoClientFactory  extends MongoClientFactory {

    private Config config;

    public MyMongoClientFactory(Config config) {
        super(config);
        this.config = config;
    }

    public MongoClient createClient() throws Exception {
         return new MongoClient(Arrays.asList(
                 new ServerAddress("localhost", 27017)
                 )
         );
     }

    public String getDBName() {
        return config.getString("playmorphia.database");
    }

}

Usage

Play Framework 2.8.x

A way to use PlayMorphia is to create a repositories package containing repository classes, one for each model. A repository class contains all methods to access to the collection members. The package structure should be similar to the following:

|- controllers
|- models
|- repositories

Model example:

import org.bson.types.ObjectId;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;

@Entity(value = "DB.users")
public class User  {

    @Id
    private ObjectId _id;
    private String firstname;
    private String lastname;
    private String email;

    public ObjectId getId() {
        return _id;
    }

    public void setId(ObjectId _id) {
        this._id = _id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Repository example:

package repositories;
       

import it.unifi.cerm.playmorphia.PlayMorphia;
import models.User;
import org.bson.types.ObjectId;

import javax.inject.Inject;

public class UserRepository {

    private final PlayMorphia morphia;

    @Inject
    public UserRepository(PlayMorphia morphia) {
        this.morphia = morphia;
    }

    public User findById(String id) {
        User user = morphia.
                datastore().
                createQuery(User.class).
                field("_id").
                equal(new ObjectId(id)).
                get();
        return user;
    }

    public void save(User u) {
        morphia.datastore().save(u);
    }
}

Controller example:

import views.html.modifyUserView;

public class UserController extends Controller {

    @Inject
    private UserRepository user;

    public Result modifyUser(String id) {
        User u = user.findById(id);
        return ok(modifyUserView.render(u));
    }
}

Contact

If you have a question or need some help you can just open an issue.

License

The license is Apache 2.0, see LICENSE.txt.

play-morphia's People

Contributors

morellik avatar ducthienbui97 avatar rozkerim avatar

Stargazers

jamesmcpc avatar Ethan Lin avatar Gael Gentil avatar Amit Pal avatar  avatar  avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

play-morphia's Issues

Not able to access repository class inside DTO object.

@morellik I used @Inject private UserRepository userRepository; declaration as per instructions. But I can only access it inside UserController class not UserData class (DTO object for user registration form). I used the same declaration @Inject private UserRepository userRepository inside UserData class but it returns null pointer exception at the runtime. Please help me resolve this issue.

package it.unifi.cerm.playmorphia does not exist

Thanks for upgrading the morphia library but I am unable to compile the project

#Stacktrace
play.sbt.PlayExceptions$CompilationException: Compilation error[package it.unifi.cerm.playmorphia does not exist] at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34) at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34) at scala.Option.map(Option.scala:146) at play.sbt.run.PlayReload$.$anonfun$taskFailureHandler$1(PlayReload.scala:33) at scala.Option.map(Option.scala:146) at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:28) at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:24) at play.sbt.run.PlayReload$.$anonfun$compile$3(PlayReload.scala:51) at scala.util.Either$LeftProjection.map(Either.scala:573) at play.sbt.run.PlayReload$.compile(PlayReload.scala:51)

I performed following steps:

  1. Make changes in applicaiton.conf as per the tutorial
  2. Added Playorphia.jar to the project
  3. Created MongoClientFactory
  4. Compile the project and it gave me above error.

This is kind of strange because while importing it.unifi.cerm.playmorphia.MongoClientFactory in my mongoClientFactory class it's showing me no errors. I can even import it into another class but at the time of compilation it's throwing me above stacks

What mistake I am making here?

error: cannot find symbol: private PlayMorphia morphia;

Hi, Thanks for building the morphia plugin. Although when I am trying to integrate this plugin with my play
2.5.X java application I am getting an error saying "private PlayMorphia morphi".
my config/application.config is

playmorphia {
    uri="mongodb://127.0.0.1:27017/YourDB"
    database="YourDB"
    models="models"
    mongoClientFactory="controllers.MyMongoClientFactory"
}

and i have created the lib folder under my app folder.
If u get time please revert back.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.