Making a domain non-persistent
In grails app, there might come scenarios where one need to create a non-persistent domain rather than creating a command obejct or POJO / POGO.
GORM comes with a handy static property mapWith which has default value GORM (which associates any domain with gorm persistence layer).
To make a domain non persistent set mapWith=”none”. For example, see Ticket domain below
[code lang=”groovy”]
public class Ticket {
String id
List<Long> productInstanceId
static hasMany = [productInstanceId:Long]
static mapWith = "none"
}
[/code]
Advantages:-
- Still behaves like a GORM Domain in most of the cases like Ticket domain above is still accessible by GrailsDomainClass
- When using multiple database , still respects the convention over configuration policy.
- supports scaffolding while same can’t be done for command objects.
Also, at some point of time to check on a particular domain that what type of mapping it has, fetch the static property with domain class. For the domain above, it would be
[code lang=”groovy”]
Ticket.mapWith
[/code]
or
[code lang=”groovy”]
Ticket.class.mapWith
[/code]
If doing a dynamic check then can find it with the help of DefaultGrailsDomainClass.
[code lang=”groovy”]
GrailsDomainClass domainClass = new DefaultGrailsDomainClass(Ticket.class)
domainClass.mappingStrategy
[/code]