如何配置grails域类属性以存储为(postgres 9.4)jsonb? [英] How do I configure a grails domain class attribute to be stored as (postgres 9.4) jsonb?
问题描述
class Test {
字符串数据
静态约束= {
}
静态映射= {
数据类型:'jsonb'
}
}
这会引发一个异常(原因最终是 Invocation init方法失败;嵌套异常是org.hibernate.MappingException:无法确定类型:jsonb,在表:test,for columns:[org.hibernate.mapping.Column(data)]
) 。
我也试过列:'data',sqlType:'jsonb'
,它创建了一个 text
列名为 data
。
如何正确告诉grails使用 jsonb
作为sql列类型?是否有可能?
$ b (postgresql jdbc驱动程序在版本9.4-1200.jdbc4中与hibernate 4一起使用。)
String
您可以: -
声明您自己的
org.hibernate.usertype.UserType
。添加到src / java
:public class JSONBType implements UserType {
@Override
public int [] sqlTypes(){
return new int [] {Types.OTHER};
@SuppressWarnings(rawtypes)
@Override
public Class returnedClass(){
return String.class;
$ b @Override
public boolean equals(Object x,Object y)throws HibernateException {
return(x!= null)&& x.equals(Y);
}
@Override
public int hashCode(Object x)throws HibernateException {
return x.hashCode();
}
$ b @Override
public Object nullSafeGet(ResultSet rs,String [] names,SessionImplementor sessionImplementor,Object owner)
抛出HibernateException,SQLException {
返回rs.getString(名称[0]);
$ b @Override
public void nullSafeSet(PreparedStatement st,Object value,int index,SessionImplementor sessionImplementor)
抛出HibernateException,SQLException {
st。 setObject(index,value,(value == null)?Types.NULL:Types.OTHER);
$ b @Override
public Object deepCopy(Object value)抛出HibernateException {
if(value == null)return null;
返回新的String((String)值);
}
@Override
public boolean isMutable(){
return false;
$ b @Override
public可串行化反汇编(Object value)抛出HibernateException {
return(Serializable)value;
}
@Override
public Object assemble(Serializable cached,Object owner)
throws HibernateException {
return cached;
$ b @Override
public Object replace(Object original,Object target,Object owner)
throws HibernateException {
return deepCopy(original);
$ / code $ / pre $
$ <$>域:
静态映射= {
数据类型:your.package.JSONBType,sqlType:jsonb
map jsonb
不是 String
,但直接指向 JSONObject
或到你现有的类或接口。在这种情况下,GORM将负责序列化/反序列化json,而不再需要在应用程序中明确地执行它。 这是 UserType
实现的示例。
I've tried to configure a domain class like this:
class Test {
String data
static constraints = {
}
static mapping = {
data type: 'jsonb'
}
}
This throws an exception (the reason, in the end, being Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: jsonb, at table: test, for columns: [org.hibernate.mapping.Column(data)]
).
I also tried column: 'data', sqlType: 'jsonb'
, which creates a text
column named data
.
How do I correctly tell grails to use jsonb
as the sql column type? Is it at all possible?
(The postgresql jdbc driver is used in version 9.4-1200.jdbc4 with hibernate 4.)
To configure domain to map jsonb
type to String
you can:
Declare your own
org.hibernate.usertype.UserType
. Add tosrc/java
:public class JSONBType implements UserType { @Override public int[] sqlTypes() { return new int[] { Types.OTHER }; } @SuppressWarnings("rawtypes") @Override public Class returnedClass() { return String.class; } @Override public boolean equals(Object x, Object y) throws HibernateException { return (x != null) && x.equals(y); } @Override public int hashCode(Object x) throws HibernateException { return x.hashCode(); } @Override public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessionImplementor, Object owner) throws HibernateException, SQLException { return rs.getString(names[0]); } @Override public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor sessionImplementor) throws HibernateException, SQLException { st.setObject(index, value, (value == null) ? Types.NULL : Types.OTHER); } @Override public Object deepCopy(Object value) throws HibernateException { if (value == null) return null; return new String((String)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 owner) throws HibernateException { return cached; } @Override public Object replace(Object original, Object target, Object owner) throws HibernateException { return deepCopy(original); } }
After that you can simply declare mapping in the domain:
static mapping = { data type: "your.package.JSONBType", sqlType: "jsonb" }
Also you can map jsonb
not to String
, but directly to JSONObject
or to your existing class or interface. In that case GORM will take responsibility for serializing/deserializing json and you no longer need do it explicitly in an application. Here is an example of such UserType
implementation.
这篇关于如何配置grails域类属性以存储为(postgres 9.4)jsonb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!