GormUtils Class
Gorm-tools provides two utils to work with domains. See GormUtils and GormMetaUtils
GormUtils¶
Coping properties from source to target domain instance¶
Using copyProperties
we can copy some of properties from one domain to another:
User user = new User(firstName: 'John', lastName: 'Doe' age: 30) User user2 = new User() User user3 = new User() GormUtils.copyProperties(user, user2, false, 'firstName') GormUtils.copyProperties(user, user3, false, 'firstName', 'age') assert user2.firstName == user.firstName assert user2.age != user.age assert user3.firstName == user.firstName assert user3.age == user.age
user
is the source object, user2
and user3
are targets.
If the 3rd argument is false the method will override a target value even if it's not null.
Then we can specify properties to copy from the source object.
Fetching a nested property using a string name¶
GormUtils allows us to get a nested property from an object using a string as a path:
Address address = new Address(street: 'street', city: 'city') User user = new User(firstName: 'John', lastName: 'Doe' age: 30, address: address) String street = GormUtils.getPropertyValue(person, 'address.street') assert street == address.street
.
) symbol to a parent's property name.
Copying a domain instance¶
User user = new User(firstName: 'John', lastName: 'Doe' age: 30) User copy = GormUtils.copyDomain(User, user) assert copy.firstName == 'John' assert copy.lastName == 'Doe' assert copy.age == 30
User copy = GormUtils.copyDomain(new User(), user) assert copy.firstName == 'John' assert copy.lastName == 'Doe' assert copy.age == 30
GormMetaUtils¶
Getting a domain class¶
// using a fully qualified name with package GormMetaUtils.getDomainClass("gorm.tools.Org") // using a domain instance GormMetaUtils.getDomainClass(new Org()) // using a class GormMetaUtils.getDomainClass(Org)
Getting a persistent entity¶
// using a fully qualified name with package GormMetaUtils.getPersistentEntity("gorm.tools.Org") // using a domain instance GormMetaUtils.getPersistentEntity(new Org()) // using a class GormMetaUtils.getPersistentEntity(Org)