user2325154 user2325154 - 2 months ago 8
Java Question

Extra/unwanted domain class name getting inserted into MongoDB collection with Morphia?

I am using Morphia (http://mongodb.github.io/morphia/1.2/getting-started/quick-tour/) to try out a sample CRUD application in Java + MongoDB. I tried inserting some documents in the collection (user) and it worked fine, however i see an extra field (

"className" : "com.study.mongodb.User"
) getting inserted in my collection which I do not want.

user: collection data

/* 1 */
{
"_id" : ObjectId("57dabf8be273f32fecf6e6cd"),
"className" : "com.study.mongodb.User",
"firstName" : "Alex",
"lastName" : "Foo",
"birthDate" : Date(60237752400000),
"hasPremiumAccess" : true
}

/* 2 */
{
"_id" : ObjectId("57dabf8be273f32fecf6e6ce"),
"className" : "com.study.mongodb.User",
"firstName" : "Sacha",
"lastName" : "Foo",
"birthDate" : Date(60564859200000),
"hasPremiumAccess" : false
}


Main.java

public class Main {

private final Datastore ds;
private final UserDAO userDAO;

public Main() {
this.ds = new MorphiaService().getDatastore();
this.userDAO = new UserDAOImpl(User.class, ds);
}

public static void main(String[] args) {
Main m = new Main();

//drop collection every time to prevent duplicate record insertion thru below method
m.dropCollection();

//save user
m.saveUsers();

//get user(s)
m.displayAllUsers();
m.displayUserByFirstAndLastName();

//update user(s)
//delete user(s)
}

private void dropCollection() {
ds.getDB().getCollection("user").drop();
}

public void saveUsers() {
System.out.println("\n***************Save user example***************");
User user1 = new User("Alex", "Foo", new Date(1978, 10, 10), true);
User user2 = new User("Sacha", "Foo", new Date(1989, 2, 23), false);
userDAO.save(user1);
userDAO.save(user2);
System.out.println("After persist:");
System.out.println("User1 objectId = " + user1.getObjectId());
System.out.println("User2 objectId = " + user2.getObjectId());
}

}


MorphiaService.java

public class MorphiaService {

private static final String HOST_NAME = "gsi-547576";
private static final int PORT = 27017;
private static final String DB_NAME = "test";

private Morphia morphia;
private Datastore datastore;

public MorphiaService() {
MongoClient mongoClient = new MongoClient(HOST_NAME, PORT);
morphia = new Morphia();
morphia.mapPackage("org.study.crud.domain");
datastore = morphia.createDatastore(mongoClient, DB_NAME);
}

public Morphia getMorphia() {
return morphia;
}

public void setMorphia(Morphia morphia) {
this.morphia = morphia;
}

public Datastore getDatastore() {
return datastore;
}

public void setDatastore(Datastore datastore) {
this.datastore = datastore;
}

}


UserDAOImpl.java

public class UserDAOImpl extends BasicDAO<User, ObjectId> implements UserDAO {

public UserDAOImpl(Class<User> entityClass, Datastore ds) {
super(entityClass, ds);
}

@Override
public List<User> getAllUsers() {
final Query<User> query = ds.createQuery(User.class);
final List<User> users = query.asList();
return users;
}

@Override
public User getByFirstNameLastName(String firstName, String lastName) {
Query<User> query = createQuery().field("firstName").equal(firstName).field("lastName").equal(lastName);
return query.get();
}

}


User.java

@Entity("user")
public class User {

@Id
private ObjectId objectId;
private String firstName;
private String lastName;
private Date birthDate;
private boolean hasPremiumAccess;

/**
* keep an empty constructor so that morphia can recreate this entity when you want to fetch it from the database
*/
public User() {
}

/**
* full constructor (without objectId, we let morphia generate this one for us)
*
* @param firstName
* @param lastName
* @param birthDate
* @param hasPremiumAccess
*/
public User(String firstName, String lastName, Date birthDate, boolean hasPremiumAccess) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hasPremiumAccess = hasPremiumAccess;
}

public ObjectId getObjectId() {
return objectId;
}

public void setObjectId(ObjectId objectId) {
this.objectId = objectId;
}

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 Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public boolean isHasPremiumAccess() {
return hasPremiumAccess;
}

public void setHasPremiumAccess(boolean hasPremiumAccess) {
this.hasPremiumAccess = hasPremiumAccess;
}

@Override
public String toString() {
return "User{" + "objectId=" + objectId + ", firstName=" + firstName + ", lastName=" + lastName + ", birthDate=" + birthDate + ", hasPremiumAccess=" + hasPremiumAccess + '}';
}

}


Why is this field with 'className' getting inserted in the collection?

Answer

I tried to reproduce this scenario. We can disable this behaviour using below annotation.

@Entity(value = "user", noClassnameStored = true)

Regards, Sanjay Reddy