Extra/unwanted 域 class 名称被 Morphia 插入到 MongoDB 集合中?

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

我正在使用 Morphia (http://mongodb.github.io/morphia/1.2/getting-started/quick-tour/) 在 Java + MongoDB 中试用示例 CRUD 应用程序。我尝试在集合(用户)中插入一些文档并且它工作正常,但是我看到一个额外的字段("className" : "com.study.mongodb.User")被插入到我的集合中,这是我不想要的。

用户:采集数据

/* 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 + '}';
    }

}

为什么这个带有 'className' 的字段被插入到集合中?

我试图重现这个场景。我们可以使用以下注释禁用此行为。

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

此致, 桑杰雷迪