简体   繁体   English

Grails-如何在没有GORM附加字段的情况下获取对象域字段?

[英]Grails - how to get object domain fields without GORM additional fields?

In order to have a validation on my domain class object, I need to get all the relevant fields that I need to have validation on. 为了对我的域类对象进行验证,我需要获取需要对其进行验证的所有相关字段。
The exact fields I need are those who I defined by myself in my domain class and not those who GORM did. 我需要的确切字段是我自己在域类中定义的字段,而不是那些由GORM定义的字段。

I need to know how can I get those fields ? 我需要知道如何获得这些字段?
I mean, how to I get all the fields without 'id', 'version' and all other GORM generated fields. 我的意思是,如何获取所有没有'id','version'和所有其他GORM生成字段的字段。
Thanks! 谢谢!

The constraints property on a domain class instance gives you a list of ConstrainedProperty objects representing the properties that are listed in the constraints block in the domain class, in the order they are listed (see the bottom of this documentation page ). 域类实例上的constraints属性为您提供了ConstrainedProperty对象的列表,这些对象代表了按其列出的顺序在域类的constraints块中列出的属性(请参阅本文档页面的底部)。

static constraints = {
  prop2()
  prop1(nullable:true)
  prop3(blank:true)
}

So provided you have mentioned every property in the constraints block then you can use 因此,只要您在constraints块中提到了每个属性,就可以使用

myObj.constraints.collect { it.propertyName }

to get a list of the property names (in the above example you'd get [prop2, prop1, prop3] ). 获取属性名称列表(在上面的示例中,您将[prop2, prop1, prop3] )。

There are several things you can do. 您可以做几件事。

If you have a domain class like this: 如果您具有这样的域类:

// grails-app/domain/com/demo/Person.groovy
class Person {
    String firstName
    String lastName

    // notice that only firstName is constrained, not lastName
    static constraints = {        
        firstName matches: /[A-Z].*/
    }
}

You can interrogate the persistentProperties property to get a list of all persistent properties: 您可以查询persistentProperties属性以获得所有持久性属性的列表:

def person = new Person()
def personDomainClass = person.domainClass
// this will not include id and version...
def persistentPropertyNames = personDomainClass.persistentProperties*.name

assert persistentPropertyNames.size() == 2
assert 'firstName' in persistentPropertyNames
assert 'lastName' in persistentPropertyNames

If you wanted to do the same sort of thing but don't have an instance of the Person class to interrogate you can do something like this: 如果您想做同样的事情,但没有要查询的Person类的实例,则可以执行以下操作:

def personDomainClass = grailsApplication.getDomainClass('com.demo.Person')
// this will not include id and version...
def persistentPropertyNames = personDomainClass.persistentProperties*.name

assert persistentPropertyNames.size() == 2
assert 'firstName' in persistentPropertyNames
assert 'lastName' in persistentPropertyNames

You could also get the keys out of the constraints Map: 您还可以从约束图中获取键:

// this will include both firstName and lastName,
// even though lastName is not listed in the constraints
// closure.  GORM has added lastName to make it non
// nullable by default.
// this will not include id and version...
def constrainedPropertyNames = Person.constraints.keySet()

assert constrainedPropertyNames.size() == 2
assert 'firstName' in constrainedPropertyNames
assert 'lastName' in constrainedPropertyNames

I hope that helps. 希望对您有所帮助。

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

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