Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 将字段调整为存储到数据库_Grails_Gorm - Fatal编程技术网

Grails 将字段调整为存储到数据库

Grails 将字段调整为存储到数据库,grails,gorm,Grails,Gorm,假设我有一个字段content,它是json。我想将其存储在数据库中,以便我的域类只保留1字段。(这更像是一项脑力劳动;-) 我希望我的域对象只公开内容,因此我不想使用另一个字段,如字符串contentAsText,因为它在外部可见 在整个GORM文档中,我还没有找到如何管理它的方法。我尝试了beforeValidate()/beforeinstert()和onLoad()方法,但没有成功 如何在值持久化之前调整它 class MyDomain{ JSONElement content

假设我有一个字段
content
,它是json。我想将其存储在数据库中,以便我的域类只保留1字段。(这更像是一项脑力劳动;-)

我希望我的域对象只公开
内容
,因此我不想使用另一个字段,如
字符串contentAsText
,因为它在外部可见

在整个GORM文档中,我还没有找到如何管理它的方法。我尝试了
beforeValidate()/beforeinstert()
onLoad()
方法,但没有成功

如何在值持久化之前调整它

class MyDomain{

   JSONElement content

   static constraints = {
        content nullable: false, blank: false, sqlType: "text" // adapter from Map to String??
    }

    def setContent(String textContent){
        content = JSON.parse(textContent)
    }
}
我必须做两件事

  • def content
    替换为
    JSON content
    ,使其持久化,请参阅
  • 通过
    def setContent()
    将json字符串转换回json

  • 由于
    content
    JSONElement
    使用
    JSONObject
    JSONArray
    作为具体类。

    您可以为
    JSONElement
    定义自定义休眠
    用户类型
    ,如下所述:

    域内类约束:

    static constraints = {
       content type: JSONObjectUserType
    }
    
    用户类型类:

    import org.grails.web.json.JSONObject
    import org.hibernate.HibernateException
    import org.hibernate.engine.spi.SessionImplementor
    import org.hibernate.type.StandardBasicTypes
    import org.hibernate.usertype.EnhancedUserType
    
    import java.sql.PreparedStatement
    import java.sql.ResultSet
    import java.sql.SQLException
    import java.sql.Types
    
    class JSONObjectUserType implements EnhancedUserType, Serializable {
    
        private static final int[] SQL_TYPES = [Types.VARCHAR]
    
        @Override
        public int[] sqlTypes() {
            return SQL_TYPES
        }
    
        @Override
        public Class returnedClass() {
            return JSONObject.class
        }
    
        @Override
        public boolean equals(Object x, Object y) throws HibernateException {
            if (x == y) {
                return true
            }
            if (x == null || y == null) {
                return false
            }
            JSONObject zx = (JSONObject) x
            JSONObject zy = (JSONObject) y
            return zx.equals(zy)
        }
    
        @Override
        public int hashCode(Object object) throws HibernateException {
            return object.hashCode()
        }
    
        @Override
        public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            Object jsonObject = StandardBasicTypes.STRING.nullSafeGet(resultSet, names, session, owner)
            if (jsonObject == null) {
                return null
            }
            return new JSONObject((String) jsonObject)
        }
    
        @Override
        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index, session)
            } else {
                JSONObject jsonObject = (JSONObject) value
                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, jsonObject.toString(), index, session)
            }
        }
    
        @Override
        public Object deepCopy(Object value) throws HibernateException {
            return value
        }
    
        @Override
        public boolean isMutable() {
            return false
        }
    
        @Override
        public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value
        }
    
        @Override
        public Object assemble(Serializable cached, Object value) throws HibernateException {
            return cached
        }
    
        @Override
        public Object replace(Object original, Object target, Object owner) throws HibernateException {
            return original
        }
    
        @Override
        public String objectToSQLString(Object object) {
            throw new UnsupportedOperationException()
        }
    
        @Override
        public String toXMLString(Object object) {
            return object.toString()
        }
    
        @Override
        public Object fromXMLString(String string) {
            return new JSONObject(string)
        }
    }
    
    import org.grails.web.json.JSONObject
    import org.hibernate.HibernateException
    import org.hibernate.engine.spi.SessionImplementor
    import org.hibernate.type.StandardBasicTypes
    import org.hibernate.usertype.EnhancedUserType
    
    import java.sql.PreparedStatement
    import java.sql.ResultSet
    import java.sql.SQLException
    import java.sql.Types
    
    class JSONObjectUserType implements EnhancedUserType, Serializable {
    
        private static final int[] SQL_TYPES = [Types.VARCHAR]
    
        @Override
        public int[] sqlTypes() {
            return SQL_TYPES
        }
    
        @Override
        public Class returnedClass() {
            return JSONObject.class
        }
    
        @Override
        public boolean equals(Object x, Object y) throws HibernateException {
            if (x == y) {
                return true
            }
            if (x == null || y == null) {
                return false
            }
            JSONObject zx = (JSONObject) x
            JSONObject zy = (JSONObject) y
            return zx.equals(zy)
        }
    
        @Override
        public int hashCode(Object object) throws HibernateException {
            return object.hashCode()
        }
    
        @Override
        public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
            Object jsonObject = StandardBasicTypes.STRING.nullSafeGet(resultSet, names, session, owner)
            if (jsonObject == null) {
                return null
            }
            return new JSONObject((String) jsonObject)
        }
    
        @Override
        public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
            if (value == null) {
                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index, session)
            } else {
                JSONObject jsonObject = (JSONObject) value
                StandardBasicTypes.STRING.nullSafeSet(preparedStatement, jsonObject.toString(), index, session)
            }
        }
    
        @Override
        public Object deepCopy(Object value) throws HibernateException {
            return value
        }
    
        @Override
        public boolean isMutable() {
            return false
        }
    
        @Override
        public Serializable disassemble(Object value) throws HibernateException {
            return (Serializable) value
        }
    
        @Override
        public Object assemble(Serializable cached, Object value) throws HibernateException {
            return cached
        }
    
        @Override
        public Object replace(Object original, Object target, Object owner) throws HibernateException {
            return original
        }
    
        @Override
        public String objectToSQLString(Object object) {
            throw new UnsupportedOperationException()
        }
    
        @Override
        public String toXMLString(Object object) {
            return object.toString()
        }
    
        @Override
        public Object fromXMLString(String string) {
            return new JSONObject(string)
        }
    }