如何以与实现无关的方式在 JPA 中创建 Clob
我正在使用 Ejb3 和 JPA(目前基于 Hibernate 和 Oracle 10g)
我有一个包含 clob 的实体
@Entity
@Table(name = "My_TAB")
public class ExampleEntity implements java.io.Serializable {
private Clob someText;
public void setSomeText(Clob someText) {
this.someText= someText;
}
@Column(name = "COLUMN_NAME")
public Clob getSomeText() {
return this.someText;
}
然后我想保存这种类型的实体。
目前我正在执行以下操作完美,
ExampleEntity exampleEntity = new ExampleEntity();
exampleEntity.setSomeText(Hibernate.createClob(aStringValue));
someOtherDao.save(exampleEntity);
但是这将我的代码与 Hibernate 联系起来!到目前为止,我已经特意避免使用 Hibernate 扩展并仅使用 JPA 注释。该代码之所以有效,是因为 Hibernate 确实是我当前的实现。
是否有某种 JPA API 允许我以通用方式创建 clob?因此,如果以后我决定切换到 Toplink/EclipseLink 或其他东西,我将不需要改变任何东西?
I am using Ejb3 and JPA (based on Hibernate and Oracle 10g at the moment)
I have an entity that contains a clob
@Entity
@Table(name = "My_TAB")
public class ExampleEntity implements java.io.Serializable {
private Clob someText;
public void setSomeText(Clob someText) {
this.someText= someText;
}
@Column(name = "COLUMN_NAME")
public Clob getSomeText() {
return this.someText;
}
Then I want to save an entity of this type.
At the moment I am doing the following which works perfectly
ExampleEntity exampleEntity = new ExampleEntity();
exampleEntity.setSomeText(Hibernate.createClob(aStringValue));
someOtherDao.save(exampleEntity);
However this ties my code to Hibernate! I have specifically avoided so far Hibernate extensions and used only JPA annotations. The code works because indeed Hibernate is my current implementation.
Is there some sort of JPA API that allows me to create a clob in a generic way? So if later I decide to switch to Toplink/EclipseLink or something else I won't have to change a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JPA 规范(第 9.1.5 节)就是这样一个例子,
我相信这是 CLOB 的标准方式。
There's such an example is the JPA spec (§ 9.1.5)
I believe it's the standard way for CLOB.
我不确定我是否会再次这样做,但在过去,当我需要将我的应用程序限制为最广泛使用的 sql 类型子集时,我使用单独的 char 表实现了二进制对象,并将其存储为 gzipped 和 base 64 编码。使用 XML 映射,情况类似于:
在代码中,getValue 方法检索 getEncodedValue 结果,将它们连接在一起,然后解码并解压缩它们。作为一项优化,我在父表上放置了一个简单的值列,如果它可以容纳 2000 个字符,则使用它,并且仅在必要时才转到子表。
setValue 方法对其进行压缩和编码,如果适合则将其存储在简单列中,否则将其拆分为子记录。这也会让你延迟加载,如果数据适合单个列,它甚至不需要执行单独的查询。
如果您知道您的数据库将支持 Clob,则可能有点过分了,但在我们的情况下效果很好。
I'm not sure I would do it again, but in the past when I needed to limit my app to the most widely used subset of sql types, I implemented binary objects using a separate table of char and stored it gzipped and base 64 encoded. Using XML mapping, it was something like:
In the code, the getValue method retrieved the getEncodedValue results, concatenated them all together and then decoded and unzipped them. As an optimization, I put a simple value column on the parent table and used that if it could fit in the 2000 characters and only went to the child table if necessary.
The setValue method gzipped and encoded it and stored it in the simple column if it fit, otherwise split it into the child records. That also gets you lazy loading and if the data fits in a single column, it doesn't even have to do a separate query.
Probably overkill if you know that your databases will support clobs, but worked pretty well in our situation.