hibernate 操作clob (oracle 与 mysql)

1、 mysql 操作 clob
            playMethod.setPlayCode(Hibernate.createClob(playMethod.getPlayCodeContent()));
            session.save(playMethod);

2、 oracle 操作 clob
// 注意,这里的参数是个空格,先新增一个空的Clob进去
            playMethod.setPlayCode(Hibernate.createClob(" "));
            session.save(playMethod);
            // 强制执行
            session.flush();
            session.refresh(playMethod, LockMode.UPGRADE);

            SerializableClob sc = (SerializableClob) playMethod.getPlayCode();// playMethod.getPlayCode()是Clob类型的
            Clob wrapclob = sc.getWrappedClob();// 这里的Clob是java.sql.Clob
            CLOB clob = (CLOB) wrapclob;// 这里的CLOB是oracle.sql.CLOB
            Writer writer = clob.getCharacterOutputStream();
            writer.write(playMethod.getPlayCodeContent());// playMethod.getPlayCodeContent()是String类型的,在action里就是传这个进来,然后再通过文件流形式写成CLOB字段中
            writer.close();

            session.save(playMethod);

你可能感兴趣的:(Hibernate)