[英]How do I drop a database or collection using Hibernate OGM with MongoDB
I can't drop databases or collections using Hibernate OGM.我无法使用 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()
^
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.不过,我不确定在 OGM 使用数据库时删除数据库是否是个好主意。
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.
我们会帮助你。
So, just to give a concrete example of how to use this in tests, I am adding this answer.所以,只是为了给出一个如何在测试中使用它的具体例子,我添加了这个答案。
My working repeatable test class using OGM and MongoDB looks like this:我使用 OGM 和 MongoDB 的工作可重复测试类如下所示:
class OgmAccessorTest {
private static EntityManagerFactory entityManagerFactory;
private static EntityManager entityManager;
private static TransactionManager transactionManager;
@BeforeAll
static void initialize() {
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongodb");
entityManager = entityManagerFactory.createEntityManager();
transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
}
@BeforeEach
void clearDatabase() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
transactionManager.begin();
// Collection == name of the class being saved ⮧
entityManager.createNativeQuery("db.GameCharacter.drop()").executeUpdate();
transactionManager.commit();
}
@Test
void writeShouldBeAbleToWriteRetreivableGameCharacterToMongoDB() throws SecurityException, IllegalStateException, NotSupportedException, SystemException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
GameCharacter sylvia = GameCharacters.sylvia();
OgmAccessor.write(sylvia, entityManagerFactory);
transactionManager.begin();
GameCharacter loadedGameCharacter
= entityManager.find(GameCharacter.class, sylvia._id);
assertNotNull(loadedGameCharacter);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.