在休眠时懒洋洋地加载一个clob [英] Lazily loading a clob in hibernate

查看:158
本文介绍了在休眠时懒洋洋地加载一个clob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上我所拥有的是一个大的CLOB放在我想要按需加载的特定类上。这样做的天真方式是:

$ p $ class MyType {

// ...

@Basic(fetch = FetchType.LAZY)
@Lob
public String getBlob(){
return blob;
}
}

这虽然不起作用,显然是由于其实我使用的是oracle驱动程序,即Lob对象不被视为简单的句柄,但始终加载。或者让我从我的行动中被引导相信。有一种解决方案使用特殊的工具来进行惰性属性加载,但是由于Hibernate文档似乎暗示他们对使其正常工作不感兴趣,所以我宁愿不去那条路线。特别是不得不运行一个额外的编译阶段和所有。



因此,我设想的下一个解决方案是将此对象分隔到另一个类型并定义一个关联。不幸的是,尽管文档给出了冲突的信息,但对我来说显而易见的是,延迟加载在OneToOne与共享主键的关联上不起作用。我将该协会的一方设置为ManyToOne,但我不确定如何在共享主键时执行此操作。



那么任何人都可以提出最好的方法去解决这个问题?

解决方案

根据只有这个 PostgreSQL实现Blob的确很懒。所以最好的解决方案是将blob移到另一个表中。你必须使用共享主键吗?为什么你不这样做:

  public class MyBlobWrapper {
@Id
public Long getId(){
return id;
}
@Lob
public String getBlob(){
return blob;

@OneToOne(fetch = FetchType.LAZY,可选= false)
public MyClass getParent(){
return parent;
}
}


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?

解决方案

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:

public class MyBlobWrapper {
    @Id
    public Long getId() {
       return id;
    }
    @Lob
    public String getBlob() {
        return blob;
    }
    @OneToOne(fetch=FetchType.LAZY,optional=false) 
    public MyClass getParent() {
        return parent;
    }
}

这篇关于在休眠时懒洋洋地加载一个clob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆