我当前的项目需要在某些域字段上使用Hibernate的 @ColumnTransformer
,但我无法使Grails域类继承自Hibernate带注释的域类。
我得到 MappingException: Unknown entity: BaseBook
以下是代码摘录:
关联和验证约束写在这里
//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"
}
}
此处编写的自定义列转换(加密/解密)(无法在常规Grails域类中设置)
// 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
}
宣布BaseBook& BasePage(也需要通过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>
答案 0 :(得分:0)
如果我这样做的话。我将使用GORM事件beforeInsert / beforeUpdate和onLoad。
有关详细信息,请参阅: