Morphia - It is a library for mapping Java Objects to and from Mongo DB.
Morphia uses Annotations like JPA.
For more info on Morphia
@ Entity
Helps us to store the class instance to Mongo DB via Morphia
@Entity(value="employee")
public class Employee {
}
Here the name 'employee' will be assigned to the MongoDB DBCollection.
If you don't want to store class name in the document you need to specify the argument as noClassNameStored = true
@Entity(value="employee", noClassNameStored = true)
public class Employee {
}
We want to store the class name in the document, if we are storing different entities within the same collection and reading them back. Unless we know the class name, it would be difficult to read them back.
If we are using only a single entity in the collection, it is useful to add noClassNameStored = true argument.
@Id - Mongo will automatically generate the unique Id for new objects so we don't have to be worried about. It is like a primary key on the collection, we cannot remove that. The id field is of type ObjectId.
@Index - This is to apply index to fields . Indexing is done when database.ensureIndexes() method is called.
Morphia uses Annotations like JPA.
For more info on Morphia
@ Entity
Helps us to store the class instance to Mongo DB via Morphia
@Entity(value="employee")
public class Employee {
}
Here the name 'employee' will be assigned to the MongoDB DBCollection.
If you don't want to store class name in the document you need to specify the argument as noClassNameStored = true
@Entity(value="employee", noClassNameStored = true)
public class Employee {
}
We want to store the class name in the document, if we are storing different entities within the same collection and reading them back. Unless we know the class name, it would be difficult to read them back.
If we are using only a single entity in the collection, it is useful to add noClassNameStored = true argument.
@Id - Mongo will automatically generate the unique Id for new objects so we don't have to be worried about. It is like a primary key on the collection, we cannot remove that. The id field is of type ObjectId.
@Index - This is to apply index to fields . Indexing is done when database.ensureIndexes() method is called.
"_id" : ObjectId(...),
"name" : "Alice",
"age" : 27
}
@Embedded
1. Used for an object that is dependent on a parent object
2. Embedded objects do not have any scopes outside the parent object
3. They cannot be shared between objects.
4. You can have another @Embedded object in an Embedded object
@Entity(value="employee")
public class Employee {
@Embedded
Address address;
}
@Embedded
public class Address {
private String houseNo;
private String streetName;
private String city;
private String state;
}
1. Used for an object that is dependent on a parent object
2. Embedded objects do not have any scopes outside the parent object
3. They cannot be shared between objects.
4. You can have another @Embedded object in an Embedded object
@Entity(value="employee")
public class Employee {
@Embedded
Address address;
}
@Embedded
public class Address {
private String houseNo;
private String streetName;
private String city;
private String state;
}
No comments:
Post a Comment