javamongodbhibernatehibernate-ogm

How do I drop a database or collection using Hibernate OGM with MongoDB


I can't drop databases or collections using Hibernate OGM. I've tried using these native queries but an exception is thrown for both

entityManagerFactory = Persistence.createEntityManagerFactory("myPersistence-unit");
EntityManager entityManager = openEntityManager( entityManagerFactory);
entityManager.getTransaction().begin();

    String queryDropCollection = "db.Person.drop()";
    String queryDropDB = "db.dropDatabase()";

    entityManager.createNativeQuery(queryDropCollection).executeUpdate();
    entityManager.createNativeQuery(queryDropDB).executeUpdate();

entityManager.getTransaction().commit();
entityManager.close();

The exception for dropping the collection:

Exception in thread "main" com.mongodb.util.JSONParseException: 
db.Person.drop()
^

The exception for dropping the database:

Exception in thread "main" com.mongodb.util.JSONParseException: 
db.dropDatabase()
^

Solution

  • Sorry, this is not possible right now.

    I'm not sure if it would be a good idea to drop the database while OGM is using it, though.

    I've created these two issues to keep track from it:

    Thanks for the feedback. If you feel like trying to help us even more, you could try to solve the issues in the project and send us a fix. We will help you.