Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Morphia将额外/不需要的域名插入MongoDB集合?_Java_Mongodb_Morphia - Fatal编程技术网

Java 使用Morphia将额外/不需要的域名插入MongoDB集合?

Java 使用Morphia将额外/不需要的域名插入MongoDB集合?,java,mongodb,morphia,Java,Mongodb,Morphia,我正在使用Morphia()在Java+MongoDB中试用一个示例CRUD应用程序。我尝试在集合(user)中插入一些文档,效果很好,但是我看到在我的集合中插入了一个额外的字段(“className”:“com.study.mongodb.user”) 用户:收集数据 /* 1 */ { "_id" : ObjectId("57dabf8be273f32fecf6e6cd"), "className" : "com.study.mongodb.User", "first

我正在使用Morphia()在Java+MongoDB中试用一个示例CRUD应用程序。我尝试在集合(user)中插入一些文档,效果很好,但是我看到在我的集合中插入了一个额外的字段(
“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
}
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());
    }

}
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;
    }

}
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();
    }

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

}
Main.java

/* 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
}
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());
    }

}
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;
    }

}
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();
    }

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

}
MorphiaService.java

/* 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
}
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());
    }

}
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;
    }

}
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();
    }

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

}
UserDAOImpl.java

/* 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
}
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());
    }

}
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;
    }

}
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();
    }

}
@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)
问候,, 桑杰·雷迪