I have code that I want to run when a domain class object is created; in Java, I would include this code in the constructor. How to do it in Groovy / Grails?
Thank.
Have you seen this page about groovy constructors? I have had the success of adding map constructors to Grails domain classes using this technique.
. Grails ( , , ), , . , .
, no-arg , .
//Domain Class class Author { String name Author() { //Execute post creation code } Author(String _name) { name = _name //Execute post creation code } }
, - POGO, , , . , Author :
Author
Author(name: 'John Doe')
Depending on the specific use case, you can use GORM events ...
http://docs.grails.org/3.1.1/guide/single.html#5.5.1
So you can use
def beforeInsert() { doMyCustomThing() } def onLoad() { doMyCustomThing() }
There are several other options, including Hibernate events and custom GORM events.
Source: https://habr.com/ru/post/1542102/More articles:Is coffeescript `? .` a monad operator? - haskellHow to link MediaCapture source with CaptureElement using Caliburn.Micro? - mvvmПочему это бросает "CryptoMaterial: этот объект содержит недопустимые значения" в С++, но отлично работает в python - c++CSS3 selector for group labels of radio window without selection (not checked) - htmlFinding two tail values of P from a t-distribution and degrees of freedom in Python - pythonDatabase Documentation - sqlJava EE 7 Specification, EL 3.0. changes regarding type enforcement - java-eeAnalyze math expressions with pyparsing - python-3.xFont-awesome icon not showing in Bootstrap navbar - html5cannot insert into database using web service - jqueryAll Articles