gpt4 book ai didi

java - 在 Hibernate org.hibernate.Query 中将 Clob 数据作为字符串(渴望)加载

转载 作者:行者123 更新时间:2023-11-30 09:08:20 25 4
gpt4 key购买 nike

我需要使用 Hibernate 从 clob Oracle 列中获取一个字符串, 没有延迟加载,也没有使用 Criteria 查询。它目前返回一个代理类(例如 $Proxy30),但我需要一个字符串(或者我可以转换为字符串的东西)。根据调试器,该代理适用于 oracle.sql.CLOB。

当我使用 Criteria 查询时(只有该字段的常规 Column 注释映射),这工作得很好。但是我们在一个区域中“构建”了一个使用简单 org.hibernate.Query 和 AliasToEntityMapResultTransformer.INSTANCE 的自定义 SQL 查询,这就是代理问题出现的地方。因为它只是一个 org.hibernate.Query,我假设它根本不是指我的注释映射,所以我认为乱用注释不会有帮助。

由于此处不能使用条件查询(我们正在为某些高级搜索报告要求构建查询字符串),我应该研究什么以找到解决方法?或者更好的是,我最省力的方法是什么?

此外,这还适用于 Apache Flex 应用程序 - 我需要类型为字符串(而不是 CLOB),以便在客户端的数据传输对象中使用。也许其他 Flex 开发人员之前已经解决了这个问题?

Query query = getSessionFactory().getCurrentSession().createSqlQuery(sql);
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List... resultlist = ...query.list();
resultlist.iterator();
//iterate results...
Object shouldBeAString = (Object)result.get("CLOB_COLUMN_NAME");
//The object above should be a String but is a $Proxy30
//Debugger shows an "h" property of type SerializableClobProxy

如果我需要一个自定义的“ResultTransformer” - 任何指向相关文档的链接都将不胜感激......

最佳答案

我只是遇到了同样的问题,各种链接建议将 spring 升级到 4.0.1+ 并将 hibernate 升级到 4.3.x,但这没有任何区别。然后我遇到了这个链接,它解决了我的问题。作者为 Clob 编写了一个自定义 ResultTransformer,然后将其设置为您查询的转换器,而不是 AliasToEntityMapResultTransformer。

http://javatechtricks.blogspot.co.uk/2012/12/hibernate-clob-to-string-conversion.html

下面文章中的代码:

public class MyResultTransformer extends BasicTransformerAdapter {

public final static MyResultTransformer INSTANCE;
static {
INSTANCE = new MyResultTransformer();
}

private MyResultTransformer() {

}
private static final long serialVersionUID = 1L;

@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < aliases.length; i++) {
Object t = tuple[i];
if (t != null && t instanceof Clob) {
Clob c = (Clob) tuple[i];
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copy(c.getAsciiStream(), bos);
t = new String(bos.toByteArray());
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
map.put(aliases[i], t);
}
return map;
}
}

然后在你的代码中替换

query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);

query.setResultTransformer(MyResultTransformer.INSTANCE);

此外,另一种解决方案可能是更改列类型,我自己还没有尝试过。

JDBC large objects The types blob and clob provide mappings for the Blob and Clob classes in the java.sql package. If you are dealing with truly large values, your best bet is to declare the properties as either Blob or Clob—even though this introduces an explicit JDBC dependency to your data object, it easily allows Hibernate to leverage JDBC features to lazily load the property value only if you need it.

If you are not worried that the data is too huge, you can spare yourself this direct JDBC interface, declare the property type as String or byte[ ], and map it using text or binary. These correspond to SQL column types of CLOB and VARBINARY (RAW in Oracle, BYTEA in PostgreSQL), respectively, and the values are loaded immediately into the properties when the object is loaded.

来源:http://oreilly.com/java/excerpts/harnessing-hibernate/hibernate-types.html

关于java - 在 Hibernate org.hibernate.Query 中将 Clob 数据作为字符串(渴望)加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23581549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com