我在一个独立的 groovy 应用程序(没有 grails)中使用 GORM for MongoDB。我需要将这些对象呈现为 json,但 GORM 结果包含必须在返回的 json 输出中“隐藏”的附加属性。
如何为每个映射对象删除它们并仅获取普通对象?
每个实体都没有自定义代码的解决方案将是最好的。
@Entity
class Invoice {
String name
List<InvoiceItem> items
static embedded = ['items']
}
@Entity
class InvoiceItem {
String description
int qt
}
对象的打印属性def i = Invoice.getByName("MyFirstInvoice")
[
collectionName:invoice,
gormPersistentEntity:Invoice,
async:org.grails.datastore.gorm.async.GormAsyncStaticApi@6991948,
id:3,
gormDynamicFinders: [
org.grails.datastore.gorm.finders.FindOrCreateByFinder@3337ff9, org.grails.datastore.gorm.finders.FindOrSaveByFinder@6bb41fe3, org.grails.datastore.gorm.finders.FindByFinder@517fcc04, org.grails.datastore.gorm.finders.FindAllByFinder@1fe2dc3a, org.grails.datastore.gorm.finders.FindAllByBooleanFinder@736a488, org.grails.datastore.gorm.finders.FindByBooleanFinder@4c36e88a, org.grails.datastore.gorm.finders.CountByFinder@630612e4, org.grails.datastore.gorm.finders.ListOrderByFinder@6a0c11b2],
items:[InvoiceItem : (unsaved), InvoiceItem : (unsaved)],
name:MyFirstInvoice,
DB:com.mongodb.MongoDatabaseImpl@58acea9f,
attached:false,
class:class Invoice,
version:0,
dirtyPropertyNames:[],
errors:org.grails.datastore.mapping.validation.ValidationErrors: 0 errors,
collection:com.mongodb.MongoCollectionImpl@23253bb8,
transients:[],
dirty:false, count:2,
embedded:[items]
]
预期的输出类似于:
[name:MyFirstInvoice, items:[[description:one, qt:1], [description:two, qt:2]]]