MappedSuperclass alternatives in Grails

In many past projects, I used this JPA / Hibernate approach to add auditing capabilities to the system. It is very effective and unobtrusive.

Is there an alternative to Grails @MappedSuperclass (without encoding domain model objects in Java instead of Groovy)? How can I declare a parent class in a subclass approach without creating a table for it? I read the GORM documentation ( 5.2.3 Inheritance in GORM ), but besides the table for the hierarchy and the table for the subclass discussion, I did not find any details on how to do this.

Alternatively, what is the recommended way to achieve this type of audit in Grails?

+1
source share
1 answer

OK, I did a little work, and I answered my question :)

In fact, it is as simple as declaring MappedSuperclass as abstract and grails will not create a table for it. I realized re-reading the manual (RTFM basically ... works wonders): "GORM supports inheritance from both abstract base classes and concrete GORM constants." That is, concrete classes are constant, so abstract classes are not. Pays to read more carefully.

eg.

abstract class Auditable { Date dateCreated Date lastUpdated } class Book extends Auditable { String title String description } 

Only a book table will be created, and it will have

date_created

and

LAST_UPDATED

the columns. In addition, as an added bonus, the DateCreated and lastUpdated properties are automatically timestamped by Grails.

Hope this helps others.

+4
source

Source: https://habr.com/ru/post/1401553/


All Articles