废弃。
供稿人:刘路峰
Domain类中定义多个大字段(CLOB)、并且使用Oracle数据库时,读写时容易发生 “ORA-22295: 不能把超过 4000 字节数据绑定到语句 1 中的 LOB 和 LONG”、“java.sql.SQLException: 流已被关闭” 等异常,详见《采用Oracle数据库、并且Domain类中定义多个大字段时的隐患》。
在BroFramework中,可以按照下面的代码样例编写大字段相关代码,以规避上述问题。
定义Domain类:
import bropen.toolkit.orm.OracleClobType;
/**
* 一个包含多个CLOB字段的Domain类
*/
public class ProcessDefinition {
/** 第一个大字段 */
String foo;
/** 第二个大字段 */
String bar;
static constraints = {
foo(nullable:true, maxSize:999999999)
bar(nullable:true, maxSize:999999999)
}
static mapping = {
foo type: "text"
// 从第二个开始的大字段类型改为“OracleClobType.getType()”,如果多数据源,可以getType()中传入数据源的名称
bar type: OracleClobType.getType()
}
// afterInsert 和 afterUpdate 事件中:
// 第二个大字段开始的所有大字段,单独更新每个大字段
def afterInsert() {
updateBar();
}
def afterUpdate() {
updateBar();
}
// 更新大字段
private void updateBar() {
if ( this.bar2 == null ) return;
executeUpdate(
"update bropen.bpm.definition.ProcessDefinition p set p.bar=? where p.id=?",
[this.bar2, this.id]
);
}
}
所有对此 domain 实体的保存,如果存在多个 save,则只有对这个实体的最后一次 save 调用时才可以使用 flush:true。例如:
// 错误的代码
definition.setBar( "s1234" );
def su = definition.save(flush:true);
definition.setBar( "s4567" );
def su = definition.save(flush:true);
//正确的代码:
definition.setBar( "s1234" );
def su = definition.save();
definition.setBar( "s4567" );
def su = definition.save(flush:true);