Using signs and restrictions with grails 3.x domain objects

Is it possible to somehow create a feature with fields and constraints for these fields, and then create domain classes that implement this trait and collect constrained fields?

I have some code that looks something like this:

trait Shared {
  String sharedField

  static constraints = {
    sharedField nullable: true
  }
}

class ImplementingClass implements Shared {
  ...
}

Saving an instance of the implementation object using null sharedField is then rejected in violation of the constraint.

Can this be done? Is there an alternative syntax that is needed to use constraints and other GORM constructs in features implemented by domain objects?

+4
source share
1 answer

, Grails .

importFrom(Shared) , Grails constraints clazz.getDeclaredFields(), .

:

1) Java/ Groovy, , map

class SharedConstraints {
    String sharedField

    static constraints = {
      sharedField nullable: true
    }
}

SharedConstraints importFrom

2) groovy script (http://docs.grails.org/latest/guide/hibernate.html#addingConstraints). Java, .

SharedConstraints.groovy :

constraints = {
    sharedField nullable: true
}

IntelliJ Grails 3.0 (, ), script . src/java, Grails, script intelliJ screenshot

https://github.com/grails/grails-core/issues/10052

0

All Articles