3

This is my first time using MongoDb and morphia and I am pretty new to databases in general. I am wondering how I should organize my code with morphia. I was looking into using a DAO like it says on the morphia documentation, but the way they seem to be doing it, I would have to create a DAO for each model object that I have. I liked play's methodology of basically giving Model objects the ability to save themselves but I only have vague notions of what is going on under the hood here, so I am not sure how to achieve this with morphia, or if it is even desirable to do so. The code I have so far looks like this for the skeleton of a User model.

@Entity("user")
public class User extends BasicDAO<User, ObjectId>{
    @Id ObjectId id;

public String firstName;

public String lastName;

public String email;

@Indexed public String username;

public String password;

public User(Mongo mongo, Morphia morphia){
    super(mongo, morphia, "UserDAO");
}
public User(){
    this(DBFactory.getMongo(), DBFactory.getMorphia());
}

public void save(){
    ds.save(this);
}

public static User findByUsername(String uname){
    return DBFactory.getDatastore().find(User.class, "username =", uname).get();
}

public static boolean authenticate(String uname, String pword){
    User user = DBFactory.getDatastore().createQuery(User.class).filter("username", uname).filter("password", pword).get();
    if(user == null)
        return false;
    else
        return true;
}
}

It is currently throwing a StackOverflowException, and I am not sure why, but is this a reasonable pattern to try to accomplish?

Also the DBFactory basically just exists to maintain the singleton mongodb connection.

1
  • he is obviously using java and looking to organize his code, i don't think a scala example is any help Jul 20, 2013 at 14:51

2 Answers 2

2

Play 2.0 have a module for working with MongoDb I think You should give it a try https://github.com/vznet/play-mongo-jackson-mapper#readme

1

I started using Marphia with play framework 2.x. In my opinion, it is more sophisticated than the jackson mapper. I followed this example to install marphia plugin: https://github.com/czihong/playMongoDemo

5
  • Could you elaborate on how Morphia data conversion is more sophisticated than Jackson's?
    – StaxMan
    Jul 22, 2012 at 3:47
  • code.google.com/p/morphia As you can see in the marphia home page there are lot annotations available to customize. And also it provides a type safe dao. Sep 30, 2012 at 3:26
  • Right, but Jackson is highly configurable using annotations (including external "mix-in" annotations) as well. So while I don't doubt Morphia has plenty of functionality of it's own, I don't see anything but rather basic annotations on home page; and nothing particularly sophisticated. So I was curious as to what you consider sophisticated here, esp. given you compared it to Jackson.
    – StaxMan
    Sep 30, 2012 at 4:35
  • @KarthikSankar where in that code is he is initializing the MorphiaObject? Jul 20, 2013 at 14:58
  • @nightograph MorphiaObject is initialized on github.com/czihong/playMongoDemo/blob/master/app/Global.java Aug 14, 2014 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.