This question already has answers here:
a field name “ClassName” is being inserted into mongodb by morphia
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
我正在使用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)


问候,
桑杰·雷迪(Sanjay Reddy)

关于java - 使用Morphia将多余的/不需要的域名类名称插入MongoDB集合中吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39515336/

10-12 02:16