简体   繁体   English

在Morphia createQuery方法中传递集合名称是一种好习惯吗?

[英]Is it good practice to pass collection name in Morphia createQuery method?

I usually use this approach while instantiating the query object: 我通常在实例化查询对象时使用此方法:

Query<Product> query = datastore.createQuery(Product.class);

But Morphia allows you to pass the collection name also while instantiating the query object, which would look like this: 但Morphia允许您在实例化查询对象时传递集合名称,如下所示:

Query<Product> query = datastore.createQuery(COLLECTION_NAME, Product.class);

I am unable to understand why do we have to pass the Collection name explicitly while creating the query object? 我无法理解为什么在创建查询对象时我们必须显式传递Collection名称?

When we create the model, in this case in the Product class we are already binding the collection name under the Entity annotation: 当我们创建模型时,在这种情况下,在Product类中,我们已经在Entity注释下绑定了集合名称:

@Entity(value = "product", noClassnameStored = true)
class Product {
  // model attributes declared here
}

One reason I can think of is, if the same model is used across multiple collection then we might need to pass the collection name, but even if it is so is this a good practise and does it abide the ORM guidelines? 我能想到的一个原因是,如果在多个集合中使用相同的模型,那么我们可能需要传递集合名称,但即使它是这样,这是一个好的做法并且它是否遵守ORM指南? Please help me understand. 请帮我理解。

That method is used for cases where one entity is stored in different collections depending on the application usage. 该方法用于根据应用程序使用情况将一个实体存储在不同集合中的情况。 It's "hidden" on the AdvancedDatastore interface so that all you should see on a Datastore reference is the one createQuery() that takes the type. 它在AdvancedDatastore接口上“隐藏”,因此您在Datastore引用上看到的所有内容都是采用该类型的createQuery() Most users won't need the overridden form but it was added years ago as a convenience for those who needed to map to multiple locations. 大多数用户不需要覆盖的表单,但它是多年前添加的,以方便那些需要映射到多个位置的人。

It's a mild misfeature as it doesn't quite work well with @Reference fields. 这是一个温和的错误,因为它与@Reference字段不太合作。 I'm not sure how long that feature will continue to exist as it complicates some of the implementation for very little benefit. 我不确定该功能将持续存在多长时间,因为它使一些实现变得复杂,但收效甚微。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM