我正在使用grails 2.3.4并且我有一个嵌入对象的域类。嵌入对象有一个名为'version'的属性,它似乎与GORM自动添加到数据库表的'version'字段冲突。结果是属于我的嵌入对象的'version'字段不是在数据库中创建的,因此我的应用程序无法正常工作。
我的代码如下所示:
class Thing {
String someText
EmbeddedThing embeddedThing
Date someDate
static embedded = ['embeddedThing']
static constraints = {
embeddedThing(unique: true)
}
}
class EmbeddedThing {
String textOfSomeSort
String version
String textOfSomeOtherSort
}
您可能认为快速解决方法是重命名嵌入对象的'version'属性,但该类属于包含的子项目(即JAR文件),我不允许其他人接触项目使用它。所以解决方案需要在我的域类中完全完成,或者至少以不改变嵌入对象类的方式完成。
答案 0 :(得分:0)
版本是一个特殊的列名,您应该在EmbeddedThin类中重命名版本字段
答案 1 :(得分:0)
我实际上通过使用Hibernate UserType来表示EmbeddedThing类来找到解决此问题的方法。 我的代码现在看起来像这样并且完美运行:
Thing.groovy:
import EmbeddedThingUserType
class Thing {
String someText
EmbeddedThing embeddedThing
Date someDate
static embedded = ['embeddedThing']
static mapping = {
version false
embeddedThing type: EmbeddedThingUserType, {
column name: "embedded_thing_text"
column name: "embedded_thing_version"
column name: "embedded_thing_other_text"
}
}
static constraints = {
embeddedThing(unique: true)
}
}
EmbeddedThing.groovy:
class EmbeddedThing {
String textOfSomeSort
String version
String textOfSomeOtherSort
}
EmbeddedThingUserType.groovy:
class EmbeddedThingUserType implements UserType {
int[] sqlTypes() {
return [StringType.INSTANCE.sqlType(),
StringType.INSTANCE.sqlType(),
StringType.INSTANCE.sqlType()]
}
Class returnedClass() {
return EmbeddedThing
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
if (resultSet && names) {
return new EmbeddedThing(
textOfSomeSort: resultSet?.getString(names[0] ?: '_missing_textOfSomeSort_'),
version: resultSet?.getString(names[1] ?: '_missing_version_'),
textOfSomeOtherSort: resultSet?.getString(names[2] ?: '_missing_textOfSomeOtherSort_'))
} else {
return null
}
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
if (value != null) {
preparedStatement.setString(index, value?.textOfSomeSort)
preparedStatement.setString(index + 1, value?.version)
preparedStatement.setString(index + 2, value?.textOfSomeOtherSort)
} else {
preparedStatement.setString(index, '_missing_textOfSomeSort_')
preparedStatement.setString(index + 1, '_missing_version_')
preparedStatement.setString(index + 2, '_missing_textOfSomeOtherSort_')
}
}
@Override
public boolean isMutable() {
return false
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return x.equals(y)
}
@Override
public int hashCode(Object x) throws HibernateException {
assert (x != null)
return x.hashCode()
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return value
}
@Override
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value
}
@Override
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached
}
}
Config.groovy中:
grails.gorm.default.mapping = {
'user-type'( type: EmbeddedThingUserType, class: EmbeddedThing)
}
答案 2 :(得分:-1)
请在“静态映射”中尝试version false
,以获取'EmbeddedThing'类。