0
votes

My current project requires using Hibernate's @ColumnTransformer on certain domain fields, but I am unable to make Grails domain class inherit from Hibernate annotated domain class.

I get a MappingException: Unknown entity: BaseBook

Here is the code excerpt :

Grails domain class

Associations and validation constraints are written here

//grails-app/domain/Book.groovy
class Book extends BaseBook {

   String nom

   static hasMany = [pages:Page]
   static constraints = {
      nom(nullable:true)
   }
   //static mapWith = "none"
   static mapping ={
      table "book"
   }
}

Hibernate annotated base class

Custom column transformation (encryption/decryption) written here (not possible to set in regular Grails domain classes)

// src/groovy/BaseBook.groovy
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Version
import javax.persistence.Column
import org.hibernate.annotations.ColumnTransformer

@Entity 
class BaseBook { 
   @Id @GeneratedValue 
   Long id

   @Version
   Long version

   @ColumnTransformer(read = "AES_DECRYPT(title, 'password')", 
                      write = "AES_ENCRYPT(?, 'password')")
   @Column(columnDefinition = "VARBINARY(500)")
   String title
}

Hibernate mapping

It declares BaseBook & BasePage (which needs also to be extended by Page)

<!-- grails-app/conf/hibernate.cfg.xml -->
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
   'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>

   <session-factory>
      <mapping class='BaseBook' />
      <mapping class='BasePage' />
   </session-factory>

</hibernate-configuration>
1

1 Answers

0
votes

If I were to do this. I'd be using the GORM events beforeInsert/beforeUpdate and onLoad.

For more information see: