regarding managing dependencies in MongoDB using Java

18 views
Skip to first unread message

Umesh Pratap Singh

unread,
Jul 19, 2016, 12:54:40 PM7/19/16
to mongodb-dev
Hi All,
I am new to MongoDB, and Jongo / Morphia. 

Trying to get hands on with the mapping concept in MongoDB using , Java and Jongo / Morphia Unable to figure out how to establish mapping relationship between entities.
Can someone please help me out with some example ?

For example :

I have a json 

{
"name": "abc",
"password": "xyz",
"address": {
"street": "ghgjk",
"pin": 25646
},
"readingHabbits": ["jkjsdj", "sdkhks", "jlcsd"],
"eatingHabbits": {
"internalObjOne": {
"dkks": "jdskdfl",
"lfld": "hfslvlsk"
},
"internalObjectSecond": {
"cjdlksl": "hcdkjnjkcs",
"cjsdjljsl": "chsdskjc"
}
}
}

For above json I created some classes.

Base class :-

@Document(collection="user")
public class User{
@Id
private String id;
private String name;
private String password;
@DBRef
private Address address;
@DBRef
private ReadingHabbits readingHabbits;
//setter and getter 
}

@Document(collection="address")
public class Address{
@Id
private String id;
private String street;
private int pin;
//setter and getter 
}

Do we need to create separate classes for "ReadingHabbits" and "EatingHabbits" ?

If not then is the above approach correct to map two different entities with DBRef and save it to two different collection in MongoDB using Jongo?

Or do we have any other way to do the same operation ?

Thanks In Advance..... :)

Justin Lee

unread,
Jul 19, 2016, 3:00:15 PM7/19/16
to mongo...@googlegroups.com
Using Morphia, your model would look like this:


@Entity("user")
public class User{
@Id
private String id;
private String name;
private String password;
@Embedded
private Address address;
private List<String> readingHabbits;
//setter and getter 
}

@Entity("address")
public class Address{
@Id
private String id;
private String street;
private int pin;
//setter and getter 
}

 
You would need a class for EatingHabits and you would use @Embedded on that List field, too.

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages