在休眠状态下延迟加载 Clob
人们可以通过谷歌搜索找到很多关于这个问题的信息,但我还没有找到解决这个问题的可行解决方案。
基本上我拥有的是一个特定类的大 CLOB,我想按需加载它。 执行此操作的天真的方法是:
class MyType {
// ...
@Basic(fetch=FetchType.LAZY)
@Lob
public String getBlob() {
return blob;
}
}
但这不起作用,显然是因为我使用的是 oracle 驱动程序,即 Lob 对象不被视为简单的句柄,而是始终被加载。 或者说,我的尝试让我相信了这一点。 有一种解决方案使用特殊的仪器来延迟属性加载,但 Hibernate 文档似乎表明他们对使其正常工作不太感兴趣,所以我宁愿不走这条路。 特别是必须运行额外的编译过程等等。
因此,我设想的下一个解决方案是将这个对象分离为另一种类型并定义一个关联。 不幸的是,虽然文档提供了相互冲突的信息,但对我来说,显然延迟加载不适用于具有共享主键的 OneToOne 关联。 我将关联的一侧设置为 ManyToOne,但我不太确定当存在共享主键时如何执行此操作。
那么有人可以建议解决这个问题的最佳方法吗?
There's a lot one can find about this googling a bit but I haven't quite found a workable solution to this problem.
Basically what I have is a big CLOB on a particular class that I want to have loaded on demand. The naive way to do this would be:
class MyType {
// ...
@Basic(fetch=FetchType.LAZY)
@Lob
public String getBlob() {
return blob;
}
}
That doesn't work though, apparently due to the fact I'm using oracle drivers, i.e. Lob objects aren't treated as simple handles but are always loaded. Or so I've been led to believe from my forays. There is one solution that uses special instrumentation for lazy property loading, but as the Hibernate docs seem to suggest they're less than interested in making that work correctly, so I'd rather not go that route. Especially with having to run an extra compile pass and all.
So the next solution I had envisioned was separating out this object to another type and defining an association. Unfortunately, while the docs give conflicting information, it's apparent to me that lazy loading doesn't work on OneToOne associations with shared primary key. I'd set one side of the association as ManyToOne, but I'm not quite sure how to do this when there's a shared primary key.
So can anybody suggest the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据this,只有 PostgreSQL 将 Blob 实现为真正的惰性。 所以最好的解决方案是将 blob 移动到另一个表。 必须使用共享主键吗? 你为什么不做这样的事情:
According to this only PostgreSQL implements Blob as really lazy. So the best solution is to move the blob to another table. Do you have to use a shared primary key? Why don't you do something like this:
您可以尝试将字段从
String
转换为Clob
(或Blob
),而不是使用 Hibernate 注释进行均衡:对我有用(在 Oracle 上,字段开始延迟加载)。
Instead of doing equilibristics with hibernate annotations, one may just try converting the field from
String
intoClob
(orBlob
):Worked for me (the field started to load lazily, on Oracle).
由于您似乎正在使用 Hibernate,我想知道您的问题是否与以下 Hibernate 功能有关:
使用延迟属性获取
请参阅使用 Maven 的 Hibernate 字节码检测。
Since you appear to be using Hibernate I wonder if your problem is related to the following Hibernate feature:
Using Lazy Properties Fetching
See Bytecode Instrumentation for Hibernate Using Maven.
老帖子,但只有一篇对我有帮助,感谢@TadeuszKopec 的回答。
看起来很难用 JPA 延迟加载 blob。 我尝试过 @OneToOne 关联,但它比帮助更复杂。
我只是将字节移动到另一个类,与 MyClass 没有关联(父级。相同的表,相同的 id):
只需记住在保存 blob 之前刷新父级:
现在我可以单独加载 pdf:
我只是 JPA 的初学者,希望有帮助。
Old post, but only one that helped me, thanks to @TadeuszKopec answer.
Looks like it is hard to do lazy loading of blob with JPA. I tried @OneToOne association, but it complicates more than help.
I just moved the bytes to another class, with no association with MyClass (parent. Same table, same id):
Just remember to flush parent, before saving the blob:
Now I can load the pdf alone:
I am just beginner with JPA, hope that helps.