注解方式下报java.lang.UnsupportedOperationException: Clobs are not cacheable

今天在项目中将二级缓存的配置加上去了,但对于Clob字段却报如题的错误,完整错误如下:
Caused by: java.lang.UnsupportedOperationException: Clobs are not cacheable
at org.hibernate.type.ClobType.disassemble(ClobType.java:107)
at org.hibernate.type.TypeFactory.disassemble(TypeFactory.java:474)
at org.hibernate.cache.entry.CacheEntry.(CacheEntry.java:69)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:164)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2157)
at org.hibernate.loader.Loader.list(Loader.java:2117)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)

在Clob字段中增加如下注解(没有取消其所在类的只读缓存配置)也还是报错:
@Cache(usage = CacheConcurrencyStrategy.NONE)
@Lob
@Column(name = "SQL")
public Clob getSql() {
return sql;
}

1个回答

在hibernate Annotation中,实体BLOB、CLOB类型的注解与普通的实体属性有些不同,具体操作如下:

BLOB类型,类型声明为byte[]:

private byte[] content;

注解:

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENT", columnDefinition = "BLOB",nullable=true)
public byte[] getContent() {
return this.content;
}

public void setContent(byte[] content) {
    this.content = content;
}

CLOB类型,类型声明为String即可:

private String remark;

注解:

@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name="REMARK", columnDefinition="CLOB", nullable=true)
public String getRemark() {
return this.remark;
}

public void setRemark(String recvdocRemark) {
    this.remark = remark;
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!